Skip to content

Commit

Permalink
V2
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Jan 4, 2020
1 parent 53b81eb commit 2a5694a
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 679 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.buildid*
.gradle
build
go.sum
updb
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default:
go build github.com/upsilonproject/upsilon-database-sql/cmd/updb
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-0
2.0.0-0
57 changes: 57 additions & 0 deletions cmd/updb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main;

import (
"fmt"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
. "github.com/upsilonproject/upsilon-database-sql/internal/config"
"github.com/spf13/cobra"
)

var (
rootCmd = &cobra.Command {
Use: "updb",
}
)

func initFlags() {
rootCmd.PersistentFlags().StringP("dbHost", "", "Database Host", "The hostname of the database to connect to")
rootCmd.PersistentFlags().IntP("force", "", 0, "Force version")
}


func main() {
initFlags();
InitConfig(rootCmd);

rootCmd.Execute()

conf := GetConfig();

mysqlUri := fmt.Sprintf("mysql://%v:%v@tcp(%v)/%v", conf.Database.User, conf.Database.Pass, conf.Database.Host, conf.Database.Name)

fmt.Printf("conf: %+v \n\n", conf);
fmt.Printf("db: %v \n\n", mysqlUri)


m, err := migrate.New(
"file://./mysql/migrations",
mysqlUri,
)

if err != nil {
fmt.Println("init errors:", err);
} else {
ver, dirty, err := m.Version()

fmt.Println("version before migration:", ver);

if (dirty) {
fmt.Println("Dirty DB");
} else {
err = m.Up();
fmt.Println("upgrade result:", err);
}
}
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/upsilonproject/upsilon-database-sql

go 1.13

require (
github.com/golang-migrate/migrate/v4 v4.7.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1
)
49 changes: 49 additions & 0 deletions internal/config/RuntimeConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config;

import (
"strings"
"fmt"
"github.com/spf13/viper"
"github.com/spf13/cobra"
)

type RuntimeConfig struct {
Database DatabaseConfig;
IsLoaded bool;
Force int;
}

type DatabaseConfig struct {
User string;
Pass string;
Host string;
Name string;
}

var (
conf RuntimeConfig;
)

func InitConfig(rootCmd *cobra.Command) {
viper.SetEnvPrefix("UP")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"));
viper.SetDefault("database.host", "upsilon")
viper.SetDefault("database.user", "upsilon")
viper.SetDefault("database.pass", "upsilon")
viper.SetDefault("database.name", "upsilon")
viper.AutomaticEnv();

//viper.BindPFlag("force", rootCmd.Flags().Lookup("force"));

if !conf.IsLoaded {
conf.IsLoaded = true;

if err := viper.Unmarshal(&conf); err != nil {
fmt.Println("Cannot unmarshal config: ", err)
}
}
}

func GetConfig() RuntimeConfig {
return conf;
}
8 changes: 4 additions & 4 deletions mysql/create-database
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/bash

echo "username: ${CFG_DB_USER}"
echo "password: ${CFG_DB_PASS}"
echo "username: ${UP_DATABASE_USER}"
echo "password: ${UP_DATABASE_PASS}"

mysql -h ${CFG_DB_HOST} -u ${CFG_DB_USER} -p${CFG_DB_PASS} upsilon < sql/schema.sql
mysql -h ${CFG_DB_HOST} -u ${CFG_DB_USER} -p${CFG_DB_PASS} upsilon < sql/initialData.sql
mysql -h ${UP_DATABASE_HOST} -u ${UP_DATABASE_USER} -p${UP_DATABASE_PASS} upsilon < sql/schema.sql
mysql -h ${UP_DATABASE_HOST} -u ${UP_DATABASE_USER} -p${UP_DATABASE_PASS} upsilon < sql/initialData.sql

Loading

0 comments on commit 2a5694a

Please sign in to comment.