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
2 changes: 0 additions & 2 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ var (
if usePgSchema {
differ = diff.DiffPgSchema
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "--use-pg-schema flag is experimental and may not include all entities, such as views and grants.")
} else if !viper.GetBool("EXPERIMENTAL") {
differ = diff.DiffSchemaMigraBash
}
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, differ, afero.NewOsFs())
},
Expand Down
18 changes: 11 additions & 7 deletions internal/db/diff/templates/migra.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from "npm:@pgkit/client";
import { createClient, sql } from "npm:@pgkit/client";
import { Migration } from "npm:@pgkit/migra";

// Avoids error on self-signed certificate
Expand All @@ -20,7 +20,11 @@ const extensionSchemas = [
];

try {
let sql = "";
// Step down from login role to postgres
await clientHead.query(sql`set role postgres`);
// Use schema qualified references for pg_get_expr
await clientHead.query(sql`set search_path = ''`);
let result = "";
for (const schema of includedSchemas) {
const m = await Migration.create(clientBase, clientHead, {
schema,
Expand All @@ -35,7 +39,7 @@ try {
} else {
m.add_all_changes(true);
}
sql += m.sql;
result += m.sql;
}
if (includedSchemas.length === 0) {
// Migra does not ignore custom types and triggers created by extensions, so we diff
Expand All @@ -48,7 +52,7 @@ try {
e.set_safety(false);
e.add(e.changes.schemas({ creations_only: true }));
e.add_extension_changes();
sql += e.sql;
result += e.sql;
}
// Diff user defined entities in non-managed schemas, including extensions.
const m = await Migration.create(clientBase, clientHead, {
Expand All @@ -61,7 +65,7 @@ try {
});
m.set_safety(false);
m.add_all_changes(true);
sql += m.sql;
result += m.sql;
// For managed schemas, we want to include triggers and RLS policies only.
for (const schema of managedSchemas) {
const s = await Migration.create(clientBase, clientHead, {
Expand All @@ -73,10 +77,10 @@ try {
s.add(s.changes.rlspolicies({ drops_only: true }));
s.add(s.changes.rlspolicies({ creations_only: true }));
s.add(s.changes.triggers({ creations_only: true }));
sql += s.sql;
result += s.sql;
}
}
console.log(sql);
console.log(result);
} catch (e) {
console.error(e);
} finally {
Expand Down
Loading