zitadel/cmd/admin/initialise/verify_database.go
Silvan 4272ea6fe1
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 <livio.a@gmail.com>

* Update cmd/admin/initialise/sql/README.md

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix(init): remove unused index

* user

* fix database username in defaults.yaml

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-02-16 12:30:49 +00:00

52 lines
1.2 KiB
Go

package initialise
import (
"database/sql"
_ "embed"
"fmt"
"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",
Short: "initialize only the database",
Long: `Sets up the ZITADEL database.
Prereqesits:
- cockroachdb
The user provided by flags needs priviledge to
- create the database if it does not exist
- see other users and create a new one if the user does not exist
- grant all rights of the ZITADEL database to the user created if not yet set
`,
RunE: func(cmd *cobra.Command, args []string) error {
config := Config{}
if err := viper.Unmarshal(&config); err != nil {
return err
}
return initialise(config, verifyDatabase(config.Database))
},
}
}
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)),
)
}
}