Repository navigation
Expand file tree
/
Copy pathclean_schema.js
More file actions
56 lines (44 loc) · 1.77 KB
/
Copy pathclean_schema.js
File metadata and controls
56 lines (44 loc) · 1.77 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
const fs = require('fs');
const readline = require('readline');
async function cleanSchema(inputFile, outputFile) {
const fileStream = fs.createReadStream(inputFile);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const outStream = fs.createWriteStream(outputFile);
let skipBlock = false;
for await (let line of rl) {
// 1. Skip Supabase specific system statements
if (line.match(/^CREATE SCHEMA (IF NOT EXISTS )?public/)) {
continue;
}
if (line.match(/^ALTER SCHEMA public OWNER TO /)) {
continue;
}
if (line.match(/^COMMENT ON SCHEMA public IS/)) {
continue;
}
// 2. Skip OWNER TO statements (Supabase admin/postgres permissions usually block this)
if (line.match(/^ALTER .* OWNER TO .*/)) {
continue;
}
// 3. Make Functions safe to recreate (Updates existing functions)
if (line.match(/^CREATE FUNCTION /)) {
line = line.replace(/^CREATE FUNCTION /, 'CREATE OR REPLACE FUNCTION ');
}
// 4. Make Views safe to recreate
if (line.match(/^CREATE VIEW /)) {
line = line.replace(/^CREATE VIEW /, 'CREATE OR REPLACE VIEW ');
}
// 5. Triggers: We can leave them as is, but if they exist, it might fail.
// Postgres 14+ supports CREATE OR REPLACE TRIGGER. Supabase is 15+.
if (line.match(/^CREATE TRIGGER /)) {
line = line.replace(/^CREATE TRIGGER /, 'CREATE OR REPLACE TRIGGER ');
}
// Write cleaned line to output
outStream.write(line + '\n');
}
console.log(`Cleaned schema saved to ${outputFile}`);
}
cleanSchema('schema.sql', 'schema_clean.sql');