From 4a0fb795cec98a4dd2d51e501cf172a37486f22e Mon Sep 17 00:00:00 2001 From: Carryall Date: Fri, 29 Sep 2023 17:58:58 +0700 Subject: [PATCH 1/4] [109] Check if migration file exist before running migration --- {{cookiecutter.app_name}}/Makefile | 8 +++++-- .../database/database.go | 21 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/{{cookiecutter.app_name}}/Makefile b/{{cookiecutter.app_name}}/Makefile index d5ccfe0..3027f94 100644 --- a/{{cookiecutter.app_name}}/Makefile +++ b/{{cookiecutter.app_name}}/Makefile @@ -13,7 +13,11 @@ env-teardown: db/migrate: make wait-for-postgres - goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" up +ifneq ("$(wildcard $(database/migrations/.keep))","") + goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" up +else + $(info NO migration files to run) +endif db/rollback: make wait-for-postgres @@ -39,7 +43,7 @@ dev: install-dependencies: go install github.com/cosmtrek/air@v1.43.0 - go install github.com/pressly/goose/v3/cmd/goose@v3.9.0 + go install github.com/pressly/goose/v3/cmd/goose@v3.15.0 go install github.com/ddollar/forego@latest go mod tidy npm install diff --git a/{{cookiecutter.app_name}}/database/database.go b/{{cookiecutter.app_name}}/database/database.go index 6fc606f..200882c 100644 --- a/{{cookiecutter.app_name}}/database/database.go +++ b/{{cookiecutter.app_name}}/database/database.go @@ -19,6 +19,8 @@ import ( var database *gorm.DB +const databaseDir = "database/migrations" + func InitDatabase(databaseURL string) { db, err := gorm.Open(postgres.Open(databaseURL), &gorm.Config{}) if err != nil { @@ -38,12 +40,25 @@ func migrateDB(db *gorm.DB) { log.Fatalf("Failed to convert gormDB to sqlDB: %v", err) } - err = goose.Up(sqlDB, "database/migrations", goose.WithAllowMissing()) + if migrationFileExist() { + err = goose.Up(sqlDB, databaseDir, goose.WithAllowMissing()) + if err != nil { + log.Fatalf("Failed to migrate database: %v", err) + } + + log.Println("Migrated database successfully.") + } else { + log.Println("NO migration files") + } +} + +func migrationFileExist() bool { + files, err := os.ReadDir(databaseDir) if err != nil { - log.Fatalf("Failed to migrate database: %v", err) + log.Fatalf("Missing migration directory: %v", err) } - log.Println("Migrated database successfully.") + return len(files) > 0 && files[0].Name() != ".keep" } func GetDB() *gorm.DB { From 4611bd3fe9ffe22cf0609722210792e03f018e73 Mon Sep 17 00:00:00 2001 From: Carryall Date: Mon, 23 Oct 2023 15:59:07 +0700 Subject: [PATCH 2/4] [109] Improve prompt --- {{cookiecutter.app_name}}/database/database.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.app_name}}/database/database.go b/{{cookiecutter.app_name}}/database/database.go index 200882c..da34308 100644 --- a/{{cookiecutter.app_name}}/database/database.go +++ b/{{cookiecutter.app_name}}/database/database.go @@ -43,10 +43,10 @@ func migrateDB(db *gorm.DB) { if migrationFileExist() { err = goose.Up(sqlDB, databaseDir, goose.WithAllowMissing()) if err != nil { - log.Fatalf("Failed to migrate database: %v", err) + log.Fatalf("Failed to migrate the database: %v", err) } - log.Println("Migrated database successfully.") + log.Println("Database migrated successfully.") } else { log.Println("NO migration files") } From 2cf02a4c666d688db7f006fee77e624b4267babd Mon Sep 17 00:00:00 2001 From: Carryall Date: Mon, 23 Oct 2023 16:35:25 +0700 Subject: [PATCH 3/4] [109] Add missing imports --- {{cookiecutter.app_name}}/database/database.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/{{cookiecutter.app_name}}/database/database.go b/{{cookiecutter.app_name}}/database/database.go index da34308..a1cbcd6 100644 --- a/{{cookiecutter.app_name}}/database/database.go +++ b/{{cookiecutter.app_name}}/database/database.go @@ -2,6 +2,7 @@ package database import ( "fmt" + "os" {% if cookiecutter.use_logrus == "no" %}"log" {% endif %} @@ -13,6 +14,7 @@ import ( "github.com/pressly/goose/v3" "github.com/spf13/viper" "golang.org/x/text/cases" + "golang.org/x/text/language" "gorm.io/driver/postgres" "gorm.io/gorm" ) From 1183eb40b3a3335075de908430f774a89466ea56 Mon Sep 17 00:00:00 2001 From: Carryall Date: Mon, 23 Oct 2023 16:35:38 +0700 Subject: [PATCH 4/4] [109] Change condition --- {{cookiecutter.app_name}}/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/{{cookiecutter.app_name}}/Makefile b/{{cookiecutter.app_name}}/Makefile index 3027f94..727bb47 100644 --- a/{{cookiecutter.app_name}}/Makefile +++ b/{{cookiecutter.app_name}}/Makefile @@ -13,10 +13,10 @@ env-teardown: db/migrate: make wait-for-postgres -ifneq ("$(wildcard $(database/migrations/.keep))","") - goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" up +ifeq ("$(wildcard $(database/migrations/.keep))","") + $(info NO migration files to run) else - $(info NO migration files to run) + goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" up endif db/rollback: