Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/db/declarative/declarative.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ func WriteDeclarativeSchemas(output diff.DeclarativeOutput, fsys afero.Fs) error
return err
}
}
// When pg-delta has its own config section, the declarative path is the single
// source of truth there; do not overwrite [db.migrations] schema_paths.
if utils.IsPgDeltaEnabled() && utils.Config.Experimental.PgDelta != nil &&
len(utils.Config.Experimental.PgDelta.DeclarativeSchemaPath) > 0 {
// When pg-delta is enabled, the declarative directory (default or configured)
// is the source of truth; do not overwrite [db.migrations] schema_paths.
if utils.IsPgDeltaEnabled() {
return nil
}
utils.Config.Db.Migrations.SchemaPaths = []string{
Expand Down
60 changes: 60 additions & 0 deletions internal/db/declarative/declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ func TestWriteDeclarativeSchemas(t *testing.T) {
assert.Contains(t, string(cfg), `"database"`)
}

func TestWriteDeclarativeSchemasSkipsConfigUpdateWhenPgDeltaEnabled(t *testing.T) {
fsys := afero.NewMemMapFs()
originalConfig := "[db]\n"
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte(originalConfig), 0644))
original := utils.Config.Experimental.PgDelta
utils.Config.Experimental.PgDelta = &config.PgDeltaConfig{Enabled: true}
t.Cleanup(func() {
utils.Config.Experimental.PgDelta = original
})

output := diff.DeclarativeOutput{
Files: []diff.DeclarativeFile{
{Path: "schemas/public/tables/users.sql", SQL: "create table users(id bigint);"},
},
}

err := WriteDeclarativeSchemas(output, fsys)
require.NoError(t, err)

users, err := afero.ReadFile(fsys, filepath.Join(utils.DeclarativeDir, "schemas", "public", "tables", "users.sql"))
require.NoError(t, err)
assert.Equal(t, "create table users(id bigint);", string(users))

cfg, err := afero.ReadFile(fsys, utils.ConfigPath)
require.NoError(t, err)
assert.Equal(t, originalConfig, string(cfg))
}

func TestTryCacheMigrationsCatalogWritesPrefixedCache(t *testing.T) {
fsys := afero.NewMemMapFs()
original := utils.Config.Experimental.PgDelta
Expand Down Expand Up @@ -146,6 +174,38 @@ func TestWriteDeclarativeSchemasUsesConfiguredDir(t *testing.T) {
assert.Contains(t, string(cfg), `db/decl`)
}

func TestWriteDeclarativeSchemasSkipsConfigUpdateForPgDeltaCustomDir(t *testing.T) {
fsys := afero.NewMemMapFs()
originalConfig := "[db]\n"
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte(originalConfig), 0644))
original := utils.Config.Experimental.PgDelta
utils.Config.Experimental.PgDelta = &config.PgDeltaConfig{
Enabled: true,
DeclarativeSchemaPath: filepath.Join(utils.SupabaseDirPath, "db", "decl"),
}
t.Cleanup(func() {
utils.Config.Experimental.PgDelta = original
})

output := diff.DeclarativeOutput{
Files: []diff.DeclarativeFile{
{Path: "cluster/roles.sql", SQL: "create role app;"},
},
}

err := WriteDeclarativeSchemas(output, fsys)
require.NoError(t, err)

rolesPath := filepath.Join(utils.SupabaseDirPath, "db", "decl", "cluster", "roles.sql")
roles, err := afero.ReadFile(fsys, rolesPath)
require.NoError(t, err)
assert.Equal(t, "create role app;", string(roles))

cfg, err := afero.ReadFile(fsys, utils.ConfigPath)
require.NoError(t, err)
assert.Equal(t, originalConfig, string(cfg))
}

func TestWriteDeclarativeSchemasRejectsUnsafePath(t *testing.T) {
// Export paths must stay within supabase/declarative to prevent traversal.
fsys := afero.NewMemMapFs()
Expand Down
9 changes: 8 additions & 1 deletion internal/db/diff/templates/pgdelta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const target = Deno.env.get("TARGET");

const includedSchemas = Deno.env.get("INCLUDED_SCHEMAS");
if (includedSchemas) {
supabase.filter = { schema: includedSchemas.split(",") };
const schemas = includedSchemas.split(",");
const schemaFilter = {
or: [{ "*/schema": schemas }, { "schema/name": schemas }],
};
// CompositionPattern `and` is valid FilterDSL; Deno's structural typing is strict on `or` branches.
supabase.filter = {
and: [supabase.filter!, schemaFilter],
} as typeof supabase.filter;
}

const formatOptionsRaw = Deno.env.get("FORMAT_OPTIONS");
Expand Down
17 changes: 10 additions & 7 deletions internal/db/diff/templates/pgdelta_declarative_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ async function resolveInput(ref: string | undefined) {
const source = Deno.env.get("SOURCE");
const target = Deno.env.get("TARGET");
supabase.filter = {
// Also allow dropped extensions from migrations to be capted in the declarative schema export
// Also allow dropped extensions from migrations to be captured in the declarative schema export
// TODO: fix upstream bug into pgdelta supabase integration
or: [
...supabase.filter.or,
{ type: "extension", operation: "drop", scope: "object" },
...supabase.filter!.or!,
{ objectType: "extension", operation: "drop", scope: "object" },
],
};

const includedSchemas = Deno.env.get("INCLUDED_SCHEMAS");
if (includedSchemas) {
const schemaFilter = { schema: includedSchemas.split(",") };
supabase.filter = supabase.filter
? { and: [supabase.filter, schemaFilter] }
: schemaFilter;
const schemas = includedSchemas.split(",");
const schemaFilter = {
or: [{ "*/schema": schemas }, { "schema/name": schemas }],
};
supabase.filter = {
and: [supabase.filter!, schemaFilter],
} as unknown as typeof supabase.filter;
}

const formatOptionsRaw = Deno.env.get("FORMAT_OPTIONS");
Expand Down
Loading