Skip to content

Commit

Permalink
fix: Apply extensions.sql before migrations
Browse files Browse the repository at this point in the history
Some of the migrations might depend on certain extensions. Running extensions *after* migrations breaks such migrations.
  • Loading branch information
everzet authored and soedirgo committed Dec 8, 2021
1 parent 6b78950 commit 5a3db53
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,37 @@ EOSQL
}
}

// 3. Apply migrations + extensions + seed.
// 3. Apply extensions + migrations + seed.
{
p.Send(utils.StatusMsg("Applying extensions.sql..."))

{
content, err := os.ReadFile("supabase/extensions.sql")
if errors.Is(err, os.ErrNotExist) {
// skip
} else if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
} else {
out, err := utils.DockerExec(ctx, utils.DbId, []string{
"sh", "-c", "psql --username postgres --dbname '" + currBranch + `' <<'EOSQL'
BEGIN;
` + string(content) + `
COMMIT;
EOSQL
`,
})
if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}
if err := utils.ProcessPsqlOutput(out, p); err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}
}
}

migrations, err := os.ReadDir("supabase/migrations")
if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
Expand Down Expand Up @@ -217,35 +246,6 @@ EOSQL
}
}

p.Send(utils.StatusMsg("Applying extensions.sql..."))

{
content, err := os.ReadFile("supabase/extensions.sql")
if errors.Is(err, os.ErrNotExist) {
// skip
} else if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
} else {
out, err := utils.DockerExec(ctx, utils.DbId, []string{
"sh", "-c", "psql --username postgres --dbname '" + currBranch + `' <<'EOSQL'
BEGIN;
` + string(content) + `
COMMIT;
EOSQL
`,
})
if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}
if err := utils.ProcessPsqlOutput(out, p); err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}
}
}

p.Send(utils.StatusMsg("Applying seed.sql..."))

{
Expand Down

0 comments on commit 5a3db53

Please sign in to comment.