diff --git a/internal/db/changes/changes.go b/internal/db/changes/changes.go index ddb76a6cc..77bb6cb67 100644 --- a/internal/db/changes/changes.go +++ b/internal/db/changes/changes.go @@ -163,12 +163,59 @@ func run(p *tea.Program) error { return errors.New("Error creating shadow database: " + errBuf.String()) } + { + out, err := utils.DockerExec(ctx, utils.DbId, []string{ + "sh", "-c", "psql --username postgres --host localhost --dbname '" + utils.ShadowDbName + `' <<'EOSQL' +BEGIN; +` + utils.InitialSchemaSql + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } + + { + extensionsSql, err := os.ReadFile("supabase/extensions.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 --host localhost --dbname '" + utils.ShadowDbName + `' <<'EOSQL' +BEGIN; +` + string(extensionsSql) + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } + } + migrations, err := os.ReadDir("supabase/migrations") if err != nil { return err } - for _, migration := range migrations { + for i, migration := range migrations { + // NOTE: To handle backward-compatibility. `_init.sql` as + // the first migration (prev versions of the CLI) is deprecated. + if i == 0 && strings.HasSuffix(migration.Name(), "_init.sql") { + continue + } + p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) content, err := os.ReadFile("supabase/migrations/" + migration.Name()) diff --git a/internal/db/commit/commit.go b/internal/db/commit/commit.go index 1a3e4101b..562cb6cf7 100644 --- a/internal/db/commit/commit.go +++ b/internal/db/commit/commit.go @@ -164,12 +164,59 @@ func run(p *tea.Program, name string) error { return errors.New("Error creating shadow database: " + errBuf.String()) } + { + out, err := utils.DockerExec(ctx, utils.DbId, []string{ + "sh", "-c", "psql --username postgres --host localhost --dbname '" + utils.ShadowDbName + `' <<'EOSQL' +BEGIN; +` + utils.InitialSchemaSql + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } + + { + extensionsSql, err := os.ReadFile("supabase/extensions.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 --host localhost --dbname '" + utils.ShadowDbName + `' <<'EOSQL' +BEGIN; +` + string(extensionsSql) + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } + } + migrations, err := os.ReadDir("supabase/migrations") if err != nil { return err } - for _, migration := range migrations { + for i, migration := range migrations { + // NOTE: To handle backward-compatibility. `_init.sql` as + // the first migration (prev versions of the CLI) is deprecated. + if i == 0 && strings.HasSuffix(migration.Name(), "_init.sql") { + continue + } + p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) content, err := os.ReadFile("supabase/migrations/" + migration.Name()) diff --git a/internal/db/remote/commit/commit.go b/internal/db/remote/commit/commit.go index 37c5f51e8..6d2ed781e 100644 --- a/internal/db/remote/commit/commit.go +++ b/internal/db/remote/commit/commit.go @@ -8,6 +8,7 @@ import ( "io" "os" "regexp" + "strconv" "strings" "github.com/charmbracelet/bubbles/progress" @@ -201,31 +202,31 @@ func run(p *tea.Program, url string) error { if rows, err := conn.Query(ctx, "SELECT version FROM supabase_migrations.schema_migrations ORDER BY version"); err != nil { return err } else { - versions := []string{} + remoteMigrations := []string{} for rows.Next() { var version string if err := rows.Scan(&version); err != nil { return err } - versions = append(versions, version) + remoteMigrations = append(remoteMigrations, version) } - migrations, err := os.ReadDir("supabase/migrations") + localMigrations, err := os.ReadDir("supabase/migrations") if err != nil { return err } conflictErr := errors.New("supabase_migrations.schema_migrations table is not in sync with the contents of " + utils.Bold("supabase/migrations") + ".") - if len(versions) != len(migrations) { + if len(remoteMigrations) != len(localMigrations) { return conflictErr } re := regexp.MustCompile(`([0-9]+)_.*\.sql`) - for i, version := range versions { - migrationTimestamp := re.FindStringSubmatch(migrations[i].Name())[1] + for i, remoteTimestamp := range remoteMigrations { + localTimestamp := re.FindStringSubmatch(localMigrations[i].Name())[1] - if version == migrationTimestamp { + if localTimestamp == remoteTimestamp { continue } @@ -234,27 +235,37 @@ func run(p *tea.Program, url string) error { } // 2. Create shadow db and run migrations. + p.Send(utils.StatusMsg("Creating shadow database...")) { - p.Send(utils.StatusMsg("Creating shadow database...")) + cmd := []string{} + if dbVersion, err := strconv.ParseUint(utils.DbVersion, 10, 64); err != nil { + return err + } else if dbVersion >= 140000 { + cmd = []string{"postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"} + } if _, err := utils.DockerRun( ctx, dbId, - &container.Config{ - Image: utils.DbImage, - Env: []string{"POSTGRES_PASSWORD=postgres"}, - Cmd: []string{"postgres", "-c", "wal_level=logical"}, - }, + &container.Config{Image: utils.DbImage, Env: []string{"POSTGRES_PASSWORD=postgres"}, Cmd: cmd}, &container.HostConfig{NetworkMode: netId}, ); err != nil { return err } + out, err := utils.DockerExec(ctx, dbId, []string{ - "sh", "-c", "until pg_isready --host $(hostname --ip-address); do sleep 0.1; done", + "sh", "-c", "until pg_isready --host $(hostname --ip-address); do sleep 0.1; done " + + `&& psql --username postgres --host localhost <<'EOSQL' +BEGIN; +` + utils.GlobalsSql + ` +COMMIT; +EOSQL +`, }) if err != nil { return err } + var errBuf bytes.Buffer if _, err := stdcopy.StdCopy(io.Discard, &errBuf, out); err != nil { return err @@ -264,17 +275,10 @@ func run(p *tea.Program, url string) error { } { - globalsSql, err := os.ReadFile("supabase/globals.sql") - if errors.Is(err, os.ErrNotExist) { - return errors.New("Cannot find " + utils.Bold("supabase/globals.sql") + ".") - } else if err != nil { - return err - } - out, err := utils.DockerExec(ctx, dbId, []string{ - "sh", "-c", `psql --username postgres --host localhost --dbname postgres <<'EOSQL' + "sh", "-c", `psql --username postgres --host localhost <<'EOSQL' BEGIN; -` + string(globalsSql) + ` +` + utils.InitialSchemaSql + ` COMMIT; EOSQL `, @@ -290,24 +294,24 @@ EOSQL { extensionsSql, err := os.ReadFile("supabase/extensions.sql") if errors.Is(err, os.ErrNotExist) { - return errors.New("Cannot find " + utils.Aqua("supabase/extensions.sql") + ".") + // skip } else if err != nil { return err - } - - out, err := utils.DockerExec(ctx, dbId, []string{ - "sh", "-c", `psql --username postgres --host localhost --dbname postgres <<'EOSQL' + } else { + out, err := utils.DockerExec(ctx, dbId, []string{ + "sh", "-c", `psql --username postgres --host localhost <<'EOSQL' BEGIN; ` + string(extensionsSql) + ` COMMIT; EOSQL `, - }) - if err != nil { - return err - } - if err := utils.ProcessPsqlOutput(out, p); err != nil { - return err + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } } } @@ -316,7 +320,13 @@ EOSQL return err } - for _, migration := range migrations { + for i, migration := range migrations { + // NOTE: To handle backward-compatibility. `_init.sql` as + // the first migration (prev versions of the CLI) is deprecated. + if i == 0 && strings.HasSuffix(migration.Name(), "_init.sql") { + continue + } + p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) content, err := os.ReadFile("supabase/migrations/" + migration.Name()) @@ -325,7 +335,7 @@ EOSQL } out, err := utils.DockerExec(ctx, dbId, []string{ - "sh", "-c", `psql --username postgres --host localhost --dbname postgres <<'EOSQL' + "sh", "-c", `psql --username postgres --host localhost <<'EOSQL' BEGIN; ` + string(content) + ` COMMIT; diff --git a/internal/db/remote/set/set.go b/internal/db/remote/set/set.go index f0bad345f..d52c39ad2 100644 --- a/internal/db/remote/set/set.go +++ b/internal/db/remote/set/set.go @@ -35,16 +35,9 @@ func Run(url string) error { // 2. Setup & validate `schema_migrations`. - // If `schema_migrations` doesn't exist on the remote database, create it - // and insert the timestamp for the init migration. + // If `schema_migrations` doesn't exist on the remote database, create it. if _, err := conn.Exec(ctx, "SELECT 1 FROM supabase_migrations.schema_migrations"); err != nil { - tx, err := conn.BeginTx(ctx, pgx.TxOptions{}) - if err != nil { - return err - } - defer tx.Rollback(context.Background()) //nolint:errcheck - - if _, err := tx.Exec( + if _, err := conn.Exec( ctx, `CREATE SCHEMA IF NOT EXISTS supabase_migrations; CREATE TABLE supabase_migrations.schema_migrations (version text NOT NULL PRIMARY KEY); @@ -52,57 +45,39 @@ CREATE TABLE supabase_migrations.schema_migrations (version text NOT NULL PRIMAR ); err != nil { return err } - - migrations, err := os.ReadDir("supabase/migrations") - if err != nil { - return err - } - - re := regexp.MustCompile(`([0-9]+)_.*\.sql`) - migrationTimestamp := re.FindStringSubmatch(migrations[0].Name())[1] - if _, err := tx.Exec( - ctx, - "INSERT INTO supabase_migrations.schema_migrations(version) VALUES($1);", - migrationTimestamp, - ); err != nil { - return err - } - - if err := tx.Commit(ctx); err != nil { - return err - } } - // If `migrations` is not a "prefix" of list of migrations in repo, fail & + + // If `schema_migrations` is not a "prefix" of list of migrations in repo, fail & // warn user. rows, err := conn.Query(ctx, "SELECT version FROM supabase_migrations.schema_migrations ORDER BY version") if err != nil { return err } else { - var versions []string + var remoteMigrations []string for rows.Next() { var version string if err := rows.Scan(&version); err != nil { return err } - versions = append(versions, version) + remoteMigrations = append(remoteMigrations, version) } - migrations, err := os.ReadDir("supabase/migrations") + localMigrations, err := os.ReadDir("supabase/migrations") if err != nil { return err } conflictErr := errors.New("supabase_migrations.schema_migrations table conflicts with the contents of " + utils.Bold("supabase/migrations") + ".") - if len(versions) > len(migrations) { + if len(remoteMigrations) > len(localMigrations) { return conflictErr } re := regexp.MustCompile(`([0-9]+)_.*\.sql`) - for i, version := range versions { - migrationTimestamp := re.FindStringSubmatch(migrations[i].Name())[1] + for i, remoteTimestamp := range remoteMigrations { + localTimestamp := re.FindStringSubmatch(localMigrations[i].Name())[1] - if version == migrationTimestamp { + if localTimestamp == remoteTimestamp { continue } diff --git a/internal/db/reset/reset.go b/internal/db/reset/reset.go index f84e3bf67..c7f081e41 100644 --- a/internal/db/reset/reset.go +++ b/internal/db/reset/reset.go @@ -159,57 +159,72 @@ func run(p *tea.Program) (err error) { p.Send(utils.StatusMsg("Resetting database...")) - // 2. Recreate db. - { - // https://dba.stackexchange.com/a/11895 - out, err := utils.DockerExec(ctx, utils.DbId, []string{ - "sh", "-c", "psql --username postgres --host localhost <<'EOSQL' " + - "&& dropdb --force --username postgres --host localhost '" + currBranch + "' " + - "&& createdb --username postgres --host localhost '" + currBranch + `' + if err := func() error { + // 2. Recreate db. + { + // https://dba.stackexchange.com/a/11895 + out, err := utils.DockerExec(ctx, utils.DbId, []string{ + "sh", "-c", "psql --username postgres --host localhost <<'EOSQL' " + + "&& dropdb --force --username postgres --host localhost '" + currBranch + "' " + + "&& createdb --username postgres --host localhost '" + currBranch + `' BEGIN; ` + fmt.Sprintf(utils.TerminateDbSqlFmt, currBranch) + ` COMMIT; EOSQL `, - }) - if err != nil { - return err - } - var errBuf bytes.Buffer - if _, err := stdcopy.StdCopy(io.Discard, &errBuf, out); err != nil { - return err + }) + if err != nil { + return err + } + var errBuf bytes.Buffer + if _, err := stdcopy.StdCopy(io.Discard, &errBuf, out); err != nil { + return err + } + if errBuf.Len() > 0 { + return errors.New("Error resetting database: " + errBuf.String()) + } } - if errBuf.Len() > 0 { - return errors.New("Error resetting database: " + errBuf.String()) + + // 3. Apply initial schema + extensions + migrations + seed. + + p.Send(utils.StatusMsg("Setting up initial schema...")) + { + out, err := utils.DockerExec(ctx, utils.DbId, []string{ + "sh", "-c", "psql --username postgres --host localhost --dbname '" + currBranch + `' <<'EOSQL' +BEGIN; +` + utils.InitialSchemaSql + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } } - } - // 3. Apply extensions + migrations + seed. - { p.Send(utils.StatusMsg("Applying " + utils.Bold("supabase/extensions.sql") + "...")) - { - content, err := os.ReadFile("supabase/extensions.sql") + extensionsSql, 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 --host localhost --dbname '" + currBranch + `' <<'EOSQL' BEGIN; -` + string(content) + ` +` + string(extensionsSql) + ` 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 } } @@ -217,16 +232,20 @@ EOSQL migrations, err := os.ReadDir("supabase/migrations") if err != nil { - _ = os.RemoveAll("supabase/.branches/" + currBranch) return err } - for _, migration := range migrations { + for i, migration := range migrations { + // NOTE: To handle backward-compatibility. `_init.sql` as + // the first migration (prev versions of the CLI) is deprecated. + if i == 0 && strings.HasSuffix(migration.Name(), "_init.sql") { + continue + } + p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) content, err := os.ReadFile("supabase/migrations/" + migration.Name()) if err != nil { - _ = os.RemoveAll("supabase/.branches/" + currBranch) return err } @@ -239,23 +258,19 @@ 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 " + utils.Bold("supabase/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{ @@ -267,15 +282,18 @@ 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 } } } + + return nil + }(); err != nil { + _ = os.RemoveAll("supabase/.branches/" + currBranch) + return err } return nil diff --git a/internal/start/start.go b/internal/start/start.go index 0c10cce8b..04f07b71e 100644 --- a/internal/start/start.go +++ b/internal/start/start.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "strconv" "strings" "text/template" @@ -437,31 +438,27 @@ func run(p *tea.Program) error { // Start postgres. { + cmd := []string{} + if dbVersion, err := strconv.ParseUint(utils.DbVersion, 10, 64); err != nil { + return err + } else if dbVersion >= 140000 { + cmd = []string{"postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"} + } + if _, err := utils.DockerRun( ctx, utils.DbId, - &container.Config{ - Image: utils.DbImage, - Env: []string{"POSTGRES_PASSWORD=postgres", "POSTGRES_DB=postgres"}, - Cmd: []string{"postgres", "-c", "wal_level=logical"}, - }, - &container.HostConfig{ - NetworkMode: container.NetworkMode(utils.NetId), - }, + &container.Config{Image: utils.DbImage, Env: []string{"POSTGRES_PASSWORD=postgres"}, Cmd: cmd}, + &container.HostConfig{NetworkMode: container.NetworkMode(utils.NetId)}, ); err != nil { return err } - globalsSql, err := os.ReadFile("supabase/globals.sql") - if err != nil { - return errors.New("Cannot find " + utils.Bold("supabase/globals.sql") + ".") - } - out, err := utils.DockerExec(ctx, utils.DbId, []string{ "sh", "-c", "until pg_isready --host $(hostname --ip-address); do sleep 0.1; done " + `&& psql --username postgres --host localhost <<'EOSQL' BEGIN; -` + string(globalsSql) + ` +` + utils.GlobalsSql + ` COMMIT; EOSQL `, @@ -474,9 +471,8 @@ EOSQL return err } if errBuf.Len() > 0 { - return errors.New("Error waiting for database to start: " + errBuf.String()) + return errors.New("Error starting database: " + errBuf.String()) } - } p.Send(utils.StatusMsg("Restoring branches...")) @@ -512,13 +508,13 @@ EOSQL return err } if errBuf.Len() > 0 { - _ = os.RemoveAll("supabase/.branches/" + branch.Name() + "/dump.sql") return errors.New(errBuf.String()) } return nil }(); err != nil { _ = os.RemoveAll("supabase/.branches/" + branch.Name()) + _ = os.WriteFile("supabase/.branches/_current_branch", []byte("main"), 0644) fmt.Fprintln(os.Stderr, "Error restoring branch "+utils.Aqua(branch.Name())+":", err) } } @@ -538,92 +534,82 @@ EOSQL return err } - { - out, err := utils.DockerExec(ctx, utils.DbId, []string{ - "sh", "-c", "createdb --username postgres --host localhost main", - }) - if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err - } - if err := utils.ProcessPsqlOutput(out, p); err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err + if err := func() error { + { + out, err := utils.DockerExec(ctx, utils.DbId, []string{ + "sh", "-c", "createdb --username postgres --host localhost main", + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } } - } - { - p.Send(utils.StatusMsg("Applying " + utils.Bold("supabase/extensions.sql") + "...")) - - content, err := os.ReadFile("supabase/extensions.sql") - if errors.Is(err, os.ErrNotExist) { - return errors.New("Cannot find " + utils.Bold("supabase/extensions.sql") + ".") - } else if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err - } else { + p.Send(utils.StatusMsg("Setting up initial schema...")) + { out, err := utils.DockerExec(ctx, utils.DbId, []string{ "sh", "-c", `psql --username postgres --host localhost --dbname main <<'EOSQL' BEGIN; -` + string(content) + ` +` + utils.InitialSchemaSql + ` COMMIT; EOSQL `, }) if err != nil { - _ = os.RemoveAll("supabase/.branches/main") return err } if err := utils.ProcessPsqlOutput(out, p); err != nil { - _ = os.RemoveAll("supabase/.branches/main") return err } } - } - - migrations, err := os.ReadDir("supabase/migrations") - if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err - } - - for _, migration := range migrations { - p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) - - content, err := os.ReadFile("supabase/migrations/" + migration.Name()) - if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err - } - out, err := utils.DockerExec(ctx, utils.DbId, []string{ - "sh", "-c", `psql --username postgres --host localhost --dbname main <<'EOSQL' + p.Send(utils.StatusMsg("Applying " + utils.Bold("supabase/extensions.sql") + "...")) + { + extensionsSql, err := os.ReadFile("supabase/extensions.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 --host localhost --dbname main <<'EOSQL' BEGIN; -` + string(content) + ` +` + string(extensionsSql) + ` COMMIT; EOSQL `, - }) - if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } } - if err := utils.ProcessPsqlOutput(out, p); err != nil { - _ = os.RemoveAll("supabase/.branches/main") + + migrations, err := os.ReadDir("supabase/migrations") + if err != nil { return err } - } - { - p.Send(utils.StatusMsg("Applying " + utils.Bold("supabase/seed.sql") + "...")) + for i, migration := range migrations { + // NOTE: To handle backward-compatibility. + // `_init.sql` as the first migration (prev + // versions of the CLI) is deprecated. + if i == 0 && strings.HasSuffix(migration.Name(), "_init.sql") { + continue + } + + p.Send(utils.StatusMsg("Applying migration " + utils.Bold(migration.Name()) + "...")) + + content, err := os.ReadFile("supabase/migrations/" + migration.Name()) + if err != nil { + return err + } - content, err := os.ReadFile("supabase/seed.sql") - if errors.Is(err, os.ErrNotExist) { - // skip - } else if err != nil { - _ = os.RemoveAll("supabase/.branches/main") - return err - } else { out, err := utils.DockerExec(ctx, utils.DbId, []string{ "sh", "-c", `psql --username postgres --host localhost --dbname main <<'EOSQL' BEGIN; @@ -633,14 +619,42 @@ EOSQL `, }) if err != nil { - _ = os.RemoveAll("supabase/.branches/main") return err } if err := utils.ProcessPsqlOutput(out, p); err != nil { - _ = os.RemoveAll("supabase/.branches/main") return err } } + + p.Send(utils.StatusMsg("Applying " + utils.Bold("supabase/seed.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 --host localhost --dbname main <<'EOSQL' +BEGIN; +` + string(content) + ` +COMMIT; +EOSQL +`, + }) + if err != nil { + return err + } + if err := utils.ProcessPsqlOutput(out, p); err != nil { + return err + } + } + } + + return nil + }(); err != nil { + _ = os.RemoveAll("supabase/.branches/main") + return err } } else { return err @@ -785,43 +799,39 @@ EOSQL } // Start Inbucket. - { - hostConfig := container.HostConfig{NetworkMode: container.NetworkMode(utils.NetId)} - if utils.InbucketPort != "" { - hostConfig.PortBindings = nat.PortMap{"9000/tcp": []nat.PortBinding{{HostPort: utils.InbucketPort}}} - } - + if utils.InbucketPort != "" { if _, err := utils.DockerRun( ctx, utils.InbucketId, &container.Config{ Image: utils.InbucketImage, }, - &hostConfig, + &container.HostConfig{ + NetworkMode: container.NetworkMode(utils.NetId), + PortBindings: nat.PortMap{"9000/tcp": []nat.PortBinding{{HostPort: utils.InbucketPort}}}, + }, ); err != nil { return err } } // Start Realtime. - { - if _, err := utils.DockerRun(ctx, utils.RealtimeId, &container.Config{ - Image: utils.RealtimeImage, - Env: []string{ - // connect to db directly instead of pgbouncer, since realtime doesn't work with pgbouncer for some reason - "DB_HOST=" + utils.DbId, - "DB_PORT=5432", - "DB_USER=postgres", - "DB_PASSWORD=postgres", - "DB_NAME=" + currBranch, - "SLOT_NAME=supabase_realtime", - "PORT=4000", - "SECURE_CHANNELS=true", - "JWT_SECRET=super-secret-jwt-token-with-at-least-32-characters-long", - }, - }, &container.HostConfig{NetworkMode: container.NetworkMode(utils.NetId)}); err != nil { - return err - } + if _, err := utils.DockerRun(ctx, utils.RealtimeId, &container.Config{ + Image: utils.RealtimeImage, + Env: []string{ + // connect to db directly instead of pgbouncer, since realtime doesn't work with pgbouncer for some reason + "DB_HOST=" + utils.DbId, + "DB_PORT=5432", + "DB_USER=postgres", + "DB_PASSWORD=postgres", + "DB_NAME=" + currBranch, + "SLOT_NAME=supabase_realtime", + "PORT=4000", + "SECURE_CHANNELS=true", + "JWT_SECRET=super-secret-jwt-token-with-at-least-32-characters-long", + }, + }, &container.HostConfig{NetworkMode: container.NetworkMode(utils.NetId)}); err != nil { + return err } // start postgrest @@ -853,12 +863,11 @@ EOSQL "SERVICE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIn0.M2d2z4SFn5C7HlJlaSLfrzuYim9nbY_XI40uWFN3hEE", "POSTGREST_URL=http://" + utils.RestId + ":3000", "PGRST_JWT_SECRET=super-secret-jwt-token-with-at-least-32-characters-long", - "DATABASE_URL=postgres://supabase_storage_admin:postgres@" + utils.PgbouncerId + ":5432/postgres?sslmode=disable&search_path=storage", + "DATABASE_URL=postgres://supabase_storage_admin:postgres@" + utils.PgbouncerId + ":5432/postgres?sslmode=disable", "FILE_SIZE_LIMIT=52428800", "STORAGE_BACKEND=file", "FILE_STORAGE_BACKEND_PATH=/var/lib/storage", - // TODO: https://github.com/supabase/storage-api/commit/a836fc9666c2434d89ca4b31402f74772d50fb6d - "PROJECT_REF=stub", + "TENANT_ID=stub", // TODO: https://github.com/supabase/storage-api/issues/55 "REGION=stub", "GLOBAL_S3_BUCKET=stub",