Skip to content

Commit

Permalink
Migrate database up before serving using opm
Browse files Browse the repository at this point in the history
- Add wrapper around Up migration
- Call wrapper before serving database using opm cli
  • Loading branch information
anik120 committed Oct 22, 2019
1 parent 3aa6ddd commit 9d04891
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
29 changes: 28 additions & 1 deletion cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package registry

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

"github.com/golang-migrate/migrate/v4"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
Expand Down Expand Up @@ -64,8 +66,19 @@ func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error {

logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port})

db, err := sql.Open("sqlite3", dbName)
if err != nil {
return err
}

// Migrate database to latest version before serving
err = migrateToLatest(db)
if err != nil {
return err
}

var store registry.Query
store, err = sqlite.NewSQLLiteQuerier(dbName)
store, err = sqlite.NewSQLLiteQuerierFromDb(db)
if err != nil {
logger.WithError(err).Warnf("failed to load db")
}
Expand Down Expand Up @@ -98,3 +111,17 @@ func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error {

return nil
}

func migrateToLatest(db *sql.DB) error {
migrator, err := sqlite.NewSQLLiteMigrator(db, "")
if err != nil {
return err
}
defer migrator.CleanUpMigrator()

err = migrator.MigrateUp()
if err != migrate.ErrNoChange {
return err
}
return nil
}
24 changes: 23 additions & 1 deletion pkg/sqlite/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewSQLLiteMigrator(db *sql.DB, migrationsPath string) (*SQLMigrator, error)
if err != nil {
return nil, err
}

f, err := os.Create(fmt.Sprintf("%s/%s", tempDir, file))
if err != nil {
return nil, err
Expand Down Expand Up @@ -111,6 +111,28 @@ func (m *SQLMigrator) InitMigrationVersion() error {
return nil
}

// MigrateUp is a wrapper around golang-migrate's Up. Up
// looks at the currently active migration version and will
// migrate all the way up (applying all up migrations).
func (m *SQLMigrator) MigrateUp() error {
instance, err := sqlite3.WithInstance(m.db, &sqlite3.Config{})
if err != nil {
return err
}

migrator, err := migrate.NewWithDatabaseInstance(fmt.Sprintf("file://%s", m.migrationsPath), "registrydb", instance)
if err != nil {
return err
}

err = migrator.Up()
if err != nil {
return err
}

return nil
}

// CurrentVersion returns the version of the database associated with the migrator
func (m *SQLMigrator) CurrentVersion() (uint, error) {
instance, err := sqlite3.WithInstance(m.db, &sqlite3.Config{})
Expand Down
19 changes: 19 additions & 0 deletions pkg/sqlite/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ func TestGeneratedMigrations(t *testing.T) {

require.NoError(t, err, "Unable to initialize migrator with generated migrations")
}

func TestUpMigration(t *testing.T) {
migrationsPath := "./testdata/test_db_migrations/up_migration"

store, err := NewSQLLiteLoader(WithDBName("test.db"), WithMigrationsPath(migrationsPath))
require.NoError(t, err)
defer os.Remove("test.db")

migrator, err := NewSQLLiteMigrator(store.db, migrationsPath)
require.NoError(t, err, "Unable to initialize migrator")

err = migrator.MigrateUp() // Migrating up adds a new table `test`
require.NoError(t, err)

var name string
err = store.db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='test';").Scan(&name)
require.NoError(t, err)
require.Equal(t, name, "test")
}
4 changes: 4 additions & 0 deletions pkg/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func NewSQLLiteQuerier(dbFilename string) (*SQLQuerier, error) {
return &SQLQuerier{db}, nil
}

func NewSQLLiteQuerierFromDb(db *sql.DB) (*SQLQuerier, error) {
return &SQLQuerier{db}, nil
}

func (s *SQLQuerier) ListTables(ctx context.Context) ([]string, error) {
query := "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"
rows, err := s.db.QueryContext(ctx, query)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Not required */
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS test(
test_column VARCHAR (50) PRIMARY KEY
);

0 comments on commit 9d04891

Please sign in to comment.