Skip to content

Commit

Permalink
feat(server): migrate up on start
Browse files Browse the repository at this point in the history
  • Loading branch information
ecordell committed Oct 24, 2019
1 parent ad97992 commit 77b1ce2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 16 deletions.
11 changes: 10 additions & 1 deletion cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"database/sql"
"fmt"
"net"

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions cmd/initializer/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"database/sql"
"fmt"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 32 additions & 7 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package registry

import (
"context"
"database/sql"
"fmt"
"net"

"github.com/sirupsen/logrus"
Expand All @@ -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 {
Expand All @@ -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

}
Expand All @@ -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 {
Expand All @@ -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))
}
40 changes: 34 additions & 6 deletions cmd/registry-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ 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"
"google.golang.org/grpc/reflection"

"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"
)
Expand All @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
4 changes: 4 additions & 0 deletions pkg/appregistry/dbloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 77b1ce2

Please sign in to comment.