-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate database migrate and schema example
- Loading branch information
Hidayat Hamir
committed
Feb 7, 2024
1 parent
cf6e237
commit 35d7ed6
Showing
4 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package template | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
func GetDatabaseMigrateUpTemplate(moduleName string) (string, error) { | ||
databaseMigrateUpConfig := DatabaseMigrateUpConfig{} | ||
databaseMigrateUpConfig.ModuleName = moduleName | ||
|
||
databaseMigrateUpTemplate, err := template.New("databaseMigrateUpTemplate").Parse(databaseMigrateUpTemplate) | ||
if err != nil { | ||
return "", fmt.Errorf("err parse template databaseMigrateUpTemplate: %v", err) | ||
} | ||
|
||
var templateBuf bytes.Buffer | ||
if err = databaseMigrateUpTemplate.Execute(&templateBuf, databaseMigrateUpConfig); err != nil { | ||
return "", fmt.Errorf("err create template: %v", err) | ||
} | ||
return templateBuf.String(), nil | ||
} | ||
|
||
type DatabaseMigrateUpConfig struct { | ||
ModuleName string | ||
} | ||
|
||
var databaseMigrateUpTemplate = `package main | ||
import ( | ||
"fmt" | ||
"log" | ||
"path/filepath" | ||
"github.com/joho/godotenv" | ||
migrate "github.com/rubenv/sql-migrate" | ||
"{{.ModuleName}}/database" | ||
"gorm.io/gorm" | ||
) | ||
func main() { | ||
if err := godotenv.Load(".env"); err != nil { | ||
log.Fatal(err) | ||
} | ||
db, err := database.InitializeDB() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := migrationUp(db); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Println("migrate up success") | ||
} | ||
func migrationUp(db *gorm.DB) error { | ||
migrate.SetTable("migrations") | ||
sql, err := db.DB() | ||
if err != nil { | ||
return fmt.Errorf("'*gorm.DB' fail return '*sql.DB': %v", err) | ||
} | ||
_, err = migrate.Exec(sql, "postgres", getFileMigrationSource(), migrate.Up) | ||
if err != nil { | ||
return fmt.Errorf("fail execute migrations: %v", err) | ||
} | ||
return nil | ||
} | ||
func getFileMigrationSource() *migrate.FileMigrationSource { | ||
migrations := &migrate.FileMigrationSource{ | ||
Dir: filepath.Join("database", "schema_migration"), | ||
} | ||
return migrations | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ package version | |
import "fmt" | ||
|
||
func Version() { | ||
fmt.Println("v0.0.17") | ||
fmt.Println("v0.0.18") | ||
} |