mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-19 11:45:12 +00:00
fix: init sub commands (#3218)
* fix(init): add sub commands * fix(init): admin user in config, test(init): verify functions * refactor: config, remove second commands * refactor: init steps * chore: fix link in readme * chore: numerate sql files * Update cmd/admin/initialise/sql/README.md Co-authored-by: Livio Amstutz <[email protected]> * Update cmd/admin/initialise/sql/README.md Co-authored-by: Livio Amstutz <[email protected]> * fix(init): remove unused index * user * fix database username in defaults.yaml Co-authored-by: Livio Amstutz <[email protected]>
This commit is contained in:
@@ -2,13 +2,21 @@ package initialise
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/database"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
searchDatabase = "SELECT database_name FROM [show databases] WHERE database_name = $1"
|
||||
|
||||
//go:embed sql/02_database.sql
|
||||
databaseStmt string
|
||||
)
|
||||
|
||||
func newDatabase() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "database",
|
||||
@@ -24,31 +32,20 @@ The user provided by flags needs priviledge to
|
||||
- grant all rights of the ZITADEL database to the user created if not yet set
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
config := new(Config)
|
||||
if err := viper.Unmarshal(config); err != nil {
|
||||
config := Config{}
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return initialise(config, verifyDB)
|
||||
return initialise(config, verifyDatabase(config.Database))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func verifyDB(db *sql.DB, config database.Config) error {
|
||||
logging.Info("verify database")
|
||||
exists, err := existsDatabase(db, config)
|
||||
if exists || err != nil {
|
||||
return err
|
||||
func verifyDatabase(config database.Config) func(*sql.DB) error {
|
||||
return func(db *sql.DB) error {
|
||||
return verify(db,
|
||||
exists(searchDatabase, config.Database),
|
||||
exec(fmt.Sprintf(databaseStmt, config.Database)),
|
||||
)
|
||||
}
|
||||
return createDatabase(db, config)
|
||||
}
|
||||
|
||||
func existsDatabase(db *sql.DB, config database.Config) (exists bool, err error) {
|
||||
row := db.QueryRow("SELECT EXISTS(SELECT database_name FROM [show databases] WHERE database_name = $1)", config.Database)
|
||||
err = row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func createDatabase(db *sql.DB, config database.Config) error {
|
||||
_, err := db.Exec("CREATE DATABASE " + config.Database)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user