-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.ts
64 lines (59 loc) · 1.42 KB
/
transform.ts
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
import { exit } from "process";
import { Build, buildSchema } from "../../schemas";
import { pgClient } from "../pg-connector";
import * as fs from "fs";
import "dotenv/config";
(async () => {
const build = buildSchema.parse(process.env.BUILD);
type Source = {
fileName: string;
treeDepth: number;
builds: Array<Build>;
};
const sources: Array<Source> = [
{
fileName: "borough",
treeDepth: 0,
builds: ["admin", "pluto"],
},
{
fileName: "admin",
treeDepth: 1,
builds: ["admin"],
},
{
fileName: "capital-planning",
treeDepth: 0,
builds: ["capital-planning"],
},
{
fileName: "pluto",
treeDepth: 1,
builds: ["pluto"],
},
];
const buildSources =
build === "all"
? sources
: sources.filter((source) => source.builds.includes(build));
buildSources.sort((a, b) => a.treeDepth - b.treeDepth);
try {
await pgClient.connect();
await pgClient.query("BEGIN;");
buildSources.forEach(async (source) => {
const sql = fs
.readFileSync(`pg/model-transform/${source.fileName}.sql`)
.toString();
console.debug("source", source.fileName);
await pgClient.query(sql);
});
await pgClient.query("COMMIT;");
} catch (e) {
await pgClient.query("ROLLBACK;");
console.error(e);
} finally {
console.debug("ending");
await pgClient.end();
exit();
}
})();