@@ -41,48 +41,6 @@ func InitMainDBMigrations(config config.AppConfig) (migration Migration, er erro
4141 return
4242}
4343
44- // RunMigrations used to run a migrations
45- func (migration Migration ) RunMigrations () {
46- slog .Info ("Migrations started from " , "directory" , migration .directoryName )
47- startTime := time .Now ()
48- defer func () {
49- slog .Info ("Migrations complete, total time taken " , "time" , time .Since (startTime ))
50- }()
51-
52- // dbVersion is the currently active database migration version
53- dbVersion , dirty , err := migration .m .Version ()
54- if err != nil && err != migrate .ErrNilVersion {
55- slog .Error (err .Error ())
56- }
57-
58- // localVersion is the local migration version
59- localVersion , err := migration .MigrationLocalVersion ()
60- if err != nil {
61- slog .Error (err .Error ())
62- }
63-
64- if dbVersion > uint (localVersion ) {
65- slog .Error (fmt .Sprintf ("Your database migration %d is ahead of local migration %d, you might need to rollback a few migrations" , dbVersion , localVersion ))
66- }
67- if dbVersion < uint (localVersion ) && dirty {
68- slog .Error (fmt .Sprintf ("Your currently active database migration %d is dirty, you might need to rollback it and then deploy again." , dbVersion ))
69- }
70-
71- err = migration .m .Up ()
72- if err != nil {
73- if err == migrate .ErrNoChange {
74- return
75- }
76-
77- dbVersion , _ , err2 := migration .m .Version ()
78- if err2 != nil {
79- slog .Error (err2 .Error ())
80- }
81-
82- slog .Error (fmt .Sprintf ("Migration failed with error %s, current active database migration is %d" , err , dbVersion ))
83- }
84- }
85-
8644// MigrationsUp used to make migrations up
8745func (migration Migration ) MigrationsUp () {
8846 err := migration .m .Up ()
@@ -114,16 +72,6 @@ func (migration Migration) MigrationsDown() {
11472 slog .Info ("Migration down completed" )
11573}
11674
117- // ForceVersion forces the migration to a specific version
118- func (migration Migration ) ForceVersion (version int ) {
119- err := migration .m .Force (version )
120- if err != nil {
121- slog .Error (err .Error ())
122- }
123-
124- slog .Info (fmt .Sprintf ("Migration force version %d complete" , version ))
125- }
126-
12775// CreateMigrationFile creates new migration files
12876func (migration Migration ) CreateMigrationFile (filename string ) (err error ) {
12977 if len (filename ) == 0 {
@@ -179,85 +127,6 @@ func (migration Migration) MigrationVersion() (err error) {
179127 return
180128}
181129
182- // MigrationLocalVersion gets the latest migration version from local file system
183- func (migration Migration ) MigrationLocalVersion () (localversion int , err error ) {
184- localDIRFileVersions , err := getMigrationVersionsFromDir (migration .directoryName )
185- if err != nil {
186- return 0 , fmt .Errorf ("can't get files information from local file system: %w" , err )
187- }
188-
189- if len (localDIRFileVersions ) == 0 {
190- slog .Warn ("no migration files found in local file system" )
191- return 0 , nil
192- }
193-
194- slog .Info (fmt .Sprintf ("latest migration version from local file system: %d" , localDIRFileVersions [0 ]))
195- return localDIRFileVersions [0 ], nil
196- }
197-
198- func getMigrationVersionsFromDir (dir string ) ([]int , error ) {
199- return []int {}, nil
200- }
201-
202- // GoToSpecificVersion migrates to a specific version
203- func (migration Migration ) GoToSpecificVersion (version uint ) (err error ) {
204- localDIRFileVersions , err := getMigrationVersionsFromDir (migration .directoryName )
205- if err != nil {
206- return fmt .Errorf ("can't get files information from local file system: %w" , err )
207- }
208-
209- if len (localDIRFileVersions ) == 0 {
210- slog .Warn ("no migration files found in local file system, hence migration not required" )
211- return nil
212- }
213-
214- dbversion , dirty , err := migration .m .Version ()
215- if err != nil {
216- if err == migrate .ErrNilVersion {
217- slog .Info ("no migration found, initializing DB with latest migration" )
218- err = migration .m .Migrate (version )
219- if err != migrate .ErrNoChange {
220- return err
221- }
222-
223- slog .Info (fmt .Sprintf ("database successfully initialized with migration: %d" , version ))
224- return nil
225- }
226- return err
227- }
228-
229- // if the database is in dirty state, we pick the previous successfully executed migration
230- // and force the database to that version
231- if dirty {
232- index , err := getIndexOfSlice (localDIRFileVersions , int (dbversion ))
233- if err != nil {
234- return errors .New ("database version corresponding file not found in local file system" )
235- }
236-
237- if len (localDIRFileVersions ) <= index + 1 {
238- return errors .New ("previous successfully executed migration not found in local file system" )
239- }
240- forceMigrateVersion := localDIRFileVersions [index + 1 ]
241-
242- err = migration .m .Force (forceMigrateVersion )
243- if err != nil {
244- return err
245- }
246- }
247-
248- err = migration .m .Migrate (version )
249- if err != migrate .ErrNoChange {
250- return err
251- }
252-
253- slog .Info (fmt .Sprintf ("database successfully migrated to version: %d" , version ))
254- return nil
255- }
256-
257- func getIndexOfSlice (slice []int , value int ) (int , error ) {
258- return 0 , nil
259- }
260-
261130func main () {
262131 // Setup config
263132 cfg , err := config .LoadAppConfig ()
0 commit comments