Migration Automation
- Adding docker DB template, - migrationscript, - config update, . adding migration to bee rs
This commit is contained in:
		
							
								
								
									
										4
									
								
								bee.json
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								bee.json
									
									
									
									
									
								
							@@ -3,7 +3,9 @@
 | 
			
		||||
  "go_install": false,
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "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": [],
 | 
			
		||||
  "envs": [],
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
Host = "127.0.0.1"
 | 
			
		||||
User = "postgres"
 | 
			
		||||
Password = "postgres"
 | 
			
		||||
Password = "ENvfffM4hrJAKrs8"
 | 
			
		||||
Port = 5435
 | 
			
		||||
Db   = "system"
 | 
			
		||||
Ssl  = "disable"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								docker/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								docker/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
tmp
 | 
			
		||||
@@ -5,7 +5,7 @@ services:
 | 
			
		||||
    volumes:
 | 
			
		||||
      - ./tmp/db:/var/lib/postgresql/data
 | 
			
		||||
    environment:
 | 
			
		||||
      POSTGRES_DATABASE: "system"
 | 
			
		||||
      POSTGRES_DB: "system"
 | 
			
		||||
      POSTGRES_USER: "postgres"
 | 
			
		||||
      POSTGRES_PASSWORD: "ENvfffM4hrJAKrs8"
 | 
			
		||||
    ports:
 | 
			
		||||
 
 | 
			
		||||
@@ -13,10 +13,18 @@ import (
 | 
			
		||||
var conf companyDB.DBConfig
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	migrateSystem := false
 | 
			
		||||
	migType := ""
 | 
			
		||||
	if len(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()
 | 
			
		||||
	conf = companyDB.Conf
 | 
			
		||||
@@ -28,6 +36,12 @@ func main() {
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if migrateSystem {
 | 
			
		||||
		fmt.Printf("SYSTEM DB: migrate %s\n", migType)
 | 
			
		||||
		migrateDbName("system", migType)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("migrate %s\n", migType)
 | 
			
		||||
 | 
			
		||||
	for result.Next() {
 | 
			
		||||
@@ -58,21 +72,28 @@ func printOutput(outs []byte) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
	fmt.Println(connString)
 | 
			
		||||
	dir := ""
 | 
			
		||||
	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")
 | 
			
		||||
	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
 | 
			
		||||
	printCommand(cmd)
 | 
			
		||||
	_, err := cmd.CombinedOutput()
 | 
			
		||||
	output, err := cmd.CombinedOutput()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		printError(err)
 | 
			
		||||
	// printOutput(output)
 | 
			
		||||
 | 
			
		||||
	// check what migration type
 | 
			
		||||
	// query system table
 | 
			
		||||
	// //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
 | 
			
		||||
		printOutput(output)
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("Success")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user