2022-02-11 13:07:32 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-02-16 12:30:49 +00:00
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2022-02-11 13:07:32 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-11 13:07:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newDatabase() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "database",
|
|
|
|
Short: "initialize only the database",
|
|
|
|
Long: `Sets up the ZITADEL database.
|
|
|
|
|
|
|
|
Prereqesits:
|
2022-09-05 09:24:31 +00:00
|
|
|
- cockroachDB or postgreSQL
|
2022-02-11 13:07:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
`,
|
2022-03-28 08:05:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-09-07 08:35:12 +00:00
|
|
|
config := MustNewConfig(viper.GetViper())
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
err := initialise(config.Database, VerifyDatabase(config.Database.DatabaseName()))
|
2022-03-28 08:05:09 +00:00
|
|
|
logging.OnError(err).Fatal("unable to initialize the database")
|
2022-02-11 13:07:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
func VerifyDatabase(databaseName string) func(*sql.DB) error {
|
2022-02-16 12:30:49 +00:00
|
|
|
return func(db *sql.DB) error {
|
2022-08-31 07:52:43 +00:00
|
|
|
logging.WithFields("database", databaseName).Info("verify database")
|
|
|
|
|
|
|
|
return exec(db, fmt.Sprintf(string(databaseStmt), databaseName), []string{dbAlreadyExistsCode})
|
2022-02-11 13:07:32 +00:00
|
|
|
}
|
|
|
|
}
|