Execute all registered migration script in order and mark them as executed in migration_history table
()
| 87 | |
| 88 | // Execute all registered migration script in order and mark them as executed in migration_history table |
| 89 | func (m *migratorImpl) Execute() errors.Error { |
| 90 | // sort the scripts by version |
| 91 | sort.Slice(m.pending, func(i, j int) bool { |
| 92 | return m.pending[i].script.Version() < m.pending[j].script.Version() |
| 93 | }) |
| 94 | m.Info("Execute") |
| 95 | // execute them one by one |
| 96 | db := m.basicRes.GetDal() |
| 97 | for _, swc := range m.pending { |
| 98 | scriptId := getScriptId(swc.script.Name(), swc.script.Version()) |
| 99 | m.logger.Info("applying migration script %s", scriptId) |
| 100 | err := swc.script.Up(m.basicRes) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | err = db.Create(&MigrationHistory{ |
| 105 | ScriptVersion: swc.script.Version(), |
| 106 | ScriptName: swc.script.Name(), |
| 107 | Comment: swc.comment, |
| 108 | }) |
| 109 | if err != nil { |
| 110 | return errors.Default.Wrap(err, fmt.Sprintf("failed to execute migration script %s", scriptId)) |
| 111 | } |
| 112 | m.executed[scriptId] = true |
| 113 | m.pending = m.pending[1:] |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // HasPendingScripts returns if there is any pending migration scripts |
| 119 | func (m *migratorImpl) HasPendingScripts() bool { |