Repository: supabase/cli
Pull request #1254 (2023-06-27 08:02:59)

fix: apply migrations after creating shadow db
## What kind of change does this PR introduce?

Bug fix https://github.com/supabase/cli/issues/1253

## What is the current behavior?

broke db diff during last refactor, partly because we were missing a unit test

## What is the new behavior?

added unit test to check that local migrations are after creating shadow db

## Additional context

Add any other context or screenshots.


coveralls: ## Pull Request Test Coverage Report for [Build 5387292248](https://coveralls.io/builds/60980333)

* **3** of **5**   **(60.0%)**  changed or added relevant lines in **1** file are covered.
* **7** unchanged lines in **1** file lost coverage.
* Overall coverage decreased (**-0.08%**) to **61.79%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [internal/db/diff/migra.go](https://coveralls.io/builds/60980333/source?filename=internal%2Fdb%2Fdiff%2Fmigra.go#L133) | 3 | 5 | 60.0%
<!-- | **Total:** | **3** | **5** | **60.0%** | -->

|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [internal/gen/keys/keys.go](https://coveralls.io/builds/60980333/source?filename=internal%2Fgen%2Fkeys%2Fkeys.go#L94) | 7 | 7.94% |
<!-- | **Total:** | **7** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/60980333/badge)](https://coveralls.io/builds/60980333) |
| :-- | --: |
| Change from base [Build 5386155494](https://coveralls.io/builds/60977362): |  -0.08% |
| Covered Lines: | 4751 |
| Relevant Lines: | 7689 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/cmd/postgres.go b/cmd/postgres.go
index 0e8e1c25..784b0aca 100644
--- a/cmd/postgres.go
+++ b/cmd/postgres.go
@@ -26,7 +26,7 @@ var (
 	postgresConfigUpdateCmd = &cobra.Command{
 		Use:   "update",
 		Short: "Update Postgres database config",
-		Long: `Overriding the default Postgres config could result in unstable database behaviour.
+		Long: `Overriding the default Postgres config could result in unstable database behavior.
 Custom configuration also overrides the optimizations generated based on the compute add-ons in use.`,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			return update.Run(cmd.Context(), flags.ProjectRef, postgresConfigValues, postgresConfigUpdateReplaceMode, afero.NewOsFs())
diff --git a/internal/db/diff/migra.go b/internal/db/diff/migra.go
index b28868d1..3afd6dc5 100644
--- a/internal/db/diff/migra.go
+++ b/internal/db/diff/migra.go
@@ -21,6 +21,7 @@ import (
 	"github.com/supabase/cli/internal/db/start"
 	"github.com/supabase/cli/internal/gen/keys"
 	"github.com/supabase/cli/internal/migration/apply"
+	"github.com/supabase/cli/internal/migration/list"
 	"github.com/supabase/cli/internal/utils"
 )
 
@@ -127,7 +128,11 @@ func connectShadowDatabase(ctx context.Context, timeout time.Duration, options .
 }
 
 func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
-	return MigrateShadowDatabaseVersions(ctx, container, nil, fsys, options...)
+	migrations, err := list.LoadLocalMigrations(fsys)
+	if err != nil {
+		return err
+	}
+	return MigrateShadowDatabaseVersions(ctx, container, migrations, fsys, options...)
 }
 
 func MigrateShadowDatabaseVersions(ctx context.Context, container string, migrations []string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {


Test patch:
diff --git a/internal/db/diff/migra_test.go b/internal/db/diff/migra_test.go
index 53e81acc..02b9e147 100644
--- a/internal/db/diff/migra_test.go
+++ b/internal/db/diff/migra_test.go
@@ -3,6 +3,7 @@ package diff
 import (
 	"context"
 	"errors"
+	"fmt"
 	"io"
 	"net/http"
 	"os"
@@ -16,6 +17,7 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 	"github.com/supabase/cli/internal/db/reset"
+	"github.com/supabase/cli/internal/migration/repair"
 	"github.com/supabase/cli/internal/testing/apitest"
 	"github.com/supabase/cli/internal/testing/pgtest"
 	"github.com/supabase/cli/internal/utils"
@@ -235,6 +237,9 @@ At statement 0: create schema public`)
 	t.Run("throws error on failure to diff target", func(t *testing.T) {
 		// Setup in-memory fs
 		fsys := afero.NewMemMapFs()
+		path := filepath.Join(utils.MigrationsDir, "0_test.sql")
+		sql := "create schema test"
+		require.NoError(t, afero.WriteFile(fsys, path, []byte(sql), 0644))
 		// Setup mock docker
 		require.NoError(t, apitest.MockDocker(utils.Docker))
 		defer gock.OffAll()
@@ -255,7 +260,19 @@ At statement 0: create schema public`)
 		conn.Query(utils.GlobalsSql).
 			Reply("CREATE SCHEMA").
 			Query(utils.InitialSchemaSql).
-			Reply("CREATE SCHEMA")
+			Reply("CREATE SCHEMA").
+			Query(repair.CREATE_VERSION_SCHEMA).
+			Reply("CREATE SCHEMA").
+			Query(repair.CREATE_VERSION_TABLE).
+			Reply("CREATE TABLE").
+			Query(repair.ADD_STATEMENTS_COLUMN).
+			Reply("ALTER TABLE").
+			Query(repair.ADD_NAME_COLUMN).
+			Reply("ALTER TABLE").
+			Query(sql).
+			Reply("CREATE SCHEMA").
+			Query(repair.INSERT_MIGRATION_VERSION, "0", "test", fmt.Sprintf("{%s}", sql)).
+			Reply("INSERT 0 1")
 		// Run test
 		diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, conn.Intercept)
 		// Check error