Up executes all unapplied migrations for the provided runner. On success returns list with the applied migrations file names.
()
| 120 | // |
| 121 | // On success returns list with the applied migrations file names. |
| 122 | func (r *MigrationsRunner) Up() ([]string, error) { |
| 123 | if err := r.initMigrationsTable(); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | |
| 127 | applied := []string{} |
| 128 | |
| 129 | err := r.app.AuxRunInTransaction(func(txApp App) error { |
| 130 | return txApp.RunInTransaction(func(txApp App) error { |
| 131 | for _, m := range r.migrationsList.Items() { |
| 132 | // applied migrations check |
| 133 | if r.isMigrationApplied(txApp, m.File) { |
| 134 | if m.ReapplyCondition == nil { |
| 135 | continue // no need to reapply |
| 136 | } |
| 137 | |
| 138 | shouldReapply, err := m.ReapplyCondition(txApp, r, m.File) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | if !shouldReapply { |
| 143 | continue |
| 144 | } |
| 145 | |
| 146 | // clear previous history stored entry |
| 147 | // (it will be recreated after successful execution) |
| 148 | r.saveRevertedMigration(txApp, m.File) |
| 149 | } |
| 150 | |
| 151 | // ignore empty Up action |
| 152 | if m.Up != nil { |
| 153 | if err := m.Up(txApp); err != nil { |
| 154 | return fmt.Errorf("failed to apply migration %s: %w", m.File, err) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if err := r.saveAppliedMigration(txApp, m.File); err != nil { |
| 159 | return fmt.Errorf("failed to save applied migration info for %s: %w", m.File, err) |
| 160 | } |
| 161 | |
| 162 | applied = append(applied, m.File) |
| 163 | } |
| 164 | |
| 165 | return nil |
| 166 | }) |
| 167 | }) |
| 168 | |
| 169 | if err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | return applied, nil |
| 173 | } |
| 174 | |
| 175 | // Down reverts the last `toRevertCount` applied migrations |
| 176 | // (in the order they were applied). |