2022-02-11 13:07:32 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
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"
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
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.
|
|
|
|
|
2024-01-17 10:16:48 +00:00
|
|
|
Prerequisites:
|
2022-09-05 09:24:31 +00:00
|
|
|
- cockroachDB or postgreSQL
|
2022-02-11 13:07:32 +00:00
|
|
|
|
2024-01-17 10:16:48 +00:00
|
|
|
The user provided by flags needs privileges to
|
2022-02-11 13:07:32 +00:00
|
|
|
- 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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func VerifyDatabase(databaseName string) func(*database.DB) error {
|
|
|
|
return func(db *database.DB) error {
|
2022-08-31 07:52:43 +00:00
|
|
|
logging.WithFields("database", databaseName).Info("verify database")
|
|
|
|
|
2024-07-26 20:39:55 +00:00
|
|
|
return exec(db, fmt.Sprintf(databaseStmt, databaseName), []string{dbAlreadyExistsCode})
|
2022-02-11 13:07:32 +00:00
|
|
|
}
|
|
|
|
}
|