@prisma-psm/pg 1.0.45 → 1.0.47
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,CAkLA;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,41 @@ 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
|
-
|
|
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 COALESCE para cada campo
|
|
88
|
+
const selectExpressions = fields
|
|
89
|
+
.map((field) => {
|
|
90
|
+
const colName = (0, escape_1.oid)(field.dbName || field.name);
|
|
91
|
+
// Obtém a expressão default da coluna, se houver; caso contrário, usa NULL
|
|
92
|
+
// Nota: assumindo que field.default contém a expressão SQL do default
|
|
93
|
+
const defaultValue = field.default !== undefined ? field.default : "NULL";
|
|
94
|
+
// Gera: COALESCE(s."colName", defaultExpression) AS "colName"
|
|
95
|
+
return `COALESCE(s.${colName}, ${defaultValue}) AS ${colName}`;
|
|
98
96
|
})
|
|
97
|
+
.join(",\n");
|
|
98
|
+
// Nova DEFAULT_QUERY com CTE e COALESCE para cada coluna
|
|
99
|
+
const DEFAULT_QUERY = `
|
|
100
|
+
WITH __source AS (
|
|
101
|
+
SELECT (jsonb_populate_record(null::${shadow}.${temp}, to_jsonb(_t))).*
|
|
102
|
+
FROM ${schema}.${source} _t
|
|
103
|
+
)
|
|
104
|
+
SELECT
|
|
105
|
+
${selectExpressions}
|
|
106
|
+
FROM __source s
|
|
107
|
+
`;
|
|
108
|
+
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)}`;
|
|
109
|
+
const DEFAULT_WHEN = `true`;
|
|
110
|
+
const DEFAULT_RESOLVER = fields
|
|
111
|
+
.map((f) => ` ${(0, escape_1.oid)(f.dbName || f.name)}`)
|
|
99
112
|
.join(", ");
|
|
100
113
|
let source_exists = DEFAULT_SOURCE_CHECKER;
|
|
101
114
|
let when = DEFAULT_WHEN;
|
|
102
|
-
const revision_resolver =
|
|
103
|
-
.map(field => {
|
|
115
|
+
const revision_resolver = fields
|
|
116
|
+
.map((field) => {
|
|
104
117
|
var _a, _b;
|
|
105
118
|
let expression = (_b = (_a = field.psm) === null || _a === void 0 ? void 0 : _a.restore) === null || _b === void 0 ? void 0 : _b.expression;
|
|
106
119
|
if (!expression)
|
|
@@ -111,29 +124,36 @@ function restoreBackupSQL(opts) {
|
|
|
111
124
|
let revision_query = DEFAULT_QUERY;
|
|
112
125
|
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
126
|
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 (
|
|
127
|
+
if (exists === null || exists === void 0 ? void 0 : exists.length) {
|
|
115
128
|
source_exists = exists;
|
|
116
129
|
}
|
|
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
|
-
|
|
130
|
+
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" &&
|
|
131
|
+
expression) {
|
|
119
132
|
revision_query = expression;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
&&
|
|
133
|
+
}
|
|
134
|
+
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" &&
|
|
135
|
+
expression &&
|
|
136
|
+
((_r = (_q = opts.model.psm) === null || _q === void 0 ? void 0 : _q.query) === null || _r === void 0 ? void 0 : _r[expression])) {
|
|
123
137
|
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
|
-
|
|
138
|
+
}
|
|
139
|
+
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" &&
|
|
140
|
+
expression) {
|
|
141
|
+
const model = opts.parser.models.find((m) => m.model === expression);
|
|
142
|
+
if (model) {
|
|
127
143
|
revision_query = `select * from ${(0, escape_1.oid)(model.schema || "public")}.${(0, escape_1.oid)(model.dbName || model.name)}`;
|
|
144
|
+
}
|
|
128
145
|
}
|
|
129
146
|
const sys = (0, escape_1.oid)(opts.parser.sys);
|
|
130
147
|
let revision = "null";
|
|
131
148
|
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)
|
|
149
|
+
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
150
|
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);
|
|
151
|
+
}
|
|
134
152
|
let always_query = DEFAULT_QUERY;
|
|
135
153
|
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" &&
|
|
154
|
+
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" &&
|
|
155
|
+
revision_query &&
|
|
156
|
+
revision_resolver) {
|
|
137
157
|
always_query = revision_query;
|
|
138
158
|
always_resolver = revision_resolver;
|
|
139
159
|
revision = `always-${opts.parser.migration}`;
|
|
@@ -180,12 +200,8 @@ function restoreBackupSQL(opts) {
|
|
|
180
200
|
end;
|
|
181
201
|
$$;
|
|
182
202
|
`.split("\n")
|
|
183
|
-
.filter(
|
|
184
|
-
.map(
|
|
185
|
-
if (value.startsWith(" "))
|
|
186
|
-
return value.substring(" ".length);
|
|
187
|
-
return value;
|
|
188
|
-
})
|
|
203
|
+
.filter((line) => line.trim() !== "")
|
|
204
|
+
.map((line) => line.replace(/^ {6}/, "")) // Remove indentação de 6 espaços
|
|
189
205
|
.join("\n");
|
|
190
206
|
return {
|
|
191
207
|
data: [
|
|
@@ -199,7 +215,7 @@ function restoreBackupSQL(opts) {
|
|
|
199
215
|
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
216
|
relation: relation,
|
|
201
217
|
hash: (0, sha_1.migrationHash)(opts.parser.migration, `restore:data-${relation}`),
|
|
202
|
-
operation: `restore:data-${relation}
|
|
218
|
+
operation: `restore:data-${relation}`,
|
|
203
219
|
}).join("\n"),
|
|
204
220
|
(0, notice_1.notice)(`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model} OK`),
|
|
205
221
|
],
|
|
@@ -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,4CAqLC;AAOD,4CAqBC;AAlSD,+CAAuD;AACvD,2CAAyC;AAEzC,sCAAmC;AACnC,gCAAwC;AACxC,yCAAgD;AAEhD,SAAgB,2BAA2B,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AC,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,CACjE,OAAO,CACV,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,123 @@ 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 COALESCE para cada campo
|
|
104
|
+
const selectExpressions = fields
|
|
105
|
+
.map((field) => {
|
|
106
|
+
const colName = oid(field.dbName || field.name);
|
|
107
|
+
// Obtém a expressão default da coluna, se houver; caso contrário, usa NULL
|
|
108
|
+
// Nota: assumindo que field.default contém a expressão SQL do default
|
|
109
|
+
const defaultValue =
|
|
110
|
+
field.default !== undefined ? field.default : "NULL";
|
|
111
|
+
// Gera: COALESCE(s."colName", defaultExpression) AS "colName"
|
|
112
|
+
return `COALESCE(s.${colName}, ${defaultValue}) AS ${colName}`;
|
|
113
|
+
})
|
|
114
|
+
.join(",\n");
|
|
108
115
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
// Nova DEFAULT_QUERY com CTE e COALESCE para cada coluna
|
|
117
|
+
const DEFAULT_QUERY = `
|
|
118
|
+
WITH __source AS (
|
|
119
|
+
SELECT (jsonb_populate_record(null::${shadow}.${temp}, to_jsonb(_t))).*
|
|
120
|
+
FROM ${schema}.${source} _t
|
|
121
|
+
)
|
|
122
|
+
SELECT
|
|
123
|
+
${selectExpressions}
|
|
124
|
+
FROM __source s
|
|
125
|
+
`;
|
|
113
126
|
|
|
127
|
+
const DEFAULT_SOURCE_CHECKER = `select 1 from pg_catalog.pg_tables t where t.tablename = ${lit(
|
|
128
|
+
opts.model.name
|
|
129
|
+
)} and t.schemaname = ${lit(opts.model.schema)}`;
|
|
114
130
|
const DEFAULT_WHEN = `true`;
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
.map( value => {
|
|
119
|
-
return ` ${oid(value.dbName||value.name)}`;
|
|
120
|
-
})
|
|
121
|
-
.join(", ")
|
|
122
|
-
;
|
|
131
|
+
const DEFAULT_RESOLVER = fields
|
|
132
|
+
.map((f) => ` ${oid(f.dbName || f.name)}`)
|
|
133
|
+
.join(", ");
|
|
123
134
|
|
|
124
135
|
let source_exists = DEFAULT_SOURCE_CHECKER;
|
|
125
136
|
let when = DEFAULT_WHEN;
|
|
126
137
|
|
|
127
|
-
const revision_resolver =
|
|
128
|
-
.map(
|
|
129
|
-
|
|
138
|
+
const revision_resolver = fields
|
|
139
|
+
.map((field) => {
|
|
130
140
|
let expression = field.psm?.restore?.expression;
|
|
131
|
-
if(
|
|
141
|
+
if (!expression) expression = ` ${oid(field.dbName || field.name)}`;
|
|
132
142
|
return expression;
|
|
133
143
|
})
|
|
134
|
-
.join(", ")
|
|
135
|
-
;
|
|
144
|
+
.join(", ");
|
|
136
145
|
|
|
137
146
|
let revision_query = DEFAULT_QUERY;
|
|
138
147
|
const expression = opts.model.psm?.backup?.rev?.expression;
|
|
139
148
|
const exists = opts.model.psm?.backup?.rev?.exists;
|
|
140
149
|
|
|
141
|
-
if(
|
|
150
|
+
if (exists?.length) {
|
|
142
151
|
source_exists = exists;
|
|
143
152
|
}
|
|
144
153
|
|
|
145
|
-
if(
|
|
146
|
-
&&
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
154
|
+
if (
|
|
155
|
+
opts.model.psm?.backup?.rev?.from === "query" &&
|
|
156
|
+
expression
|
|
157
|
+
) {
|
|
158
|
+
revision_query = expression;
|
|
159
|
+
} else if (
|
|
160
|
+
opts.model.psm?.backup?.rev?.from === "query:linked" &&
|
|
161
|
+
expression &&
|
|
162
|
+
opts.model.psm?.query?.[expression]
|
|
163
|
+
) {
|
|
164
|
+
revision_query = opts.model.psm?.query?.[expression];
|
|
165
|
+
} else if (
|
|
166
|
+
opts.model.psm?.backup?.rev?.from === "model" &&
|
|
167
|
+
expression
|
|
168
|
+
) {
|
|
169
|
+
const model = opts.parser.models.find((m) => m.model === expression);
|
|
170
|
+
if (model) {
|
|
171
|
+
revision_query = `select * from ${oid(
|
|
172
|
+
model.schema || "public"
|
|
173
|
+
)}.${oid(model.dbName || model.name)}`;
|
|
174
|
+
}
|
|
157
175
|
}
|
|
158
176
|
|
|
159
|
-
const sys = oid(
|
|
160
|
-
let revision= "null";
|
|
177
|
+
const sys = oid(opts.parser.sys);
|
|
178
|
+
let revision = "null";
|
|
161
179
|
const relation = `${schema}.${table}`;
|
|
162
|
-
if(
|
|
180
|
+
if (opts.model.psm?.backup?.rev?.version) {
|
|
181
|
+
revision = lit(opts.model.psm?.backup?.rev.version);
|
|
182
|
+
}
|
|
163
183
|
|
|
164
184
|
let always_query = DEFAULT_QUERY;
|
|
165
185
|
let always_resolver = DEFAULT_RESOLVER;
|
|
166
|
-
if(
|
|
186
|
+
if (
|
|
187
|
+
opts.model.psm?.backup?.rev?.apply === "ALWAYS" &&
|
|
188
|
+
revision_query &&
|
|
189
|
+
revision_resolver
|
|
190
|
+
) {
|
|
167
191
|
always_query = revision_query;
|
|
168
192
|
always_resolver = revision_resolver;
|
|
169
193
|
revision = `always-${opts.parser.migration}`;
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
const next
|
|
196
|
+
const next = `
|
|
173
197
|
do $$
|
|
174
198
|
declare
|
|
175
199
|
_revision character varying := ${revision}::character varying;
|
|
@@ -194,14 +218,14 @@ export function restoreBackupSQL(opts:RestoreOptions ): {
|
|
|
194
218
|
) then
|
|
195
219
|
with __restore as (
|
|
196
220
|
${revision_query}
|
|
197
|
-
) insert into ${shadow}.${temp} (${
|
|
221
|
+
) insert into ${shadow}.${temp} (${columns})
|
|
198
222
|
select
|
|
199
223
|
${revision_resolver}
|
|
200
224
|
from __restore r;
|
|
201
225
|
elsif _revision is null then
|
|
202
226
|
with __restore as (
|
|
203
227
|
${always_query}
|
|
204
|
-
) insert into ${shadow}.${temp} (${
|
|
228
|
+
) insert into ${shadow}.${temp} (${columns})
|
|
205
229
|
select
|
|
206
230
|
${always_resolver}
|
|
207
231
|
from __restore r;
|
|
@@ -211,38 +235,40 @@ export function restoreBackupSQL(opts:RestoreOptions ): {
|
|
|
211
235
|
end;
|
|
212
236
|
$$;
|
|
213
237
|
`.split("\n")
|
|
214
|
-
.filter(
|
|
215
|
-
.map(
|
|
216
|
-
if( value.startsWith(" ") ) return value.substring(" ".length)
|
|
217
|
-
return value;
|
|
218
|
-
})
|
|
238
|
+
.filter((line) => line.trim() !== "")
|
|
239
|
+
.map((line) => line.replace(/^ {6}/, "")) // Remove indentação de 6 espaços
|
|
219
240
|
.join("\n");
|
|
220
241
|
|
|
221
242
|
return {
|
|
222
243
|
data: [
|
|
223
|
-
notice(
|
|
244
|
+
notice(`RESTORE BACKUP FOR MODEL ${opts.model.model}`),
|
|
224
245
|
next,
|
|
225
|
-
notice(
|
|
246
|
+
notice(`RESTORE BACKUP FOR MODEL ${opts.model.model} OK`),
|
|
226
247
|
],
|
|
227
248
|
registry: [
|
|
228
|
-
notice(
|
|
229
|
-
createRevision(
|
|
249
|
+
notice(`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model}`),
|
|
250
|
+
createRevision(opts.parser, {
|
|
230
251
|
revision: opts.model.psm?.backup?.rev?.version,
|
|
231
252
|
relation: relation,
|
|
232
|
-
hash: migrationHash(
|
|
233
|
-
|
|
253
|
+
hash: migrationHash(
|
|
254
|
+
opts.parser.migration,
|
|
255
|
+
`restore:data-${relation}`
|
|
256
|
+
),
|
|
257
|
+
operation: `restore:data-${relation}`,
|
|
234
258
|
}).join("\n"),
|
|
235
|
-
notice(
|
|
259
|
+
notice(
|
|
260
|
+
`REGISTRY RESTORE OF BACKUP FOR MODEL ${opts.model.model} OK`
|
|
261
|
+
),
|
|
236
262
|
],
|
|
237
|
-
}
|
|
263
|
+
};
|
|
238
264
|
}
|
|
239
265
|
|
|
240
266
|
export interface RestoreSerialOptions extends RestoreOptions {
|
|
241
|
-
from:string
|
|
242
|
-
to:string
|
|
243
|
-
seq?:string
|
|
267
|
+
from: string;
|
|
268
|
+
to: string;
|
|
269
|
+
seq?: string;
|
|
244
270
|
}
|
|
245
|
-
export function restoreSerialSQL(
|
|
271
|
+
export function restoreSerialSQL(opts: RestoreSerialOptions) {
|
|
246
272
|
const args = [
|
|
247
273
|
` schema := ${lit(opts.model.schema, VARCHAR)}`,
|
|
248
274
|
` source := ${lit(opts.source, VARCHAR)}`,
|
|
@@ -250,13 +276,17 @@ export function restoreSerialSQL( opts:RestoreSerialOptions) {
|
|
|
250
276
|
` temp := ${lit(opts.model.temp, VARCHAR)}`,
|
|
251
277
|
` "from" := ${lit(opts.from, VARCHAR)}`,
|
|
252
278
|
` "to" := ${lit(opts.to, VARCHAR)}`,
|
|
253
|
-
` "seq" := ${lit(
|
|
279
|
+
` "seq" := ${lit(opts.seq, VARCHAR)}`,
|
|
254
280
|
];
|
|
255
281
|
return [
|
|
256
|
-
notice(
|
|
257
|
-
|
|
258
|
-
|
|
282
|
+
notice(
|
|
283
|
+
`RESTORE SEQUENCE OF FIELD ${opts.to} FROM MODEL ${opts.model.model}`
|
|
284
|
+
),
|
|
285
|
+
`select * from ${oid(opts.parser.sys)}.restore_serial(\n ${args.join(
|
|
286
|
+
",\n "
|
|
287
|
+
)}\n);`,
|
|
288
|
+
notice(
|
|
289
|
+
`RESTORE SEQUENCE OF FIELD ${opts.to} FROM MODEL ${opts.model.model} OK`
|
|
290
|
+
),
|
|
259
291
|
];
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
292
|
+
}
|