Skip to content

Commit

Permalink
feat: use embedded migrations for migrate command (#1843)
Browse files Browse the repository at this point in the history
Embeds the DB migrations in the executable. Instead of a tarball only a
single file can be used to start Supabase Auth.

This has added benefits in the Supabase platform for upgrades. See
internal discussion.
  • Loading branch information
hf authored Nov 27, 2024
1 parent 7de6bfb commit e358da5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/migrate_cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"embed"
"fmt"
"net/url"
"os"
Expand All @@ -12,6 +13,8 @@ import (
"github.com/spf13/cobra"
)

var EmbeddedMigrations embed.FS

var migrateCmd = cobra.Command{
Use: "migrate",
Long: "Migrate database strucutures. This will create new tables and add missing columns and indexes.",
Expand Down Expand Up @@ -76,11 +79,14 @@ func migrate(cmd *cobra.Command, args []string) {
log.Fatalf("%+v", errors.Wrap(err, "checking database connection"))
}

log.Debugf("Reading migrations from %s", globalConfig.DB.MigrationsPath)
mig, err := pop.NewFileMigrator(globalConfig.DB.MigrationsPath, db)
log.Debugf("Reading migrations from executable")
box, err := pop.NewMigrationBox(EmbeddedMigrations, db)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "creating db migrator"))
}

mig := box.Migrator

log.Debugf("before status")

if log.Level == logrus.DebugLevel {
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"embed"
"os/signal"
"sync"
"syscall"
Expand All @@ -12,11 +13,16 @@ import (
"github.com/supabase/auth/internal/observability"
)

//go:embed migrations/*
var embeddedMigrations embed.FS

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}

func main() {
cmd.EmbeddedMigrations = embeddedMigrations

execCtx, execCancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT)
defer execCancel()

Expand Down

0 comments on commit e358da5

Please sign in to comment.