Migration Automation

- Adding docker DB template,
- migrationscript,
- config update,
. adding migration to bee rs
This commit is contained in:
Lukas Bachschwell 2018-11-14 18:47:42 +01:00
parent ec3af0cad6
commit e8452e0d77
5 changed files with 40 additions and 16 deletions

View File

@ -3,7 +3,9 @@
"go_install": false, "go_install": false,
"scripts": { "scripts": {
"test": "go test -v tests/*.go", "test": "go test -v tests/*.go",
"convey": "goconvey tests/" "convey": "goconvey tests/",
"migrate:system": "go run scripts/migrateTenants.go system",
"migrate:tenants": "go run scripts/migrateTenants.go"
}, },
"cmd_args": [], "cmd_args": [],
"envs": [], "envs": [],

View File

@ -1,6 +1,6 @@
Host = "127.0.0.1" Host = "127.0.0.1"
User = "postgres" User = "postgres"
Password = "postgres" Password = "ENvfffM4hrJAKrs8"
Port = 5435 Port = 5435
Db = "system" Db = "system"
Ssl = "disable" Ssl = "disable"

1
docker/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp

View File

@ -5,7 +5,7 @@ services:
volumes: volumes:
- ./tmp/db:/var/lib/postgresql/data - ./tmp/db:/var/lib/postgresql/data
environment: environment:
POSTGRES_DATABASE: "system" POSTGRES_DB: "system"
POSTGRES_USER: "postgres" POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "ENvfffM4hrJAKrs8" POSTGRES_PASSWORD: "ENvfffM4hrJAKrs8"
ports: ports:

View File

@ -13,9 +13,17 @@ import (
var conf companyDB.DBConfig var conf companyDB.DBConfig
func main() { func main() {
migrateSystem := false
migType := "" migType := ""
if len(os.Args) > 1 { if len(os.Args) > 1 {
migType = os.Args[1] if os.Args[1] == "system" {
migrateSystem = true
if len(os.Args) > 2 {
migType = os.Args[2]
}
} else {
migType = os.Args[1]
}
} }
companyDB.InitCompanyDBService() companyDB.InitCompanyDBService()
@ -28,6 +36,12 @@ func main() {
panic(err.Error()) panic(err.Error())
} }
if migrateSystem {
fmt.Printf("SYSTEM DB: migrate %s\n", migType)
migrateDbName("system", migType)
return
}
fmt.Printf("migrate %s\n", migType) fmt.Printf("migrate %s\n", migType)
for result.Next() { for result.Next() {
@ -58,21 +72,28 @@ func printOutput(outs []byte) {
} }
func migrateDbName(dbName, migType string) { func migrateDbName(dbName, migType string) {
connString := fmt.Sprintf("--conn=\"postgres://%s:%s@%s:%d/%s?sslmode=%s\"", conf.User, conf.Password, conf.Host, conf.Port, dbName, conf.Ssl) dir := ""
fmt.Println(connString) if dbName == "system" {
dir = "--dir=database/systemmigrations"
}
connString := fmt.Sprintf("--conn=postgres://%s:%s@%s:%d/%s?sslmode=%s", conf.User, conf.Password, conf.Host, conf.Port, dbName, conf.Ssl)
cmdString := fmt.Sprintf("%s%s", os.Getenv("GOPATH"), "/bin/bee") cmdString := fmt.Sprintf("%s%s", os.Getenv("GOPATH"), "/bin/bee")
cmd := exec.Command(cmdString, "migrate", migType, connString)
var cmd *exec.Cmd
if migType != "" {
cmd = exec.Command(cmdString, "migrate", migType, connString, dir)
} else {
cmd = exec.Command(cmdString, "migrate", connString, dir)
}
// Combine stdout and stderr // Combine stdout and stderr
printCommand(cmd) printCommand(cmd)
_, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
printError(err) if err != nil {
// printOutput(output) printError(err)
printOutput(output)
// check what migration type } else {
// query system table fmt.Println("Success")
// //run all or single tenant, check if tenant exists }
// execute commands (blocking!)
// print out all states in new lines with green or red and error
} }