aboutsummaryrefslogtreecommitdiff
path: root/app/cmd/migrate.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/cmd/migrate.go')
-rw-r--r--app/cmd/migrate.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/app/cmd/migrate.go b/app/cmd/migrate.go
new file mode 100644
index 0000000..3c4fd05
--- /dev/null
+++ b/app/cmd/migrate.go
@@ -0,0 +1,51 @@
+package cmd
+
+import (
+ "database/sql"
+ "fmt"
+
+ "github.com/golang-migrate/migrate/v4"
+ "github.com/golang-migrate/migrate/v4/database/sqlite"
+ "github.com/golang-migrate/migrate/v4/source/iofs"
+ "github.com/spf13/cobra"
+ "gitrepo.ru/neonxp/gorum/migrations"
+)
+
+var migrateCmd = &cobra.Command{
+ Use: "migrate",
+ Short: "Migrate db",
+ Long: `Up and down migrations`,
+}
+
+var migrateUp = &cobra.Command{
+ Use: "up",
+ Short: "Migrate up",
+ Long: `Up migrations`,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ db, err := sql.Open("sqlite3", dbFile)
+ if err != nil {
+ return fmt.Errorf("open db failed: %w", err)
+ }
+ defer db.Close()
+
+ driver, err := sqlite.WithInstance(db, &sqlite.Config{})
+ if err != nil {
+ return fmt.Errorf("failed create migration driver: %w", err)
+ }
+ sourceDriver, err := iofs.New(migrations.FS, ".")
+ if err != nil {
+ return fmt.Errorf("failed open migrations: %w", err)
+ }
+
+ m, err := migrate.NewWithInstance("fs", sourceDriver, "sqlite3", driver)
+ if err != nil {
+ return fmt.Errorf("open migration failed: %w", err)
+ }
+
+ if err := m.Up(); err != nil && err != migrate.ErrNoChange {
+ return fmt.Errorf("do migration failed: %w", err)
+ }
+
+ return nil
+ },
+}