MCPcopy
hub / github.com/writefreely/writefreely / adminInitDatabase

Function adminInitDatabase

app.go:958–1002  ·  view source on GitHub ↗
(app *App)

Source from the content-addressed store, hash-verified

956var sqliteSql string
957
958func adminInitDatabase(app *App) error {
959 var schema string
960 if app.cfg.Database.Type == driverSQLite {
961 schema = sqliteSql
962 } else {
963 schema = schemaSql
964 }
965
966 tblReg := regexp.MustCompile("CREATE TABLE (IF NOT EXISTS )?`([a-z_]+)`")
967
968 queries := strings.Split(string(schema), ";\n")
969 for _, q := range queries {
970 if strings.TrimSpace(q) == "" {
971 continue
972 }
973 parts := tblReg.FindStringSubmatch(q)
974 if len(parts) >= 3 {
975 log.Info("Creating table %s...", parts[2])
976 } else {
977 log.Info("Creating table ??? (Weird query) No match in: %v", parts)
978 }
979 _, err := app.db.Exec(q)
980 if err != nil {
981 log.Error("%s", err)
982 } else {
983 log.Info("Created.")
984 }
985 }
986
987 // Set up migrations table
988 log.Info("Initializing appmigrations table...")
989 err := migrations.SetInitialMigrations(migrations.NewDatastore(app.db.DB, app.db.driverName))
990 if err != nil {
991 return fmt.Errorf("Unable to set initial migrations: %v", err)
992 }
993
994 log.Info("Running migrations...")
995 err = migrations.Migrate(migrations.NewDatastore(app.db.DB, app.db.driverName))
996 if err != nil {
997 return fmt.Errorf("migrate: %s", err)
998 }
999
1000 log.Info("Done.")
1001 return nil
1002}
1003
1004// ServerUserAgent returns a User-Agent string to use in external requests. The
1005// hostName parameter may be left empty.

Callers 2

DoConfigFunction · 0.85
CreateSchemaFunction · 0.85

Calls 3

SetInitialMigrationsFunction · 0.92
NewDatastoreFunction · 0.92
MigrateFunction · 0.92

Tested by

no test coverage detected