Skip to content

Commit

Permalink
feat(db remote commit): use pg_dump if first migration
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Aug 5, 2022
1 parent 2990b57 commit a761143
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
64 changes: 59 additions & 5 deletions internal/db/remote/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -172,7 +173,62 @@ func run(p utils.Program, url string) error {
}
}

// 2. Create shadow db and run migrations.
timestamp := utils.GetCurrentTimestamp()

// 2. Special case if this is the first migration
{
localMigrations, err := os.ReadDir(filepath.Join("supabase", "migrations"))
if err != nil {
return err
}

if len(localMigrations) == 0 {
strings.Join(utils.InternalSchemas, "|")

// Use pg_dump instead of schema diff
out, err := utils.DockerRun(
ctx,
dbId,
&container.Config{
Image: utils.DbImage,
Env: []string{"POSTGRES_PASSWORD=postgres"},
Entrypoint: []string{
"sh", "-c",
"pg_dump --schema-only --quote-all-identifier --exclude-schema '" + strings.Join(utils.InternalSchemas, "|") + `' --schema '*' --extension '*' --dbname '` + url + `' | sed 's/CREATE SCHEMA "public"/-- CREATE SCHEMA "public"/'`,
},
Labels: map[string]string{
"com.supabase.cli.project": utils.Config.ProjectId,
"com.docker.compose.project": utils.Config.ProjectId,
},
},
&container.HostConfig{NetworkMode: netId},
)
if err != nil {
return err
}

var dumpBuf, errBuf bytes.Buffer
if _, err := stdcopy.StdCopy(&dumpBuf, &errBuf, out); err != nil {
return err
}
if errBuf.Len() > 0 {
return errors.New("Error running pg_dump on remote database: " + errBuf.String())
}

// Insert a row to `schema_migrations`
if _, err := conn.Query(ctx, "INSERT INTO supabase_migrations.schema_migrations(version) VALUES($1)", timestamp); err != nil {
return err
}

if err := os.WriteFile(filepath.Join("supabase", "migrations", timestamp+"_remote_commit.sql"), dumpBuf.Bytes(), 0644); err != nil {
return err
}

return nil
}
}

// 3. Create shadow db and run migrations.
p.Send(utils.StatusMsg("Creating shadow database..."))
{
cmd := []string{}
Expand Down Expand Up @@ -311,9 +367,7 @@ EOSQL
}
}

timestamp := utils.GetCurrentTimestamp()

// 3. Diff remote db (source) & shadow db (target) and write it as a new migration.
// 4. Diff remote db (source) & shadow db (target) and write it as a new migration.
{
p.Send(utils.StatusMsg("Committing changes on remote database as a new migration..."))

Expand Down Expand Up @@ -348,7 +402,7 @@ EOSQL
}
}

// 4. Insert a row to `schema_migrations`
// 5. Insert a row to `schema_migrations`
if _, err := conn.Query(ctx, "INSERT INTO supabase_migrations.schema_migrations(version) VALUES($1)", timestamp); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/utils/container_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ func filterDiffOutput(diffBytes []byte) ([]byte, error) {
}

isSchemaIgnored := func(schema string) bool {
ignoredSchemas := []string{"auth", "extensions", "pgbouncer", "realtime", "storage", "supabase_functions", "supabase_migrations"}
for _, s := range ignoredSchemas {
for _, s := range InternalSchemas {
if s == schema {
return true
}
Expand Down
3 changes: 3 additions & 0 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ var (
PostgresUrlPattern = regexp.MustCompile(`^postgres(?:ql)?://postgres:(.+)@(.+?)(:\d+)?/postgres$`)
MigrateFilePattern = regexp.MustCompile(`([0-9]+)_.*\.sql`)
BranchNamePattern = regexp.MustCompile(`[[:word:]-]+`)

// These schemas are ignored from schema diffs
InternalSchemas = []string{"auth", "extensions", "graphql_public", "pgbouncer", "realtime", "storage", "supabase_functions", "supabase_migrations", "pg_catalog", "pg_toast", "information_schema"}
)

func GetCurrentTimestamp() string {
Expand Down

0 comments on commit a761143

Please sign in to comment.