-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-queries.ts
129 lines (104 loc) · 3.79 KB
/
generate-queries.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as crypto from 'crypto'
import * as fs from 'fs'
import * as path from 'path'
export const getQuery = (js: string) => {
const hash = crypto.createHash('sha256').update(js).digest('hex')
const quotes = `$_${hash}$`
if (js.includes(quotes)) {
throw new Error(`Failed to generate quote markers to properly escape js code`)
}
return `
create or replace function git_call_sync(name text, args json) returns json as
${quotes}
var module = {}; // fake module; webpack does 'module.exports.blah = ...'
${js}; // <-- bundled code
return module.exports.git_call_sync(name, args);
${quotes}
language plv8;
create or replace function git_track() returns trigger as
$$
var git_call_sync = plv8.find_function('public.git_call_sync');
const newData = git_call_sync(
'rowToRepo',
[{OLD, NEW, TG_NAME, TG_WHEN, TG_LEVEL, TG_OP, TG_RELID, TG_TABLE_NAME, TG_TABLE_SCHEMA, TG_ARGV}]
);
return {...NEW, ...newData};
$$
language plv8;
create or replace function git_resolve(git_json json, ref text) returns json as
$$
declare
result json;
begin
select git_call_sync('gitResolve', json_build_array(git_json, ref))
into result;
return result;
end;
$$
language plpgsql;
create or replace function git_log(git_json json, depth int) returns json as
$$
declare
result json;
begin
select git_call_sync('gitLog', json_build_array(git_json, depth))
into result;
return result;
end;
$$
language plpgsql;
-- overload for getting full depth
create or replace function git_log(git_json json) returns json as
$$
declare
result json;
begin
select git_log(git_json, 0)
into result;
return result;
end;
$$
language plpgsql;
create or replace function git_set_local_config(name text, value text) returns text as
$$
select set_config('git.' || name, value, /* is_local */ true);
$$
language sql;
create or replace function git_set_global_config(name text, value text) returns text as
$$
select set_config('git.' || name, value, /* is_local */ false);
$$
language sql;
create or replace function git_get_config(name text) returns text as
$$
select current_setting('git.' || name, /* missing_ok */ true);
$$
language sql;
`
}
export const write = (filesystem = fs) => {
const sql = getQuery(filesystem.readFileSync(require.resolve('..')).toString())
const queriesDir = path.join(__dirname, '../queries')
filesystem.mkdirSync(queriesDir, {recursive: true})
filesystem.writeFileSync(path.join(queriesDir, 'create-git-functions.sql'), sql, 'utf8')
filesystem.writeFileSync(
path.join(queriesDir, 'index.js'),
`const path = require('path')\n` + //
`const fs = require('fs')\n\n` +
`exports.gitFunctionsPath = path.join(__dirname, 'create-git-functions.sql')\n\n` +
`exports.getGitFunctionsSql = () => fs.readFileSync(exports.gitFunctionsPath, 'utf8')\n\n` +
`exports.getGitFunctionsSqlAsync = () => fs.promises.readFile(exports.gitFunctionsPath, 'utf8')\n`,
'utf8',
)
filesystem.writeFileSync(
path.join(queriesDir, 'index.d.ts'),
`/** Path on filesystem to file containing git tracking SQL functions */\n` + //
`export const gitFunctionsPath: string\n\n` +
`/** Synchronously read the file system to return git tracking SQL functions as a string */\n` +
`export const getGitFunctionsSql: () => string\n\n` +
`/** Asynchronously read the file system to return git tracking SQL functions as a string */\n` +
`export const getGitFunctionsSqlAsync: () => Promise<string>\n`,
'utf8',
)
}
if (require.main === module) write()