Skip to content

Commit 2a5694a

Browse files
committed
V2
1 parent 53b81eb commit 2a5694a

30 files changed

+498
-679
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.buildid*
44
.gradle
55
build
6+
go.sum
7+
updb

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
default:
2+
go build github.com/upsilonproject/upsilon-database-sql/cmd/updb

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0-0
1+
2.0.0-0

cmd/updb/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main;
2+
3+
import (
4+
"fmt"
5+
"github.com/golang-migrate/migrate/v4"
6+
_ "github.com/golang-migrate/migrate/v4/database/mysql"
7+
_ "github.com/golang-migrate/migrate/v4/source/file"
8+
. "github.com/upsilonproject/upsilon-database-sql/internal/config"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var (
13+
rootCmd = &cobra.Command {
14+
Use: "updb",
15+
}
16+
)
17+
18+
func initFlags() {
19+
rootCmd.PersistentFlags().StringP("dbHost", "", "Database Host", "The hostname of the database to connect to")
20+
rootCmd.PersistentFlags().IntP("force", "", 0, "Force version")
21+
}
22+
23+
24+
func main() {
25+
initFlags();
26+
InitConfig(rootCmd);
27+
28+
rootCmd.Execute()
29+
30+
conf := GetConfig();
31+
32+
mysqlUri := fmt.Sprintf("mysql://%v:%v@tcp(%v)/%v", conf.Database.User, conf.Database.Pass, conf.Database.Host, conf.Database.Name)
33+
34+
fmt.Printf("conf: %+v \n\n", conf);
35+
fmt.Printf("db: %v \n\n", mysqlUri)
36+
37+
38+
m, err := migrate.New(
39+
"file://./mysql/migrations",
40+
mysqlUri,
41+
)
42+
43+
if err != nil {
44+
fmt.Println("init errors:", err);
45+
} else {
46+
ver, dirty, err := m.Version()
47+
48+
fmt.Println("version before migration:", ver);
49+
50+
if (dirty) {
51+
fmt.Println("Dirty DB");
52+
} else {
53+
err = m.Up();
54+
fmt.Println("upgrade result:", err);
55+
}
56+
}
57+
}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/upsilonproject/upsilon-database-sql
2+
3+
go 1.13
4+
5+
require (
6+
github.com/golang-migrate/migrate/v4 v4.7.1
7+
github.com/spf13/cobra v0.0.5
8+
github.com/spf13/viper v1.6.1
9+
)

internal/config/RuntimeConfig.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package config;
2+
3+
import (
4+
"strings"
5+
"fmt"
6+
"github.com/spf13/viper"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type RuntimeConfig struct {
11+
Database DatabaseConfig;
12+
IsLoaded bool;
13+
Force int;
14+
}
15+
16+
type DatabaseConfig struct {
17+
User string;
18+
Pass string;
19+
Host string;
20+
Name string;
21+
}
22+
23+
var (
24+
conf RuntimeConfig;
25+
)
26+
27+
func InitConfig(rootCmd *cobra.Command) {
28+
viper.SetEnvPrefix("UP")
29+
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"));
30+
viper.SetDefault("database.host", "upsilon")
31+
viper.SetDefault("database.user", "upsilon")
32+
viper.SetDefault("database.pass", "upsilon")
33+
viper.SetDefault("database.name", "upsilon")
34+
viper.AutomaticEnv();
35+
36+
//viper.BindPFlag("force", rootCmd.Flags().Lookup("force"));
37+
38+
if !conf.IsLoaded {
39+
conf.IsLoaded = true;
40+
41+
if err := viper.Unmarshal(&conf); err != nil {
42+
fmt.Println("Cannot unmarshal config: ", err)
43+
}
44+
}
45+
}
46+
47+
func GetConfig() RuntimeConfig {
48+
return conf;
49+
}

mysql/create-database

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/bash
22

3-
echo "username: ${CFG_DB_USER}"
4-
echo "password: ${CFG_DB_PASS}"
3+
echo "username: ${UP_DATABASE_USER}"
4+
echo "password: ${UP_DATABASE_PASS}"
55

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

0 commit comments

Comments
 (0)