Skip to content

Commit

Permalink
feat: commit & reset
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Nov 25, 2021
1 parent 78fd495 commit 48b12d2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
30 changes: 15 additions & 15 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@ package cmd

import (
"github.com/spf13/cobra"
"github.com/supabase/cli/internal/db/dump"
"github.com/supabase/cli/internal/db/restore"
"github.com/supabase/cli/internal/db/commit"
"github.com/supabase/cli/internal/db/reset"
)

var (
migrationName string

dbCmd = &cobra.Command{
Use: "db",
Short: "Dump or restore the local database.",
Short: "Commit or reset the local database.",
}

dbDumpCmd = &cobra.Command{
Use: "dump",
Short: "Diffs the local database with current migrations, writing it as a new migration, and writes a new structured dump.",
dbCommitCmd = &cobra.Command{
Use: "commit",
Short: "Diffs the local database with current migrations, writing it as a new migration.",
RunE: func(cmd *cobra.Command, args []string) error {
return dump.DbDump(migrationName)
return commit.Run(migrationName)
},
}

dbRestoreCmd = &cobra.Command{
Use: "restore",
Short: "Restores the local database to reflect current migrations. Any changes on the local database that is not dumped will be lost.",
dbResetCmd = &cobra.Command{
Use: "reset",
Short: "Resets the local database to reflect current migrations. Any changes on the local database that is not committed will be lost.",
RunE: func(cmd *cobra.Command, args []string) error {
return restore.DbRestore()
return reset.Run()
},
}
)

func init() {
dbDumpCmd.Flags().StringVar(&migrationName, "name", "", "Name of the migration.")
cobra.CheckErr(dbDumpCmd.MarkFlagRequired("name"))
dbCommitCmd.Flags().StringVar(&migrationName, "name", "", "Name of the migration.")
cobra.CheckErr(dbCommitCmd.MarkFlagRequired("name"))

dbCmd.AddCommand(dbDumpCmd)
dbCmd.AddCommand(dbRestoreCmd)
dbCmd.AddCommand(dbCommitCmd)
dbCmd.AddCommand(dbResetCmd)
rootCmd.AddCommand(dbCmd)
}
8 changes: 4 additions & 4 deletions internal/db/dump/dump.go → internal/db/commit/commit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dump
package commit

import (
"bytes"
Expand All @@ -18,7 +18,7 @@ import (
)

// TODO: Handle cleanup on SIGINT/SIGTERM.
func DbDump(name string) error {
func Run(name string) error {
// Sanity checks.
{
utils.LoadConfig()
Expand Down Expand Up @@ -46,13 +46,13 @@ func DbDump(name string) error {
return err
}
if errors.Is(ctx.Err(), context.Canceled) {
return errors.New("Aborted `supabase db dump`.")
return errors.New("Aborted `supabase db commit`.")
}
if err := <-errCh; err != nil {
return err
}

fmt.Println("Finished `supabase db dump` on `" + currBranch + "`.")
fmt.Println("Finished `supabase db commit` on branch " + currBranch + ".")
return nil
}

Expand Down
80 changes: 59 additions & 21 deletions internal/db/restore/restore.go → internal/db/reset/reset.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package restore
package reset

import (
"bytes"
Expand All @@ -18,17 +18,17 @@ import (
)

// TODO: Handle cleanup on SIGINT/SIGTERM.
func DbRestore() error {
func Run() error {
// Sanity checks.
{
utils.LoadConfig()
utils.AssertSupabaseStartIsRunning()

if branch, err := utils.GetCurrentBranch(); err != nil {
branch, err := utils.GetCurrentBranch()
if err != nil {
return err
} else {
currBranch = branch
}
currBranch = branch
}

s := spinner.NewModel()
Expand All @@ -46,13 +46,13 @@ func DbRestore() error {
return err
}
if errors.Is(ctx.Err(), context.Canceled) {
return errors.New("Aborted `supabase db restore`.")
return errors.New("Aborted `supabase db reset`.")
}
if err := <-errCh; err != nil {
return err
}

fmt.Println("Finished `supabase db restore` on `" + currBranch + "`.")
fmt.Println("Finished `supabase db reset` on branch " + currBranch + ".")
return nil
}

Expand Down Expand Up @@ -188,10 +188,11 @@ EOSQL
}
}

// 3. Apply migrations + seed.
// 3. Apply migrations + extensions + seed.
{
migrations, err := os.ReadDir("supabase/migrations")
if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}

Expand All @@ -200,6 +201,7 @@ EOSQL

content, err := os.ReadFile("supabase/migrations/" + migration.Name())
if err != nil {
_ = os.RemoveAll("supabase/.branches/" + currBranch)
return err
}

Expand All @@ -212,34 +214,70 @@ 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..."))
p.Send(utils.StatusMsg("Applying extensions.sql..."))

content, err := os.ReadFile("supabase/seed.sql")
if errors.Is(err, os.ErrNotExist) {
// skip
} else if err != nil {
return err
} else {
out, err := utils.DockerExec(ctx, utils.DbId, []string{
"sh", "-c", "psql --username postgres --dbname '" + currBranch + `' <<'EOSQL'
{
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 {
return err
})
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
}
}
if err := utils.ProcessPsqlOutput(out, p); err != nil {
}

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

{
content, err := os.ReadFile("supabase/seed.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
}
}
}
}
Expand Down

0 comments on commit 48b12d2

Please sign in to comment.