-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathpgdelta_declarative_export.ts
More file actions
73 lines (69 loc) · 1.92 KB
/
pgdelta_declarative_export.ts
File metadata and controls
73 lines (69 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// This script is executed inside Edge Runtime by the CLI to export a target
// schema as declarative file payloads. It accepts either live DB URLs or
// catalog-file references for SOURCE/TARGET, which enables cached sync flows.
import {
createPlan,
deserializeCatalog,
exportDeclarativeSchema,
} from "npm:@supabase/pg-delta@1.0.0-alpha.13";
import { supabase } from "npm:@supabase/pg-delta@1.0.0-alpha.13/integrations/supabase";
async function resolveInput(ref: string | undefined) {
if (!ref) {
return null;
}
if (ref.startsWith("postgres://") || ref.startsWith("postgresql://")) {
return ref;
}
const json = await Deno.readTextFile(ref);
return deserializeCatalog(JSON.parse(json));
}
const source = Deno.env.get("SOURCE");
const target = Deno.env.get("TARGET");
const includedSchemas = Deno.env.get("INCLUDED_SCHEMAS");
if (includedSchemas) {
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");
let formatOptions = undefined;
if (formatOptionsRaw) {
formatOptions = JSON.parse(formatOptionsRaw);
}
try {
const result = await createPlan(
await resolveInput(source),
await resolveInput(target),
{
...supabase,
skipDefaultPrivilegeSubtraction: true,
},
);
if (!result) {
console.log(
JSON.stringify({
version: 1,
mode: "declarative",
files: [],
}),
);
} else {
const output = exportDeclarativeSchema(result, {
integration: supabase,
formatOptions,
});
console.log(
JSON.stringify(output, (_key, value) =>
typeof value === "bigint" ? Number(value) : value,
),
);
}
} catch (e) {
console.error(e);
// Force close event loop
throw new Error("");
}