feat: Added migration tool
This commit is contained in:
45
migration/main.go
Normal file
45
migration/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"text/template"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/db"
|
||||
appLog "git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
|
||||
const migrationDir = "migrations"
|
||||
|
||||
var sqlMigrationTemplate = template.Must(template.New("goose.sql-migration").Parse(`-- +goose Up
|
||||
|
||||
-- +goose Down
|
||||
`))
|
||||
|
||||
func init() {
|
||||
goose.SetSequential(true)
|
||||
goose.SetLogger(appLog.Global.Get(appLog.DATABASE))
|
||||
}
|
||||
|
||||
func CreateMigration(name string) error {
|
||||
return goose.CreateWithTemplate(nil, migrationDir, sqlMigrationTemplate, name, "sql")
|
||||
}
|
||||
|
||||
func RunMigration() {
|
||||
connect, err := db.Connect()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dbConn, err := connect.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = goose.SetDialect("postgres"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = goose.Up(dbConn, migrationDir); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user