Skip to content

Commit

Permalink
Bundle migrations in binary
Browse files Browse the repository at this point in the history
Use the go-bindata tool to generate a golang migrations file to
compile the content of the db_migrations folder into the bundle. This
ensures that the compiled binaries are portable and do not require
around the migrations themselves along with the binary.

Also updated the makefile to pull the go-bindata file and generate the
`migration.go` file as part of `make build`
  • Loading branch information
kevinrizza committed Oct 15, 2019
1 parent b9de978 commit fcf1ff1
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 26 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CMDS := $(addprefix bin/, $(shell go list $(MOD_FLAGS) ./cmd/... | xargs -I{} b

.PHONY: build test vendor clean

all: clean test build
all: clean install-go-bindata test build

$(CMDS):
go build $(MOD_FLAGS) $(extra_flags) -o $@ ./cmd/$(shell basename $@)
Expand All @@ -25,6 +25,12 @@ image-upstream:
vendor:
go mod vendor

install-go-bindata:
go get -u github.com/go-bindata/go-bindata/...

generate-migration-bundle:
go-bindata -pkg sqlite -o ./pkg/sqlite/migrations.go ./pkg/sqlite/db_migrations/

codegen:
protoc -I pkg/api/ --go_out=plugins=grpc:pkg/api pkg/api/*.proto
protoc -I pkg/api/grpc_health_v1 --go_out=plugins=grpc:pkg/api/grpc_health_v1 pkg/api/grpc_health_v1/*.proto
Expand Down
2 changes: 1 addition & 1 deletion cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
logger.Fatalf("error getting configmap: %s", err)
}

sqlLoader, err := sqlite.NewSQLLiteLoader(dbName, "")
sqlLoader, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(dbName))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
return err
}

dbLoader, err := sqlite.NewSQLLiteLoader(outFilename, "")
dbLoader, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(outFilename))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/appregistry/dbloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func NewDbLoader(dbName string, logger *logrus.Entry) (*dbLoader, error) {
sqlLoader, err := sqlite.NewSQLLiteLoader(dbName, "")
sqlLoader, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(dbName))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func server() {
}
s := grpc.NewServer()

load, err := sqlite.NewSQLLiteLoader(dbName, "../sqlite/db_migrations")
load, err := sqlite.NewSQLLiteLoader(sqlite.WithDBName(dbName))
if err != nil {
logrus.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestConfigMapLoader(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)

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

Expand All @@ -34,7 +34,7 @@ func TestConfigMapLoader(t *testing.T) {
}

func TestQuerierForConfigmap(t *testing.T) {
load, err := NewSQLLiteLoader("test.db", "./db_migrations")
load, err := NewSQLLiteLoader(WithDBName("test.db"))
require.NoError(t, err)
defer os.Remove("test.db")

Expand Down
23 changes: 23 additions & 0 deletions pkg/sqlite/db_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sqlite

type DbOptions struct {
// OutFileName is used to define the database file name that is generated from the loader
OutFileName string

// Migrator refers to the SQL migrator used to initialize the database with
MigrationsPath string
}

type DbOption func(*DbOptions)

func WithDBName(name string) DbOption {
return func(o *DbOptions) {
o.OutFileName = name
}
}

func WithMigrationsPath(path string) DbOption {
return func(o *DbOptions) {
o.MigrationsPath = path
}
}
6 changes: 3 additions & 3 deletions pkg/sqlite/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestDirectoryLoader(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)

store, err := NewSQLLiteLoader("test.db", "./db_migrations")
store, err := NewSQLLiteLoader(WithDBName("test.db"))
require.NoError(t, err)
defer func() {
if err := os.Remove("test.db"); err != nil {
Expand All @@ -33,7 +33,7 @@ func TestDirectoryLoader(t *testing.T) {
func TestDirectoryLoaderWithBadManifests(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)

store, err := NewSQLLiteLoader("test.db", "./db_migrations")
store, err := NewSQLLiteLoader(WithDBName("test.db"))
require.NoError(t, err)
defer func() {
if err := os.Remove("test.db"); err != nil {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestDirectoryLoaderWithBadManifests(t *testing.T) {
}

func TestQuerierForDirectory(t *testing.T) {
load, err := NewSQLLiteLoader("test.db", "./db_migrations")
load, err := NewSQLLiteLoader(WithDBName("test.db"))
require.NoError(t, err)
defer func() {
if err := os.Remove("test.db"); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions pkg/sqlite/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ type SQLLoader struct {

var _ registry.Load = &SQLLoader{}

func NewSQLLiteLoader(outFilename, migrationsPath string) (*SQLLoader, error) {
db, err := sql.Open("sqlite3", outFilename) // TODO: ?immutable=true
func NewSQLLiteLoader(opts ...DbOption) (*SQLLoader, error) {
options := DbOptions{}
for _, o := range opts {
o(&options)
}

db, err := sql.Open("sqlite3", options.OutFileName) // TODO: ?immutable=true
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -79,7 +84,12 @@ func NewSQLLiteLoader(outFilename, migrationsPath string) (*SQLLoader, error) {
}

// Apply the current latest database version to keep net new databases in sync with upgradeable ones
migrator := NewSQLLiteMigrator(db, migrationsPath)
migrator, err := NewSQLLiteMigrator(db, options.MigrationsPath)
if err != nil {
return nil, err
}
defer migrator.CleanUpMigrator()

err = migrator.InitMigrationVersion()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestAddPackageChannels(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
db := fmt.Sprintf("%d.db", rand.Int())
store, err := NewSQLLiteLoader(db, "./db_migrations")
store, err := NewSQLLiteLoader(WithDBName(db))
require.NoError(t, err)
defer func() {
if err := os.Remove(db); err != nil {
Expand Down
Loading

0 comments on commit fcf1ff1

Please sign in to comment.