MCPcopy Create free account
hub / github.com/daodst/chat / Up

Method Up

internal/sqlutil/migrate.go:83–118  ·  view source on GitHub ↗

Up executes all migrations in order they were added.

(ctx context.Context)

Source from the content-addressed store, hash-verified

81
82// Up executes all migrations in order they were added.
83func (m *Migrator) Up(ctx context.Context) error {
84 var (
85 err error
86 dendriteVersion = internal.VersionString()
87 )
88 // ensure there is a table for known migrations
89 executedMigrations, err := m.ExecutedMigrations(ctx)
90 if err != nil {
91 return fmt.Errorf("unable to create/get migrations: %w", err)
92 }
93
94 return WithTransaction(m.db, func(txn *sql.Tx) error {
95 for i := range m.migrations {
96 now := time.Now().UTC().Format(time.RFC3339)
97 migration := m.migrations[i]
98 logrus.Debugf("Executing database migration '%s'", migration.Version)
99 // Skip migration if it was already executed
100 if _, ok := executedMigrations[migration.Version]; ok {
101 continue
102 }
103 err = migration.Up(ctx, txn)
104 if err != nil {
105 return fmt.Errorf("unable to execute migration '%s': %w", migration.Version, err)
106 }
107 _, err = txn.ExecContext(ctx, insertVersionSQL,
108 migration.Version,
109 now,
110 dendriteVersion,
111 )
112 if err != nil {
113 return fmt.Errorf("unable to insert executed migrations: %w", err)
114 }
115 }
116 return nil
117 })
118}
119
120// ExecutedMigrations returns a map with already executed migrations in addition to creating the
121// migrations table, if it doesn't exist.

Callers 15

NewSqliteKeyChangesTableFunction · 0.95
OpenFunction · 0.95
CreateMembershipTableFunction · 0.95
CreateMembershipTableFunction · 0.95
Test_migrations_UpFunction · 0.95
NewPostgresReceiptsTableFunction · 0.95
NewPostgresEventsTableFunction · 0.95

Calls 3

ExecutedMigrationsMethod · 0.95
WithTransactionFunction · 0.85
FormatMethod · 0.80

Tested by 1

Test_migrations_UpFunction · 0.76