-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
498 additions
and
679 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
.buildid* | ||
.gradle | ||
build | ||
go.sum | ||
updb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.0-0 | ||
2.0.0-0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.