EnsureClean checks whether all migrations for the current version have been applied, without making any changes to the database. If not, returns a non-nil error.
(db *sql.DB)
| 172 | // applied, without making any changes to the database. If not, returns a |
| 173 | // non-nil error. |
| 174 | func EnsureClean(db *sql.DB) error { |
| 175 | sourceDriver, m, err := setup(db, migrations) |
| 176 | if err != nil { |
| 177 | return xerrors.Errorf("migrate setup: %w", err) |
| 178 | } |
| 179 | |
| 180 | version, dirty, err := m.Version() |
| 181 | if err != nil { |
| 182 | return xerrors.Errorf("get migration version: %w", err) |
| 183 | } |
| 184 | |
| 185 | if dirty { |
| 186 | return xerrors.Errorf("database has not been cleanly migrated") |
| 187 | } |
| 188 | |
| 189 | // Verify that the database's migration version is "current" by checking |
| 190 | // that a migration with that version exists, but there is no next version. |
| 191 | err = CheckLatestVersion(sourceDriver, version) |
| 192 | if err != nil { |
| 193 | return xerrors.Errorf("database needs migration: %w", err) |
| 194 | } |
| 195 | |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | // Returns nil if currentVersion corresponds to the latest available migration, |
| 200 | // otherwise an error explaining why not. |
no test coverage detected