diff options
Diffstat (limited to 'app/cmd/migrate.go')
-rw-r--r-- | app/cmd/migrate.go | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/app/cmd/migrate.go b/app/cmd/migrate.go deleted file mode 100644 index 95a1dc3..0000000 --- a/app/cmd/migrate.go +++ /dev/null @@ -1,99 +0,0 @@ -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`, -} - -func init() { - migrateCmd.AddCommand( - &cobra.Command{ - Use: "up", - Short: "Migrate up", - Long: `Up migrations`, - RunE: up, - }, - &cobra.Command{ - Use: "down", - Short: "Migrate down", - Long: `Down migrations`, - RunE: down, - }, - &cobra.Command{ - Use: "drop", - Short: "Drop db", - Long: `Deletes everything in the database`, - RunE: drop, - }, - ) -} - -func up(_ *cobra.Command, _ []string) error { - m, err := initMigrate() - if err != nil { - return fmt.Errorf("open migration failed: %w", err) - } - defer m.Close() - if err := m.Up(); err != nil && err != migrate.ErrNoChange { - return fmt.Errorf("do migration failed: %w", err) - } - - return nil -} - -func down(_ *cobra.Command, _ []string) error { - m, err := initMigrate() - if err != nil { - return fmt.Errorf("open migration failed: %w", err) - } - defer m.Close() - if err := m.Down(); err != nil && err != migrate.ErrNoChange { - return fmt.Errorf("do migration failed: %w", err) - } - - return nil -} - -func drop(_ *cobra.Command, _ []string) error { - m, err := initMigrate() - if err != nil { - return fmt.Errorf("open migration failed: %w", err) - } - defer m.Close() - if err := m.Drop(); err != nil && err != migrate.ErrNoChange { - return fmt.Errorf("do migration failed: %w", err) - } - - return nil -} - -func initMigrate() (*migrate.Migrate, error) { - db, err := sql.Open("sqlite3", dbFile) - if err != nil { - return nil, fmt.Errorf("open db failed: %w", err) - } - defer db.Close() - - driver, err := sqlite.WithInstance(db, &sqlite.Config{}) - if err != nil { - return nil, fmt.Errorf("failed create migration driver: %w", err) - } - sourceDriver, err := iofs.New(migrations.FS, ".") - if err != nil { - return nil, fmt.Errorf("failed open migrations: %w", err) - } - - return migrate.NewWithInstance("fs", sourceDriver, "sqlite3", driver) -} |