Up executes all migrations in order they were added.
(ctx context.Context)
| 81 | |
| 82 | // Up executes all migrations in order they were added. |
| 83 | func (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. |