@prisma-psm/core 1.0.54 → 1.0.55
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 +1 -1
- package/src/tools/commit.d.ts.map +1 -1
- package/src/tools/commit.js +35 -10
- package/src/tools/commit.js.map +1 -1
- package/src/tools/commit.ts +111 -81
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commit.d.ts","sourceRoot":"","sources":["commit.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"commit.d.ts","sourceRoot":"","sources":["commit.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,cAAc;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,CAAC;CAC9B;AAED,wBAAsB,MAAM,CAAC,IAAI,EAAE,cAAc,iBAsKhD"}
|
package/src/tools/commit.js
CHANGED
|
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.commit = commit;
|
|
40
40
|
const fs = __importStar(require("node:fs"));
|
|
41
41
|
const Path = __importStar(require("node:path"));
|
|
42
|
+
const os = __importStar(require("node:os")); // <-- adicionado para diretório temporário
|
|
42
43
|
const yaml = __importStar(require("yaml"));
|
|
43
44
|
const tar = __importStar(require("tar"));
|
|
44
45
|
const chalk_1 = __importDefault(require("chalk"));
|
|
@@ -49,6 +50,7 @@ const fs_1 = require("../utils/fs");
|
|
|
49
50
|
const execute_1 = require("./execute");
|
|
50
51
|
function commit(opts) {
|
|
51
52
|
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
var _a;
|
|
52
54
|
require('dotenv').config();
|
|
53
55
|
const moment = require('moment');
|
|
54
56
|
if (opts.generate) {
|
|
@@ -56,22 +58,45 @@ function commit(opts) {
|
|
|
56
58
|
if (!command)
|
|
57
59
|
command = "prisma generate";
|
|
58
60
|
cp.spawnSync("npx", [...command.split(" ")], {
|
|
59
|
-
cwd: process.cwd()
|
|
61
|
+
cwd: process.cwd(),
|
|
60
62
|
});
|
|
61
63
|
}
|
|
62
64
|
const { psm, psm_sql, driver, home } = yield (0, common_1.psmLockup)({ schema: opts.schema });
|
|
63
65
|
const next = Path.join(psm.psm.output, "next/migration.next.sql");
|
|
64
66
|
const check = Path.join(psm.psm.output, "next/migration.next.check.sql");
|
|
67
|
+
// -------------------- Leitura do preview corrigida --------------------
|
|
65
68
|
let preview;
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
const revisionsDir = Path.join(home, "psm/revisions/schema");
|
|
70
|
+
const lastFile = (0, fs_1.getLatestFolder)(revisionsDir);
|
|
71
|
+
if (lastFile) {
|
|
72
|
+
const tarPath = Path.join(revisionsDir, lastFile);
|
|
73
|
+
const tempDir = fs.mkdtempSync(Path.join(os.tmpdir(), "psm-preview-"));
|
|
74
|
+
try {
|
|
75
|
+
yield tar.x({
|
|
76
|
+
file: tarPath,
|
|
77
|
+
cwd: tempDir,
|
|
78
|
+
});
|
|
79
|
+
const extractedItems = fs.readdirSync(tempDir);
|
|
80
|
+
if (extractedItems.length > 0) {
|
|
81
|
+
// Assume que dentro do tar há uma única pasta com o mesmo nome da revisão
|
|
82
|
+
const extractedDir = Path.join(tempDir, extractedItems[0]);
|
|
83
|
+
const psmYamlPath = Path.join(extractedDir, "psm.yml");
|
|
84
|
+
if (fs.existsSync(psmYamlPath)) {
|
|
85
|
+
const content = fs.readFileSync(psmYamlPath, "utf-8");
|
|
86
|
+
preview = yaml.parse(content);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
69
93
|
}
|
|
94
|
+
// ---------------------------------------------------------------------
|
|
70
95
|
psm.migration = {
|
|
71
96
|
revision: `${moment().format('YYYYMMDDHHmmss')} - ${psm.psm.migration}`,
|
|
72
97
|
instante: moment().format('YYYYMMDDHHmmss'),
|
|
73
|
-
preview: preview === null || preview === void 0 ? void 0 : preview.migration.revision,
|
|
74
|
-
label: opts.label
|
|
98
|
+
preview: (_a = preview === null || preview === void 0 ? void 0 : preview.migration) === null || _a === void 0 ? void 0 : _a.revision,
|
|
99
|
+
label: opts.label,
|
|
75
100
|
};
|
|
76
101
|
let label = "";
|
|
77
102
|
if (!!opts.label)
|
|
@@ -100,7 +125,7 @@ function commit(opts) {
|
|
|
100
125
|
const fetchResponse = yield (0, deploy_1.fetch)({
|
|
101
126
|
psm: psm,
|
|
102
127
|
driver: driver,
|
|
103
|
-
home: home
|
|
128
|
+
home: home,
|
|
104
129
|
});
|
|
105
130
|
if (fetchResponse.error) {
|
|
106
131
|
throw fetchResponse.error;
|
|
@@ -125,7 +150,7 @@ function commit(opts) {
|
|
|
125
150
|
const custom = (0, execute_1.CreateCustom)({
|
|
126
151
|
home: home,
|
|
127
152
|
nextRev: nextRev,
|
|
128
|
-
migrator: migrator
|
|
153
|
+
migrator: migrator,
|
|
129
154
|
}, "functions", "triggers", "views");
|
|
130
155
|
result = yield migrator.migrate(custom).catch(reason => {
|
|
131
156
|
return { error: reason, success: false };
|
|
@@ -148,10 +173,10 @@ function commit(opts) {
|
|
|
148
173
|
const archiveName = Path.join(home || process.cwd(), `psm/revisions/schema/${psm.migration.instante}${label}.tar.gz`);
|
|
149
174
|
yield tar.c({
|
|
150
175
|
gzip: {
|
|
151
|
-
level: 9
|
|
176
|
+
level: 9,
|
|
152
177
|
},
|
|
153
178
|
file: archiveName,
|
|
154
|
-
cwd: Path.dirname(nextRev)
|
|
179
|
+
cwd: Path.dirname(nextRev),
|
|
155
180
|
}, [Path.basename(nextRev)]);
|
|
156
181
|
console.log(chalk_1.default.green(`✔ Migration compactada: ${archiveName}`));
|
|
157
182
|
fs.rmSync(nextRev, { recursive: true, force: true });
|
package/src/tools/commit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commit.js","sourceRoot":"","sources":["commit.ts"],"names":[],"mappings":";AAAA,gCAAgC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"commit.js","sourceRoot":"","sources":["commit.ts"],"names":[],"mappings":";AAAA,gCAAgC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsBhC,wBAsKC;AA1LD,4CAA8B;AAC9B,gDAAkC;AAClC,4CAA8B,CAAgB,2CAA2C;AACzF,2CAA6B;AAC7B,yCAA2B;AAC3B,kDAA0B;AAE1B,uDAAyC;AACzC,qCAAiC;AACjC,qCAAqC;AACrC,oCAAyE;AACzE,uCAAyC;AASzC,SAAsB,MAAM,CAAC,IAAoB;;;QAC7C,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;QAEjC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,IAAI,OAAO,GAAG,IAAI,CAAC,kBAAkB,CAAC,CAAC;YACvC,IAAI,CAAC,OAAO;gBAAE,OAAO,GAAG,iBAAiB,CAAC;YAC1C,EAAE,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE;gBACzC,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;aACrB,CAAC,CAAC;QACP,CAAC;QAED,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAA,kBAAS,EAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QAChF,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;QAClE,MAAM,K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:fs";
|
|
4
4
|
import * as Path from "node:path";
|
|
5
|
+
import * as os from "node:os"; // <-- adicionado para diretório temporário
|
|
5
6
|
import * as yaml from "yaml";
|
|
6
7
|
import * as tar from "tar";
|
|
7
8
|
import chalk from "chalk";
|
|
8
|
-
import {PSMConfigFile} from "../configs";
|
|
9
|
+
import { PSMConfigFile } from "../configs";
|
|
9
10
|
import * as cp from "node:child_process";
|
|
10
|
-
import {fetch} from "./deploy";
|
|
11
|
-
import {psmLockup} from "./common";
|
|
12
|
-
import {getLatestFolder, gitAddPath, sanitizeLabel} from "../utils/fs";
|
|
13
|
-
import {CreateCustom} from "./execute";
|
|
11
|
+
import { fetch } from "./deploy";
|
|
12
|
+
import { psmLockup } from "./common";
|
|
13
|
+
import { getLatestFolder, gitAddPath, sanitizeLabel } from "../utils/fs";
|
|
14
|
+
import { CreateCustom } from "./execute";
|
|
14
15
|
|
|
15
16
|
export interface MigrateOptions {
|
|
16
|
-
schema?:string
|
|
17
|
-
generate?:string
|
|
18
|
-
label?:string
|
|
19
|
-
"generate-command":string
|
|
17
|
+
schema?: string;
|
|
18
|
+
generate?: string;
|
|
19
|
+
label?: string;
|
|
20
|
+
"generate-command": string;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
|
|
23
|
+
export async function commit(opts: MigrateOptions) {
|
|
22
24
|
require('dotenv').config();
|
|
23
25
|
const moment = require('moment');
|
|
24
26
|
|
|
25
|
-
if(
|
|
27
|
+
if (opts.generate) {
|
|
26
28
|
let command = opts["generate-command"];
|
|
27
|
-
if(
|
|
28
|
-
cp.spawnSync(
|
|
29
|
-
cwd: process.cwd()
|
|
29
|
+
if (!command) command = "prisma generate";
|
|
30
|
+
cp.spawnSync("npx", [...command.split(" ")], {
|
|
31
|
+
cwd: process.cwd(),
|
|
30
32
|
});
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
const { psm, psm_sql, driver, home } = await psmLockup({ schema: opts.schema });
|
|
34
|
-
const next = Path.join(
|
|
35
|
-
const check = Path.join(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
let preview:PSMConfigFile;
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
const next = Path.join(psm.psm.output, "next/migration.next.sql");
|
|
37
|
+
const check = Path.join(psm.psm.output, "next/migration.next.check.sql");
|
|
38
|
+
|
|
39
|
+
// -------------------- Leitura do preview corrigida --------------------
|
|
40
|
+
let preview: PSMConfigFile | undefined;
|
|
41
|
+
const revisionsDir = Path.join(home, "psm/revisions/schema");
|
|
42
|
+
const lastFile = getLatestFolder(revisionsDir);
|
|
43
|
+
|
|
44
|
+
if (lastFile) {
|
|
45
|
+
const tarPath = Path.join(revisionsDir, lastFile);
|
|
46
|
+
const tempDir = fs.mkdtempSync(Path.join(os.tmpdir(), "psm-preview-"));
|
|
47
|
+
try {
|
|
48
|
+
await tar.x({
|
|
49
|
+
file: tarPath,
|
|
50
|
+
cwd: tempDir,
|
|
51
|
+
});
|
|
52
|
+
const extractedItems = fs.readdirSync(tempDir);
|
|
53
|
+
if (extractedItems.length > 0) {
|
|
54
|
+
// Assume que dentro do tar há uma única pasta com o mesmo nome da revisão
|
|
55
|
+
const extractedDir = Path.join(tempDir, extractedItems[0]);
|
|
56
|
+
const psmYamlPath = Path.join(extractedDir, "psm.yml");
|
|
57
|
+
if (fs.existsSync(psmYamlPath)) {
|
|
58
|
+
const content = fs.readFileSync(psmYamlPath, "utf-8");
|
|
59
|
+
preview = yaml.parse(content) as PSMConfigFile;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} finally {
|
|
63
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
64
|
+
}
|
|
42
65
|
}
|
|
66
|
+
// ---------------------------------------------------------------------
|
|
43
67
|
|
|
44
68
|
psm.migration = {
|
|
45
|
-
revision: `${
|
|
46
|
-
instante: moment().format(
|
|
47
|
-
preview: preview?.migration
|
|
48
|
-
label: opts.label
|
|
49
|
-
}
|
|
50
|
-
let label = "";
|
|
51
|
-
if( !!opts.label ) label = ` - ${sanitizeLabel( opts.label )}`;
|
|
52
|
-
const nextRev = Path.join( home, `psm/revisions/schema/${psm.migration.instante}${label}`);
|
|
69
|
+
revision: `${moment().format('YYYYMMDDHHmmss')} - ${psm.psm.migration}`,
|
|
70
|
+
instante: moment().format('YYYYMMDDHHmmss'),
|
|
71
|
+
preview: preview?.migration?.revision,
|
|
72
|
+
label: opts.label,
|
|
73
|
+
};
|
|
53
74
|
|
|
75
|
+
let label = "";
|
|
76
|
+
if (!!opts.label) label = ` - ${sanitizeLabel(opts.label)}`;
|
|
77
|
+
const nextRev = Path.join(home, `psm/revisions/schema/${psm.migration.instante}${label}`);
|
|
54
78
|
|
|
55
|
-
if(
|
|
56
|
-
throw new Error(
|
|
79
|
+
if (!fs.existsSync(check)) {
|
|
80
|
+
throw new Error("Migrate error: next/migration.next.check.sql file not found!");
|
|
57
81
|
}
|
|
58
|
-
if(
|
|
59
|
-
throw new Error(
|
|
82
|
+
if (!fs.existsSync(next)) {
|
|
83
|
+
throw new Error("Migrate error: next/migration.next.sql file not found!");
|
|
60
84
|
}
|
|
61
85
|
|
|
62
86
|
const migrator = driver.migrator({
|
|
63
|
-
url: process.env[
|
|
64
|
-
migrate: fs.readFileSync(
|
|
65
|
-
check: fs.readFileSync(
|
|
66
|
-
core: fs.readFileSync(
|
|
87
|
+
url: process.env[psm.psm.url],
|
|
88
|
+
migrate: fs.readFileSync(next).toString(),
|
|
89
|
+
check: fs.readFileSync(check).toString(),
|
|
90
|
+
core: fs.readFileSync(psm_sql).toString(),
|
|
67
91
|
});
|
|
68
92
|
|
|
69
93
|
let result = await migrator.core();
|
|
70
|
-
if(
|
|
71
|
-
console.error(
|
|
72
|
-
result.messages.forEach(
|
|
73
|
-
console.error(
|
|
94
|
+
if (!result.success) {
|
|
95
|
+
console.error(result.error);
|
|
96
|
+
result.messages.forEach(error => {
|
|
97
|
+
console.error(error);
|
|
74
98
|
});
|
|
75
|
-
throw new Error(
|
|
99
|
+
throw new Error("Migrate error: Core failed!");
|
|
76
100
|
}
|
|
77
101
|
|
|
78
102
|
const fetchResponse = await fetch({
|
|
79
103
|
psm: psm,
|
|
80
104
|
driver: driver,
|
|
81
|
-
home: home
|
|
105
|
+
home: home,
|
|
82
106
|
});
|
|
83
107
|
|
|
84
|
-
if(
|
|
108
|
+
if (fetchResponse.error) {
|
|
85
109
|
throw fetchResponse.error;
|
|
86
110
|
}
|
|
87
111
|
|
|
88
|
-
const noPulled = fetchResponse.revs.filter(
|
|
112
|
+
const noPulled = fetchResponse.revs.filter(n => !n.pulled);
|
|
89
113
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
throw new Error( `Commit not pulled already exists! Please run ${chalk.bold("psm deploy")} first!` );
|
|
114
|
+
if (noPulled.length) {
|
|
115
|
+
throw new Error(`Commit not pulled already exists! Please run ${chalk.bold("psm deploy")} first!`);
|
|
93
116
|
}
|
|
94
117
|
|
|
95
118
|
result = await migrator.test();
|
|
96
|
-
if(
|
|
97
|
-
console.error(
|
|
98
|
-
result.messages.forEach(
|
|
99
|
-
console.error(
|
|
119
|
+
if (!result.success) {
|
|
120
|
+
console.error(result.error);
|
|
121
|
+
result.messages.forEach(error => {
|
|
122
|
+
console.error(error);
|
|
100
123
|
});
|
|
101
|
-
throw new Error(
|
|
124
|
+
throw new Error("Migrate error: Check shadow failed!");
|
|
102
125
|
}
|
|
103
126
|
|
|
104
127
|
const dump = await migrator.dump();
|
|
105
|
-
if(
|
|
128
|
+
if (dump.error) {
|
|
106
129
|
throw dump.error;
|
|
107
130
|
}
|
|
108
131
|
|
|
109
|
-
fs.mkdirSync(
|
|
110
|
-
const custom = CreateCustom(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
132
|
+
fs.mkdirSync(nextRev, { recursive: true });
|
|
133
|
+
const custom = CreateCustom(
|
|
134
|
+
{
|
|
135
|
+
home: home,
|
|
136
|
+
nextRev: nextRev,
|
|
137
|
+
migrator: migrator,
|
|
138
|
+
},
|
|
139
|
+
"functions",
|
|
140
|
+
"triggers",
|
|
141
|
+
"views"
|
|
142
|
+
);
|
|
115
143
|
|
|
116
|
-
result = await migrator.migrate(
|
|
117
|
-
return
|
|
144
|
+
result = await migrator.migrate(custom).catch(reason => {
|
|
145
|
+
return { error: reason, success: false };
|
|
118
146
|
});
|
|
119
147
|
|
|
120
|
-
if(
|
|
121
|
-
console.error(
|
|
122
|
-
result.messages.forEach(
|
|
123
|
-
console.error(
|
|
148
|
+
if (!result.success) {
|
|
149
|
+
console.error(result.error);
|
|
150
|
+
result.messages.forEach(error => {
|
|
151
|
+
console.error(error);
|
|
124
152
|
});
|
|
125
|
-
if(
|
|
126
|
-
fs.rmdirSync(
|
|
153
|
+
if (fs.existsSync(nextRev)) {
|
|
154
|
+
fs.rmdirSync(nextRev);
|
|
127
155
|
}
|
|
128
|
-
throw new Error(
|
|
156
|
+
throw new Error(`Migrate error: Commit migration failed! ${home}`);
|
|
129
157
|
}
|
|
130
158
|
|
|
131
159
|
custom.createFiles();
|
|
132
160
|
|
|
133
|
-
fs.writeFileSync(
|
|
134
|
-
fs.writeFileSync(
|
|
135
|
-
fs.writeFileSync(
|
|
161
|
+
fs.writeFileSync(Path.join(nextRev, "migration.sql"), migrator.migrateRaw(custom));
|
|
162
|
+
fs.writeFileSync(Path.join(nextRev, "psm.yml"), yaml.stringify(psm));
|
|
163
|
+
fs.writeFileSync(Path.join(nextRev, "backup.sql"), dump.output);
|
|
136
164
|
|
|
137
|
-
fs.unlinkSync(
|
|
165
|
+
fs.unlinkSync(check);
|
|
138
166
|
|
|
139
|
-
const archiveName = Path.join(
|
|
167
|
+
const archiveName = Path.join(
|
|
168
|
+
home || process.cwd(),
|
|
169
|
+
`psm/revisions/schema/${psm.migration.instante}${label}.tar.gz`
|
|
170
|
+
);
|
|
140
171
|
|
|
141
172
|
await tar.c(
|
|
142
173
|
{
|
|
143
174
|
gzip: {
|
|
144
|
-
level: 9
|
|
175
|
+
level: 9,
|
|
145
176
|
},
|
|
146
177
|
file: archiveName,
|
|
147
|
-
cwd: Path.dirname(nextRev)
|
|
178
|
+
cwd: Path.dirname(nextRev),
|
|
148
179
|
},
|
|
149
180
|
[Path.basename(nextRev)]
|
|
150
181
|
);
|
|
@@ -152,8 +183,7 @@ export async function commit(opts:MigrateOptions ) {
|
|
|
152
183
|
console.log(chalk.green(`✔ Migration compactada: ${archiveName}`));
|
|
153
184
|
|
|
154
185
|
fs.rmSync(nextRev, { recursive: true, force: true });
|
|
155
|
-
fs.unlinkSync(
|
|
156
|
-
|
|
157
|
-
gitAddPath(home || process.cwd(), archiveName );
|
|
158
|
-
}
|
|
186
|
+
fs.unlinkSync(next);
|
|
159
187
|
|
|
188
|
+
gitAddPath(home || process.cwd(), archiveName);
|
|
189
|
+
}
|