Skip to content

Commit

Permalink
feat: generate database migrate and schema example
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidayat Hamir committed Feb 7, 2024
1 parent cf6e237 commit 35d7ed6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ go install github.com/spacetronot-research-team/erago@latest
or you can define your prefered version.

```shell
go install github.com/spacetronot-research-team/erago@v0.0.17
go install github.com/spacetronot-research-team/erago@v0.0.18
```

Or you can download erago binary from [release page](https://github.com/spacetronot-research-team/erago/releases).
Expand Down
51 changes: 51 additions & 0 deletions cmd/createproject/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ func CreateProject(projectName string, moduleName string) {
logrus.Fatal(fmt.Errorf("err write file projectPath/database/open.go: %v", err))
}

logrus.Info("create migrate dir in database dir")
if err := os.MkdirAll(filepath.Join(projectPath, "database", "migrate"), os.ModePerm); err != nil {
logrus.Fatal(fmt.Errorf("err mkdir projectPath/database/migrate: %v", err))
}

logrus.Info("create up.go file in migrate dir")
if err := generateDatabaseMigrateUpTemplate(projectPath, moduleName); err != nil {
logrus.Fatal(fmt.Errorf("err write file projectPath/database/migrate/up: %v", err))
}

logrus.Info("create schema_migration dir in database dir")
if err := os.MkdirAll(filepath.Join(projectPath, "database", "schema_migration"), os.ModePerm); err != nil {
logrus.Fatal(fmt.Errorf("err mkdir projectPath/database/schema_migration: %v", err))
}

if err := generateSchemaMigrationExample(projectPath); err != nil {
logrus.Fatal(fmt.Errorf("err generate schema migration example: %v", err))
}

logrus.Info("create cmd dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "cmd"), os.ModePerm); err != nil {
logrus.Fatal(fmt.Errorf("err mkdir projectPath/cmd: %v", err))
Expand All @@ -83,6 +102,24 @@ func CreateProject(projectName string, moduleName string) {
logrus.Info("create project finish, go to your project:\n\tcd ", projectPath)
}

func generateSchemaMigrationExample(projectPath string) error {
schemaExampleTemplate := `-- +migrate Up
SELECT
*
from
erago;
-- +migrate Down`

path := filepath.Join(projectPath, "database", "schema_migration", "20240114192700-example.sql")

if err := os.WriteFile(path, []byte(schemaExampleTemplate), os.ModePerm); err != nil {
return fmt.Errorf("err write database schema migration example: %v", err)
}

return nil
}

func runGoModInit(moduleName string, projectPath string) error {
cmd := exec.Command("go", "mod", "init", moduleName)
cmd.Dir = projectPath
Expand Down Expand Up @@ -122,3 +159,17 @@ func generateMainTemplate(projectPath string, moduleName string) error {

return nil
}

func generateDatabaseMigrateUpTemplate(projectPath string, moduleName string) error {
databaseMigrateUpTemplate, err := template.GetDatabaseMigrateUpTemplate(moduleName)
if err != nil {
return fmt.Errorf("err get databaseMigrateUp template: %v", err)
}

path := filepath.Join(projectPath, "database", "migrate", "up.go")
if err := os.WriteFile(path, []byte(databaseMigrateUpTemplate), os.ModePerm); err != nil {
return fmt.Errorf("err write database migrate up template: %v", err)
}

return nil
}
81 changes: 81 additions & 0 deletions cmd/createproject/template/database_migrate_up.go
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
}
`
2 changes: 1 addition & 1 deletion cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package version
import "fmt"

func Version() {
fmt.Println("v0.0.17")
fmt.Println("v0.0.18")
}

0 comments on commit 35d7ed6

Please sign in to comment.