Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update migrate.go #1059

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions database/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
package migrate

import (
"database/sql"
"embed"
"os"
"strconv"
"database/sql"
"embed"
"log"
"os"
"strconv"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3"
)

//go:embed migrations/*.sql
// embedMigrations includes all SQL migration files.
var embedMigrations embed.FS

// MigrationsDir migration dir
const MigrationsDir string = "migrations"
// MigrationsDir is the directory for database migration files.
const MigrationsDir = "migrations"

func init() {
goose.SetBaseFS(embedMigrations)
goose.SetSequential(true)
goose.SetTableName("scroll_migrations")
goose.SetBaseFS(embedMigrations)
goose.SetSequential(true)
goose.SetTableName("scroll_migrations")

verbose, _ := strconv.ParseBool(os.Getenv("LOG_SQL_MIGRATIONS"))
goose.SetVerbose(verbose)
verbose, err := strconv.ParseBool(os.Getenv("LOG_SQL_MIGRATIONS"))
if err != nil {
log.Printf("Warning: Failed to parse LOG_SQL_MIGRATIONS: %v", err)
}
goose.SetVerbose(verbose)
}

// Migrate migrate db
// Migrate applies database migrations.
func Migrate(db *sql.DB) error {
//return goose.Up(db, MIGRATIONS_DIR, goose.WithAllowMissing())
return goose.Up(db, MigrationsDir, goose.WithAllowMissing())
return goose.Up(db, MigrationsDir, goose.WithAllowMissing())
}

// Rollback rollback to the given version
// Rollback reverts the database to the given version.
func Rollback(db *sql.DB, version *int64) error {
if version != nil {
return goose.DownTo(db, MigrationsDir, *version)
}
return goose.Down(db, MigrationsDir)
if version != nil {
return goose.DownTo(db, MigrationsDir, *version)
}
return goose.Down(db, MigrationsDir)
}

// ResetDB clean and migrate db.
// ResetDB cleans and migrates the database.
func ResetDB(db *sql.DB) error {
if err := Rollback(db, new(int64)); err != nil {
return err
}
return Migrate(db)
if err := Rollback(db, new(int64)); err != nil {
return err
}
return Migrate(db)
}

// Current get current version
// Current returns the current database version.
func Current(db *sql.DB) (int64, error) {
return goose.GetDBVersion(db)
return goose.GetDBVersion(db)
}

// Status is normal or not
// Status checks if the database is up-to-date with migrations.
func Status(db *sql.DB) error {
return goose.Version(db, MigrationsDir)
return goose.Version(db, MigrationsDir)
}

// Create a new migration folder
// Create generates a new migration file.
func Create(db *sql.DB, name, migrationType string) error {
return goose.Create(db, MigrationsDir, name, migrationType)
return goose.Create(db, MigrationsDir, name, migrationType)
}
Loading