migrateUpCommands creates the command for applying migrations.
()
| 46 | |
| 47 | // migrateUpCommands creates the command for applying migrations. |
| 48 | func migrateUpCommands() *cobra.Command { |
| 49 | cmd := &cobra.Command{ |
| 50 | Use: "up", |
| 51 | Run: func(cmd *cobra.Command, args []string) { |
| 52 | // Define the source of the migrations. |
| 53 | migrations := migrate.EmbedFileSystemMigrationSource{ |
| 54 | FileSystem: ledgerforge.SQLFiles, |
| 55 | Root: "sql", |
| 56 | } |
| 57 | |
| 58 | // Fetch the configuration. |
| 59 | cnf, err := config.Fetch() |
| 60 | if err != nil { |
| 61 | logrus.Errorf("Error fetching config: %v", err) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | // Connect to the database. |
| 66 | db, err := database.ConnectDB(cnf.DataSource) |
| 67 | if err != nil { |
| 68 | logrus.Errorf("Error connecting to database: %v", err) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | // Set the schema for the migrations. |
| 73 | migrate.SetSchema("ledgerforge") |
| 74 | |
| 75 | // Apply the migrations. |
| 76 | n, err := migrate.Exec(db, "postgres", migrations, migrate.Up) |
| 77 | if err != nil { |
| 78 | logrus.Errorf("Error migrating up: %v", err) |
| 79 | } else { |
| 80 | logrus.Infof("Applied %d migrations!\n", n) |
| 81 | } |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | return cmd |
| 86 | } |
| 87 | |
| 88 | // migrateDownCommands creates the command for rolling back migrations. |
| 89 | func migrateDownCommands() *cobra.Command { |
no test coverage detected