Skip to content

Commit

Permalink
feat: allow configuring migra schema diff (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Aug 4, 2022
1 parent 013daee commit 7be5ed5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ var (
}

useMigra bool
schema string

dbCommitCmd = &cobra.Command{
Use: "commit <migration name>",
Short: "Diffs the local database with current migrations, writing it as a new migration.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if useMigra {
return commit.RunMigra(args[0], afero.NewOsFs())
return commit.RunMigra(args[0], schema, afero.NewOsFs())
}
return commit.Run(args[0])
},
Expand Down Expand Up @@ -143,6 +144,7 @@ func init() {
dbCmd.AddCommand(dbBranchCmd)
dbCmd.AddCommand(dbChangesCmd)
dbCommitCmd.Flags().BoolVar(&useMigra, "migra", false, "Use migra to generate schema diff.")
dbCommitCmd.Flags().StringVarP(&schema, "schema", "s", "public", "The schema to diff (defaults to public).")
dbCmd.AddCommand(dbCommitCmd)
dbPushCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the migrations that would be applied, but don't actually apply them.")
dbCmd.AddCommand(dbPushCmd)
Expand Down
14 changes: 9 additions & 5 deletions internal/db/commit/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
resetShadowScript string
)

func RunMigra(name string, fsys afero.Fs) error {
func RunMigra(name, schema string, fsys afero.Fs) error {
// Sanity checks.
{
if err := utils.AssertSupabaseStartIsRunning(); err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func RunMigra(name string, fsys afero.Fs) error {
baseUrl := "postgresql://postgres:postgres@" + utils.DbId + ":5432/"
source := baseUrl + utils.ShadowDbName
target := baseUrl + "postgres"
diff, err := diffSchema(ctx, source, target)
diff, err := diffSchema(ctx, source, target, schema)
if err != nil {
return err
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func applyMigrations(ctx context.Context, url string, fsys afero.Fs, options ...
}

// Diffs local database schema against shadow, saves output as a migration script.
func diffSchema(ctx context.Context, source, target string) (string, error) {
func diffSchema(ctx context.Context, source, target, schema string) (string, error) {
// Pull migra image
imageUrl := "docker.io/" + diffImage
if _, _, err := utils.Docker.ImageInspectWithRaw(ctx, imageUrl); err != nil {
Expand Down Expand Up @@ -251,8 +251,12 @@ func diffSchema(ctx context.Context, source, target string) (string, error) {
migraId,
&container.Config{
Image: imageUrl,
Env: []string{"SOURCE=" + source, "TARGET=" + target},
Cmd: []string{"/bin/sh", "-c", diffSchemaScript},
Env: []string{
"SOURCE=" + source,
"TARGET=" + target,
"SCHEMA=" + schema,
},
Cmd: []string{"/bin/sh", "-c", diffSchemaScript},
Labels: map[string]string{
"com.supabase.cli.project": utils.Config.ProjectId,
"com.docker.compose.project": utils.Config.ProjectId,
Expand Down
2 changes: 1 addition & 1 deletion internal/db/commit/templates/migra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ sed -i 's/ and e.objid is null/ -- and e.objid is null/g' \
/usr/local/lib/python3.9/site-packages/schemainspect/pg/sql/enums.sql

# diff public schema only
migra --unsafe --schema=public "$SOURCE" "$TARGET"
migra --unsafe --schema="${SCHEMA:public}" "$SOURCE" "$TARGET"

0 comments on commit 7be5ed5

Please sign in to comment.