diff --git a/cmd/configmap-server/main.go b/cmd/configmap-server/main.go index ca163a99bb..fa471fc0ef 100644 --- a/cmd/configmap-server/main.go +++ b/cmd/configmap-server/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "database/sql" "fmt" "net" @@ -98,11 +99,19 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { logger.Fatalf("error getting configmap: %s", err) } - sqlLoader, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(dbName)) + db, err := sql.Open("sqlite3", dbName) if err != nil { return err } + sqlLoader, err := sqlite.NewSQLLiteLoader(db) + if err != nil { + return err + } + if err := sqlLoader.Migrate(context.TODO()); err != nil { + return err + } + configMapPopulator := sqlite.NewSQLLoaderForConfigMap(sqlLoader, *configMap) if err := configMapPopulator.Populate(); err != nil { err = fmt.Errorf("error loading manifests from configmap: %s", err) diff --git a/cmd/initializer/main.go b/cmd/initializer/main.go index 5fdbe45c1b..4666b6dd2f 100644 --- a/cmd/initializer/main.go +++ b/cmd/initializer/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + "database/sql" "fmt" "github.com/sirupsen/logrus" @@ -53,11 +55,19 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { return err } - dbLoader, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(outFilename)) + db, err := sql.Open("sqlite3", outFilename) if err != nil { return err } - defer dbLoader.Close() + defer db.Close() + + dbLoader, err := sqlite.NewSQLLiteLoader(db) + if err != nil { + return err + } + if err := dbLoader.Migrate(context.TODO()); err != nil { + return err + } loader := sqlite.NewSQLLoaderForDirectory(dbLoader, manifestDir) if err := loader.Populate(); err != nil { diff --git a/cmd/opm/registry/serve.go b/cmd/opm/registry/serve.go index 8b30ec5b2e..2a020a351c 100644 --- a/cmd/opm/registry/serve.go +++ b/cmd/opm/registry/serve.go @@ -2,6 +2,8 @@ package registry import ( "context" + "database/sql" + "fmt" "net" "github.com/sirupsen/logrus" @@ -12,9 +14,9 @@ import ( "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" "github.com/operator-framework/operator-registry/pkg/lib/log" - "github.com/operator-framework/operator-registry/pkg/registry" "github.com/operator-framework/operator-registry/pkg/server" "github.com/operator-framework/operator-registry/pkg/sqlite" + "github.com/operator-framework/operator-registry/pkg/sqlite/migrations" ) func newRegistryServeCmd() *cobra.Command { @@ -37,7 +39,7 @@ func newRegistryServeCmd() *cobra.Command { rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to sqlite db") rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on") rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file") - + rootCmd.Flags().Bool("skip-migrate", false, "do not attempt to migrate to the latest db revision when starting") return rootCmd } @@ -64,15 +66,18 @@ func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error { logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port}) - var store registry.Query - store, err = sqlite.NewSQLLiteQuerier(dbName) + db, err := sql.Open("sqlite3", dbName) if err != nil { - logger.WithError(err).Warnf("failed to load db") + return err } - if store == nil { - store = registry.NewEmptyQuerier() + + // migrate to the latest version + if err := migrate(cmd, db); err != nil { + logger.WithError(err).Warnf("couldn't migrate db") } + store := sqlite.NewSQLLiteQuerierFromDb(db) + // sanity check that the db is available tables, err := store.ListTables(context.TODO()) if err != nil { @@ -98,3 +103,23 @@ func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error { return nil } + +func migrate(cmd *cobra.Command, db *sql.DB) error { + shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate") + if err != nil { + return err + } + if shouldSkipMigrate { + return nil + } + + migrator, err := sqlite.NewSQLLiteMigrator(db) + if err != nil { + return err + } + if migrator == nil { + return fmt.Errorf("failed to load migrator") + } + + return migrator.Up(context.TODO(), migrations.From(migrations.InitMigrationKey)) +} diff --git a/cmd/registry-server/main.go b/cmd/registry-server/main.go index a06c341106..47ee464ea1 100644 --- a/cmd/registry-server/main.go +++ b/cmd/registry-server/main.go @@ -2,9 +2,13 @@ package main import ( "context" + "database/sql" + "fmt" "net" "github.com/operator-framework/operator-registry/pkg/lib/log" + "github.com/operator-framework/operator-registry/pkg/sqlite/migrations" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "google.golang.org/grpc" @@ -12,7 +16,6 @@ import ( "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" - "github.com/operator-framework/operator-registry/pkg/registry" "github.com/operator-framework/operator-registry/pkg/server" "github.com/operator-framework/operator-registry/pkg/sqlite" ) @@ -36,6 +39,7 @@ func init() { rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to sqlite db") rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on") rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file") + rootCmd.Flags().Bool("skip-migrate", false, "do not attempt to migrate to the latest db revision when starting") if err := rootCmd.Flags().MarkHidden("debug"); err != nil { logrus.Panic(err.Error()) } @@ -69,15 +73,18 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port}) - var store registry.Query - store, err = sqlite.NewSQLLiteQuerier(dbName) + db, err := sql.Open("sqlite3", dbName) if err != nil { - logger.WithError(err).Warnf("failed to load db") + return err } - if store == nil { - store = registry.NewEmptyQuerier() + + // migrate to the latest version + if err := migrate(cmd, db); err != nil { + logger.WithError(err).Warnf("couldn't migrate db") } + store := sqlite.NewSQLLiteQuerierFromDb(db) + // sanity check that the db is available tables, err := store.ListTables(context.TODO()) if err != nil { @@ -103,3 +110,24 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { return nil } + + +func migrate(cmd *cobra.Command, db *sql.DB) error { + shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate") + if err != nil { + return err + } + if shouldSkipMigrate { + return nil + } + + migrator, err := sqlite.NewSQLLiteMigrator(db) + if err != nil { + return err + } + if migrator == nil { + return fmt.Errorf("failed to load migrator") + } + + return migrator.Up(context.TODO(), migrations.From(migrations.InitMigrationKey)) +} diff --git a/pkg/appregistry/dbloader.go b/pkg/appregistry/dbloader.go index 8ba4957eef..fd427c3f2a 100644 --- a/pkg/appregistry/dbloader.go +++ b/pkg/appregistry/dbloader.go @@ -22,6 +22,10 @@ func NewDbLoader(dbName string, logger *logrus.Entry) (*dbLoader, error) { return nil, err } + if err := sqlLoader.Migrate(context.TODO()); err != nil { + return nil, err + } + return &dbLoader{ loader: sqlLoader, logger: logger,