@prisma-psm/pg 1.0.45 → 1.0.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -7,11 +7,6 @@ export interface RestoreOptions {
|
|
|
7
7
|
parser: PostgresParserOptions;
|
|
8
8
|
}
|
|
9
9
|
export declare function lockTable(opts: RestoreOptions): string[];
|
|
10
|
-
export interface RestoreOptions {
|
|
11
|
-
source: string;
|
|
12
|
-
model: ModelOptions;
|
|
13
|
-
parser: PostgresParserOptions;
|
|
14
|
-
}
|
|
15
10
|
export declare function restoreBackupSQL(opts: RestoreOptions): {
|
|
16
11
|
data: string[];
|
|
17
12
|
registry: string[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["engine.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAG7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AAK/C,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,qBAAqB,YAuCtE;AAED,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,qBAAqB,CAAC;CACjC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,cAAc,YAyB7C;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,cAAc,GAAG;IACpD,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,QAAQ,EAAE,MAAM,EAAE,CAAC;CACtB,CAuLA;AAED,MAAM,WAAW,oBAAqB,SAAQ,cAAc;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB;AACD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,oBAAoB,YAqB1D"}
|
|
@@ -43,7 +43,7 @@ function createFunctionRestoreSerial(opts) {
|
|
|
43
43
|
end if;
|
|
44
44
|
return next;
|
|
45
45
|
end;
|
|
46
|
-
|
|
46
|
+
$$;`,
|
|
47
47
|
], tab);
|
|
48
48
|
}
|
|
49
49
|
function lockTable(opts) {
|
|
@@ -68,10 +68,7 @@ function lockTable(opts) {
|
|
|
68
68
|
RAISE NOTICE 'TABLE ${schema}.${source} NOT FOUND, SKIPPING LOCK';
|
|
69
69
|
END IF;
|
|
70
70
|
END $$;`.trim();
|
|
71
|
-
return [
|
|
72
|
-
(0, notice_1.notice)(`PREPARING LOCK FOR MODEL ${modelName}`),
|
|
73
|
-
sql
|
|
74
|
-
];
|
|
71
|
+
return [(0, notice_1.notice)(`PREPARING LOCK FOR MODEL ${modelName}`), sql];
|
|
75
72
|
}
|
|
76
73
|
function restoreBackupSQL(opts) {
|
|
77
74
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7;
|
|
@@ -82,25 +79,47 @@ function restoreBackupSQL(opts) {
|
|
|
82
79
|
const temp = (0, escape_1.oid)(opts.model.temp);
|
|
83
80
|
if ((_b = (_a = opts.model.psm) === null || _a === void 0 ? void 0 : _a.backup) === null || _b === void 0 ? void 0 : _b.skip)
|
|
84
81
|
return null;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const columns =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
.
|
|
97
|
-
return `
|
|
82
|
+
// Filtra campos escalares não gerados (serão restaurados)
|
|
83
|
+
const filter = (field) => !field.isGenerated && field.kind === "scalar";
|
|
84
|
+
const fields = opts.model.fields.filter(filter);
|
|
85
|
+
// Lista de colunas para o INSERT (na mesma ordem dos campos)
|
|
86
|
+
const columns = fields.map((f) => ` ${(0, escape_1.oid)(f.name)}`).join(", ");
|
|
87
|
+
// Constrói a lista de expressões SELECT com CASE que verifica existência da chave no JSON original
|
|
88
|
+
const selectExpressions = fields
|
|
89
|
+
.map((field) => {
|
|
90
|
+
const colName = (0, escape_1.oid)(field.dbName || field.name);
|
|
91
|
+
const colNameLit = (0, escape_1.lit)(field.dbName || field.name);
|
|
92
|
+
// Obtém a expressão default do modelo (assumindo que reflete o schema do banco)
|
|
93
|
+
const defaultValue = field.default !== undefined ? field.default : "NULL";
|
|
94
|
+
return `
|
|
95
|
+
CASE
|
|
96
|
+
WHEN original_json ? ${colNameLit} THEN s.${colName}
|
|
97
|
+
ELSE ${defaultValue}
|
|
98
|
+
END AS ${colName}
|
|
99
|
+
`.trim();
|
|
98
100
|
})
|
|
101
|
+
.join(",\n");
|
|
102
|
+
// Nova DEFAULT_QUERY com CTE que inclui o JSON original e aplica CASE para cada coluna
|
|
103
|
+
const DEFAULT_QUERY = `
|
|
104
|
+
WITH __source AS (
|
|
105
|
+
SELECT
|
|
106
|
+
to_jsonb(_t) AS original_json,
|
|
107
|
+
(jsonb_populate_record(null::${shadow}.${temp}, to_jsonb(_t))).*
|
|
108
|
+
FROM ${schema}.${source} _t
|
|
109
|
+
)
|
|
110
|
+
SELECT
|
|
111
|
+
${selectExpressions}
|
|
112
|
+
FROM __source s
|
|
113
|
+
`;
|
|
114
|
+
const DEFAULT_SOURCE_CHECKER = `select 1 from pg_catalog.pg_tables t where t.tablename = ${(0, escape_1.lit)(opts.model.name)} and t.schemaname = ${(0, escape_1.lit)(opts.model.schema)}`;
|
|
115
|
+
const DEFAULT_WHEN = `true`;
|
|
116
|
+
const DEFAULT_RESOLVER = fields
|
|
117
|
+
.map((f) => ` ${(0, escape_1.oid)(f.dbName || f.name)}`)
|
|
99
118
|
.join(", ");
|
|
100
119
|
let source_exists = DEFAULT_SOURCE_CHECKER;
|
|
101
120
|
let when = DEFAULT_WHEN;
|
|
102
|
-
const revision_resolver =
|
|
103
|
-
.map(field => {
|
|
121
|
+
const revision_resolver = fields
|
|
122
|
+
.map((field) => {
|
|
104
123
|
var _a, _b;
|
|
105
124
|
let expression = (_b = (_a = field.psm) === null || _a === void 0 ? void 0 : _a.restore) === null || _b === void 0 ? void 0 : _b.expression;
|
|
106
125
|
if (!expression)
|
|
@@ -111,29 +130,36 @@ function restoreBackupSQL(opts) {
|
|
|
111
130
|
let revision_query = DEFAULT_QUERY;
|
|
112
131
|
const expression = (_e = (_d = (_c = opts.model.psm) === null || _c === void 0 ? void 0 : _c.backup) === null || _d === void 0 ? void 0 : _d.rev) === null || _e === void 0 ? void 0 : _e.expression;
|
|
113
132
|
const exists = (_h = (_g = (_f = opts.model.psm) === null || _f === void 0 ? void 0 : _f.backup) === null || _g === void 0 ? void 0 : _g.rev) === null || _h === void 0 ? void 0 : _h.exists;
|
|
114
|
-
if (
|
|
133
|
+
if (exists === null || exists === void 0 ? void 0 : exists.length) {
|
|
115
134
|
source_exists = exists;
|
|
116
135
|
}
|
|
117
|
-
if (((_l = (_k = (_j = opts.model.psm) === null || _j === void 0 ? void 0 : _j.backup) === null || _k === void 0 ? void 0 : _k.rev) === null || _l === void 0 ? void 0 : _l.from) === "query"
|
|
118
|
-
|
|
136
|
+
if (((_l = (_k = (_j = opts.model.psm) === null || _j === void 0 ? void 0 : _j.backup) === null || _k === void 0 ? void 0 : _k.rev) === null || _l === void 0 ? void 0 : _l.from) === "query" &&
|
|
137
|
+
expression) {
|
|
119
138
|
revision_query = expression;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
&&
|
|
139
|
+
}
|
|
140
|
+
else if (((_p = (_o = (_m = opts.model.psm) === null || _m === void 0 ? void 0 : _m.backup) === null || _o === void 0 ? void 0 : _o.rev) === null || _p === void 0 ? void 0 : _p.from) === "query:linked" &&
|
|
141
|
+
expression &&
|
|
142
|
+
((_r = (_q = opts.model.psm) === null || _q === void 0 ? void 0 : _q.query) === null || _r === void 0 ? void 0 : _r[expression])) {
|
|
123
143
|
revision_query = (_t = (_s = opts.model.psm) === null || _s === void 0 ? void 0 : _s.query) === null || _t === void 0 ? void 0 : _t[expression];
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
144
|
+
}
|
|
145
|
+
else if (((_w = (_v = (_u = opts.model.psm) === null || _u === void 0 ? void 0 : _u.backup) === null || _v === void 0 ? void 0 : _v.rev) === null || _w === void 0 ? void 0 : _w.from) === "model" &&
|
|
146
|
+
expression) {
|
|
147
|
+
const model = opts.parser.models.find((m) => m.model === expression);
|
|
148
|
+
if (model) {
|
|
127
149
|
revision_query = `select * from ${(0, escape_1.oid)(model.schema || "public")}.${(0, escape_1.oid)(model.dbName || model.name)}`;
|
|
150
|
+
}
|
|
128
151
|
}
|
|
129
152
|
const sys = (0, escape_1.oid)(opts.parser.sys);
|
|
130
153
|
let revision = "null";
|
|
131
154
|
const relation = `${schema}.${table}`;
|
|
132
|
-
if ((_z = (_y = (_x = opts.model.psm) === null || _x === void 0 ? void 0 : _x.backup) === null || _y === void 0 ? void 0 : _y.rev) === null || _z === void 0 ? void 0 : _z.version)
|
|
155
|
+
if ((_z = (_y = (_x = opts.model.psm) === null || _x === void 0 ? void 0 : _x.backup) === null || _y === void 0 ? void 0 : _y.rev) === null || _z === void 0 ? void 0 : _z.version) {
|
|
133
156
|
revision = (0, escape_1.lit)((_1 = (_0 = opts.model.psm) === null || _0 === void 0 ? void 0 : _0.backup) === null || _1 === void 0 ? void 0 : _1.rev.version);
|
|
157
|
+
}
|
|
134
158
|
let always_query = DEFAULT_QUERY;
|
|
135
159
|
let always_resolver = DEFAULT_RESOLVER;
|
|
136
|
-
if (((_4 = (_3 = (_2 = opts.model.psm) === null || _2 === void 0 ? void 0 : _2.backup) === null || _3 === void 0 ? void 0 : _3.rev) === null || _4 === void 0 ? void 0 : _4.apply) === "ALWAYS" &&
|
|
160
|
+
if (((_4 = (_3 = (_2 = opts.model.psm) === null || _2 === void 0 ? void 0 : _2.backup) === null || _3 === void 0 ? void 0 : _3.rev) === null || _4 === void 0 ? void 0 : _4.apply) === "ALWAYS" &&
|
|
161
|
+
revision_query &&
|
|
162
|
+
revision_resolver) {
|
|
137
163
|
always_query = revision_query;
|
|
138
164
|
always_resolver = revision_resolver;
|
|
139
165
|
revision = `always-${opts.parser.migration}`;
|
|
@@ -180,12 +206,8 @@ function restoreBackupSQL(opts) {
|
|
|
180
206
|
end;
|
|
181
207
|
$$;
|
|
182
208
|
`.split("\n")
|
|
183
|
-
.filter(
|
|
184
|
-
.map(
|
|
185
|
-
if (value.startsWith(" "))
|
|
186
|
-
return value.substring(" ".length);
|
|
187
|
-
return value;
|
|
188
|
-
})
|
|
209
|
+
.filter((line) => line.trim() !== "")
|
|
210
|
+
.map((line) => line.replace(/^ {6}/, "")) // Remove indentação de 6 espaços
|
|
189
211
|
.join("\n");
|
|
190
212
|
return {
|
|
191
213
|
data: [
|
|
@@ -199,7 +221,7 @@ function restoreBackupSQL(opts) {
|
|
|
199
221
|
revision: (_7 = (_6 = (_5 = opts.model.psm) === null || _5 === void 0 ? void 0 : _5.backup) === null || _6 === void 0 ? void 0 : _6.rev) === null || _7 === void 0 ? void 0 : _7.version,
|
|
200
222
|
relation: relation,
|
|
201
223
|
hash: (0, sha_1.migrationHash)(opts.parser.migration, `restore:data-${relation}`),
|
|
202
|
-
operation: `restore:data-${relation}
|
|
224
|
+
operation: `restore:data-${relation}`,
|
|
203
225
|
}).join("\n"),
|
|
204
226
|
(0, notice_1.notice)(`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model} OK`),
|
|
205
227
|
],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.js","sourceRoot":"","sources":["engine.ts"],"names":[],"mappings":";;AAQA,
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["engine.ts"],"names":[],"mappings":";;AAQA,kEAuCC;AAQD,8BAyBC;AAED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;QAChD,YAAY,IAAA,YAAG,EAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,gBAAO,CAAC,EAAE;QAC3C,cAAc,IAAA,YAAG,EAAC,IAAI,CAAC,IAAI,EAAE,gBAAO,CAAC,EAAE;QACvC,YAAY,IAAA,YAAG,EAAC,IAAI,CAAC,EAAE,EAAE,gBAAO,CAAC,EAAE;QACnC,aAAa,IAAA,YAAG,EAAC,IAAI,CAAC,GAAG,EAAE,gBAAO,CAAC,EAAE;KACxC,CAAC;IACF,OAAO;QACH,IAAA,eAAM,EACF,6BAA6B,IAAI,CAAC,EAAE,eAAe,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CACxE;QACD,iBAAiB,IAAA,YAAG,EAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAuB,IAAI,CAAC,IAAI,CAC7D,OAAO,CACd,MAAM;QACP,IAAA,eAAM,EACF,6BAA6B,IAAI,CAAC,EAAE,eAAe,IAAI,CAAC,KAAK,CAAC,KAAK,KAAK,CAC3E;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {FieldOption, ModelOptions} from "@prisma-psm/core";
|
|
2
|
-
import {oid, lit, VARCHAR} from "../../utils/escape";
|
|
1
|
+
import { FieldOption, ModelOptions } from "@prisma-psm/core";
|
|
2
|
+
import { oid, lit, VARCHAR } from "../../utils/escape";
|
|
3
3
|
import { noTab } from "../../utils/tabs";
|
|
4
4
|
import { PostgresParserOptions } from "../def";
|
|
5
|
-
import {notice} from "../notice";
|
|
6
|
-
import {createRevision} from "../sys";
|
|
7
|
-
import {migrationHash} from "../../utils/sha";
|
|
5
|
+
import { notice } from "../notice";
|
|
6
|
+
import { createRevision } from "../sys";
|
|
7
|
+
import { migrationHash } from "../../utils/sha";
|
|
8
8
|
|
|
9
|
-
export function createFunctionRestoreSerial(
|
|
10
|
-
const sys = oid(
|
|
11
|
-
const tab = " "
|
|
12
|
-
return noTab(
|
|
13
|
-
|
|
9
|
+
export function createFunctionRestoreSerial(opts: PostgresParserOptions) {
|
|
10
|
+
const sys = oid(opts.sys);
|
|
11
|
+
const tab = " ";
|
|
12
|
+
return noTab(
|
|
13
|
+
[
|
|
14
|
+
`create or replace function ${sys}.restore_serial(
|
|
14
15
|
schema character varying,
|
|
15
16
|
source character varying,
|
|
16
17
|
shadow character varying,
|
|
@@ -40,15 +41,16 @@ export function createFunctionRestoreSerial( opts: PostgresParserOptions) {
|
|
|
40
41
|
end if;
|
|
41
42
|
return next;
|
|
42
43
|
end;
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
$$;`,
|
|
45
|
+
],
|
|
46
|
+
tab
|
|
47
|
+
);
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
parser:PostgresParserOptions
|
|
50
|
+
export interface RestoreOptions {
|
|
51
|
+
source: string;
|
|
52
|
+
model: ModelOptions;
|
|
53
|
+
parser: PostgresParserOptions;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
export function lockTable(opts: RestoreOptions) {
|
|
@@ -75,101 +77,128 @@ export function lockTable(opts: RestoreOptions) {
|
|
|
75
77
|
END IF;
|
|
76
78
|
END $$;`.trim();
|
|
77
79
|
|
|
78
|
-
return [
|
|
79
|
-
notice(`PREPARING LOCK FOR MODEL ${modelName}`),
|
|
80
|
-
sql
|
|
81
|
-
];
|
|
80
|
+
return [notice(`PREPARING LOCK FOR MODEL ${modelName}`), sql];
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
export
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
export function restoreBackupSQL(opts: RestoreOptions): {
|
|
84
|
+
data: string[];
|
|
85
|
+
registry: string[];
|
|
86
|
+
} {
|
|
87
|
+
const schema = oid(opts.model.schema);
|
|
88
|
+
const source = oid(opts.source);
|
|
89
|
+
const shadow = oid(opts.parser.shadow);
|
|
90
|
+
const table = oid(opts.model.name);
|
|
91
|
+
const temp = oid(opts.model.temp);
|
|
89
92
|
|
|
90
|
-
|
|
91
|
-
data:string[]
|
|
92
|
-
registry:string[]
|
|
93
|
-
}{
|
|
94
|
-
const schema = oid(opts.model.schema);
|
|
95
|
-
const source = oid(opts.source);
|
|
96
|
-
const shadow = oid(opts.parser.shadow);
|
|
97
|
-
const table = oid(opts.model.name);
|
|
98
|
-
const temp = oid(opts.model.temp);
|
|
93
|
+
if (opts.model.psm?.backup?.skip) return null as any;
|
|
99
94
|
|
|
100
|
-
|
|
95
|
+
// Filtra campos escalares não gerados (serão restaurados)
|
|
96
|
+
const filter = (field: FieldOption) =>
|
|
97
|
+
!field.isGenerated && field.kind === "scalar";
|
|
98
|
+
const fields = opts.model.fields.filter(filter);
|
|
101
99
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
&& field.kind === "scalar"
|
|
105
|
-
;
|
|
106
|
-
}
|
|
100
|
+
// Lista de colunas para o INSERT (na mesma ordem dos campos)
|
|
101
|
+
const columns = fields.map((f) => ` ${oid(f.name)}`).join(", ");
|
|
107
102
|
|
|
103
|
+
// Constrói a lista de expressões SELECT com CASE que verifica existência da chave no JSON original
|
|
104
|
+
const selectExpressions = fields
|
|
105
|
+
.map((field) => {
|
|
106
|
+
const colName = oid(field.dbName || field.name);
|
|
107
|
+
const colNameLit = lit(field.dbName || field.name);
|
|
108
|
+
// Obtém a expressão default do modelo (assumindo que reflete o schema do banco)
|
|
109
|
+
const defaultValue = field.default !== undefined ? field.default : "NULL";
|
|
110
|
+
return `
|
|
111
|
+
CASE
|
|
112
|
+
WHEN original_json ? ${colNameLit} THEN s.${colName}
|
|
113
|
+
ELSE ${defaultValue}
|
|
114
|
+
END AS ${colName}
|
|
115
|
+
`.trim();
|
|
116
|
+
})
|
|
117
|
+
.join(",\n");
|
|
108
118
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
119
|
+
// Nova DEFAULT_QUERY com CTE que inclui o JSON original e aplica CASE para cada coluna
|
|
120
|
+
const DEFAULT_QUERY = `
|
|
121
|
+
WITH __source AS (
|
|
122
|
+
SELECT
|
|
123
|
+
to_jsonb(_t) AS original_json,
|
|
124
|
+
(jsonb_populate_record(null::${shadow}.${temp}, to_jsonb(_t))).*
|
|
125
|
+
FROM ${schema}.${source} _t
|
|
126
|
+
)
|
|
127
|
+
SELECT
|
|
128
|
+
${selectExpressions}
|
|
129
|
+
FROM __source s
|
|
130
|
+
`;
|
|
113
131
|
|
|
132
|
+
const DEFAULT_SOURCE_CHECKER = `select 1 from pg_catalog.pg_tables t where t.tablename = ${lit(
|
|
133
|
+
opts.model.name
|
|
134
|
+
)} and t.schemaname = ${lit(opts.model.schema)}`;
|
|
114
135
|
const DEFAULT_WHEN = `true`;
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
.map( value => {
|
|
119
|
-
return ` ${oid(value.dbName||value.name)}`;
|
|
120
|
-
})
|
|
121
|
-
.join(", ")
|
|
122
|
-
;
|
|
136
|
+
const DEFAULT_RESOLVER = fields
|
|
137
|
+
.map((f) => ` ${oid(f.dbName || f.name)}`)
|
|
138
|
+
.join(", ");
|
|
123
139
|
|
|
124
140
|
let source_exists = DEFAULT_SOURCE_CHECKER;
|
|
125
141
|
let when = DEFAULT_WHEN;
|
|
126
142
|
|
|
127
|
-
const revision_resolver =
|
|
128
|
-
.map(
|
|
129
|
-
|
|
143
|
+
const revision_resolver = fields
|
|
144
|
+
.map((field) => {
|
|
130
145
|
let expression = field.psm?.restore?.expression;
|
|
131
|
-
if(
|
|
146
|
+
if (!expression) expression = ` ${oid(field.dbName || field.name)}`;
|
|
132
147
|
return expression;
|
|
133
148
|
})
|
|
134
|
-
.join(", ")
|
|
135
|
-
;
|
|
149
|
+
.join(", ");
|
|
136
150
|
|
|
137
151
|
let revision_query = DEFAULT_QUERY;
|
|
138
152
|
const expression = opts.model.psm?.backup?.rev?.expression;
|
|
139
153
|
const exists = opts.model.psm?.backup?.rev?.exists;
|
|
140
154
|
|
|
141
|
-
if(
|
|
155
|
+
if (exists?.length) {
|
|
142
156
|
source_exists = exists;
|
|
143
157
|
}
|
|
144
158
|
|
|
145
|
-
if(
|
|
146
|
-
&&
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
159
|
+
if (
|
|
160
|
+
opts.model.psm?.backup?.rev?.from === "query" &&
|
|
161
|
+
expression
|
|
162
|
+
) {
|
|
163
|
+
revision_query = expression;
|
|
164
|
+
} else if (
|
|
165
|
+
opts.model.psm?.backup?.rev?.from === "query:linked" &&
|
|
166
|
+
expression &&
|
|
167
|
+
opts.model.psm?.query?.[expression]
|
|
168
|
+
) {
|
|
169
|
+
revision_query = opts.model.psm?.query?.[expression];
|
|
170
|
+
} else if (
|
|
171
|
+
opts.model.psm?.backup?.rev?.from === "model" &&
|
|
172
|
+
expression
|
|
173
|
+
) {
|
|
174
|
+
const model = opts.parser.models.find((m) => m.model === expression);
|
|
175
|
+
if (model) {
|
|
176
|
+
revision_query = `select * from ${oid(
|
|
177
|
+
model.schema || "public"
|
|
178
|
+
)}.${oid(model.dbName || model.name)}`;
|
|
179
|
+
}
|
|
157
180
|
}
|
|
158
181
|
|
|
159
|
-
const sys = oid(
|
|
160
|
-
let revision= "null";
|
|
182
|
+
const sys = oid(opts.parser.sys);
|
|
183
|
+
let revision = "null";
|
|
161
184
|
const relation = `${schema}.${table}`;
|
|
162
|
-
if(
|
|
185
|
+
if (opts.model.psm?.backup?.rev?.version) {
|
|
186
|
+
revision = lit(opts.model.psm?.backup?.rev.version);
|
|
187
|
+
}
|
|
163
188
|
|
|
164
189
|
let always_query = DEFAULT_QUERY;
|
|
165
190
|
let always_resolver = DEFAULT_RESOLVER;
|
|
166
|
-
if(
|
|
191
|
+
if (
|
|
192
|
+
opts.model.psm?.backup?.rev?.apply === "ALWAYS" &&
|
|
193
|
+
revision_query &&
|
|
194
|
+
revision_resolver
|
|
195
|
+
) {
|
|
167
196
|
always_query = revision_query;
|
|
168
197
|
always_resolver = revision_resolver;
|
|
169
198
|
revision = `always-${opts.parser.migration}`;
|
|
170
199
|
}
|
|
171
200
|
|
|
172
|
-
const next
|
|
201
|
+
const next = `
|
|
173
202
|
do $$
|
|
174
203
|
declare
|
|
175
204
|
_revision character varying := ${revision}::character varying;
|
|
@@ -194,14 +223,14 @@ export function restoreBackupSQL(opts:RestoreOptions ): {
|
|
|
194
223
|
) then
|
|
195
224
|
with __restore as (
|
|
196
225
|
${revision_query}
|
|
197
|
-
) insert into ${shadow}.${temp} (${
|
|
226
|
+
) insert into ${shadow}.${temp} (${columns})
|
|
198
227
|
select
|
|
199
228
|
${revision_resolver}
|
|
200
229
|
from __restore r;
|
|
201
230
|
elsif _revision is null then
|
|
202
231
|
with __restore as (
|
|
203
232
|
${always_query}
|
|
204
|
-
) insert into ${shadow}.${temp} (${
|
|
233
|
+
) insert into ${shadow}.${temp} (${columns})
|
|
205
234
|
select
|
|
206
235
|
${always_resolver}
|
|
207
236
|
from __restore r;
|
|
@@ -211,38 +240,40 @@ export function restoreBackupSQL(opts:RestoreOptions ): {
|
|
|
211
240
|
end;
|
|
212
241
|
$$;
|
|
213
242
|
`.split("\n")
|
|
214
|
-
.filter(
|
|
215
|
-
.map(
|
|
216
|
-
if( value.startsWith(" ") ) return value.substring(" ".length)
|
|
217
|
-
return value;
|
|
218
|
-
})
|
|
243
|
+
.filter((line) => line.trim() !== "")
|
|
244
|
+
.map((line) => line.replace(/^ {6}/, "")) // Remove indentação de 6 espaços
|
|
219
245
|
.join("\n");
|
|
220
246
|
|
|
221
247
|
return {
|
|
222
248
|
data: [
|
|
223
|
-
notice(
|
|
249
|
+
notice(`RESTORE BACKUP FOR MODEL ${opts.model.model}`),
|
|
224
250
|
next,
|
|
225
|
-
notice(
|
|
251
|
+
notice(`RESTORE BACKUP FOR MODEL ${opts.model.model} OK`),
|
|
226
252
|
],
|
|
227
253
|
registry: [
|
|
228
|
-
notice(
|
|
229
|
-
createRevision(
|
|
254
|
+
notice(`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model}`),
|
|
255
|
+
createRevision(opts.parser, {
|
|
230
256
|
revision: opts.model.psm?.backup?.rev?.version,
|
|
231
257
|
relation: relation,
|
|
232
|
-
hash: migrationHash(
|
|
233
|
-
|
|
258
|
+
hash: migrationHash(
|
|
259
|
+
opts.parser.migration,
|
|
260
|
+
`restore:data-${relation}`
|
|
261
|
+
),
|
|
262
|
+
operation: `restore:data-${relation}`,
|
|
234
263
|
}).join("\n"),
|
|
235
|
-
notice(
|
|
264
|
+
notice(
|
|
265
|
+
`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model} OK`
|
|
266
|
+
),
|
|
236
267
|
],
|
|
237
|
-
}
|
|
268
|
+
};
|
|
238
269
|
}
|
|
239
270
|
|
|
240
271
|
export interface RestoreSerialOptions extends RestoreOptions {
|
|
241
|
-
from:string
|
|
242
|
-
to:string
|
|
243
|
-
seq?:string
|
|
272
|
+
from: string;
|
|
273
|
+
to: string;
|
|
274
|
+
seq?: string;
|
|
244
275
|
}
|
|
245
|
-
export function restoreSerialSQL(
|
|
276
|
+
export function restoreSerialSQL(opts: RestoreSerialOptions) {
|
|
246
277
|
const args = [
|
|
247
278
|
` schema := ${lit(opts.model.schema, VARCHAR)}`,
|
|
248
279
|
` source := ${lit(opts.source, VARCHAR)}`,
|
|
@@ -250,13 +281,17 @@ export function restoreSerialSQL( opts:RestoreSerialOptions) {
|
|
|
250
281
|
` temp := ${lit(opts.model.temp, VARCHAR)}`,
|
|
251
282
|
` "from" := ${lit(opts.from, VARCHAR)}`,
|
|
252
283
|
` "to" := ${lit(opts.to, VARCHAR)}`,
|
|
253
|
-
` "seq" := ${lit(
|
|
284
|
+
` "seq" := ${lit(opts.seq, VARCHAR)}`,
|
|
254
285
|
];
|
|
255
286
|
return [
|
|
256
|
-
notice(
|
|
257
|
-
|
|
258
|
-
|
|
287
|
+
notice(
|
|
288
|
+
`RESTORE SEQUENCE OF FIELD ${opts.to} FROM MODEL ${opts.model.model}`
|
|
289
|
+
),
|
|
290
|
+
`select * from ${oid(opts.parser.sys)}.restore_serial(\n ${args.join(
|
|
291
|
+
",\n "
|
|
292
|
+
)}\n);`,
|
|
293
|
+
notice(
|
|
294
|
+
`RESTORE SEQUENCE OF FIELD ${opts.to} FROM MODEL ${opts.model.model} OK`
|
|
295
|
+
),
|
|
259
296
|
];
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
297
|
+
}
|