@prisma-psm/core 1.0.2 → 1.0.4

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.
Files changed (59) hide show
  1. package/README.md +190 -2
  2. package/package.json +14 -2
  3. package/src/configs.d.ts +20 -0
  4. package/src/configs.d.ts.map +1 -0
  5. package/src/configs.js +3 -0
  6. package/src/configs.js.map +1 -0
  7. package/src/configs.ts +19 -0
  8. package/src/driver.d.ts +1 -0
  9. package/src/driver.d.ts.map +1 -1
  10. package/src/driver.ts +2 -1
  11. package/src/index.d.ts.map +1 -1
  12. package/src/index.js +0 -158
  13. package/src/index.js.map +1 -1
  14. package/src/index.ts +1 -171
  15. package/src/launcher/commands/check.d.ts +4 -0
  16. package/src/launcher/commands/check.d.ts.map +1 -0
  17. package/src/launcher/commands/check.js +13 -0
  18. package/src/launcher/commands/check.js.map +1 -0
  19. package/src/launcher/commands/check.ts +17 -0
  20. package/src/launcher/commands/deploy.d.ts +5 -0
  21. package/src/launcher/commands/deploy.d.ts.map +1 -0
  22. package/src/launcher/commands/deploy.js +13 -0
  23. package/src/launcher/commands/deploy.js.map +1 -0
  24. package/src/launcher/commands/deploy.ts +17 -0
  25. package/src/launcher/commands/generate.d.ts +4 -0
  26. package/src/launcher/commands/generate.d.ts.map +1 -0
  27. package/src/launcher/commands/generate.js +14 -0
  28. package/src/launcher/commands/generate.js.map +1 -0
  29. package/src/launcher/commands/generate.ts +18 -0
  30. package/src/launcher/commands/migrate.d.ts +5 -0
  31. package/src/launcher/commands/migrate.d.ts.map +1 -0
  32. package/src/launcher/commands/migrate.js +29 -0
  33. package/src/launcher/commands/migrate.js.map +1 -0
  34. package/src/launcher/commands/migrate.ts +35 -0
  35. package/src/launcher/index.d.ts +3 -0
  36. package/src/launcher/index.d.ts.map +1 -0
  37. package/src/launcher/index.js +17 -0
  38. package/src/launcher/index.js.map +1 -0
  39. package/src/launcher/index.ts +14 -0
  40. package/src/tools/check.d.ts +2 -0
  41. package/src/tools/check.d.ts.map +1 -0
  42. package/src/tools/check.js +6 -0
  43. package/src/tools/check.js.map +1 -0
  44. package/src/tools/check.ts +3 -0
  45. package/src/tools/deploy.d.ts +7 -0
  46. package/src/tools/deploy.d.ts.map +1 -0
  47. package/src/tools/deploy.js +3 -0
  48. package/src/tools/deploy.js.map +1 -0
  49. package/src/tools/deploy.ts +6 -0
  50. package/src/tools/generate.d.ts +2 -0
  51. package/src/tools/generate.d.ts.map +1 -0
  52. package/src/tools/generate.js +158 -0
  53. package/src/tools/generate.js.map +1 -0
  54. package/src/tools/generate.ts +151 -0
  55. package/src/tools/migrate.d.ts +8 -0
  56. package/src/tools/migrate.d.ts.map +1 -0
  57. package/src/tools/migrate.js +137 -0
  58. package/src/tools/migrate.js.map +1 -0
  59. package/src/tools/migrate.ts +121 -0
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ const check_1 = require("../../tools/check");
3
+ const command = {
4
+ command: "check",
5
+ builder: args => {
6
+ return args;
7
+ },
8
+ handler: args => {
9
+ (0, check_1.check)();
10
+ }
11
+ };
12
+ module.exports = command;
13
+ //# sourceMappingURL=check.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.js","sourceRoot":"","sources":["check.ts"],"names":[],"mappings":";AAEA,6CAAwC;AAGxC,MAAM,OAAO,GAAiB;IAC1B,OAAO,EAAE,OAAO;IAChB,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,IAAA,aAAK,GAAE,CAAC;IACZ,CAAC;CACJ,CAAA;AAED,iBAAS,OAAO,CAAC"}
@@ -0,0 +1,17 @@
1
+ import {CommandModule} from "yargs";
2
+ import {migrate} from "../../tools/migrate";
3
+ import {check} from "../../tools/check";
4
+
5
+
6
+ const command:CommandModule = {
7
+ command: "check",
8
+ builder: args => {
9
+ return args;
10
+ },
11
+ handler: args => {
12
+ check();
13
+ }
14
+ }
15
+
16
+ export = command;
17
+
@@ -0,0 +1,5 @@
1
+ import { CommandModule } from "yargs";
2
+ import { DeployOptions } from "../../tools/deploy";
3
+ declare const command: CommandModule<DeployOptions, DeployOptions>;
4
+ export = command;
5
+ //# sourceMappingURL=deploy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AACpC,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAGjD,QAAA,MAAM,OAAO,EAAC,aAAa,CAAC,aAAa,EAAE,aAAa,CASvD,CAAA;AAED,SAAS,OAAO,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ const command = {
3
+ command: "migrate deploy",
4
+ describe: "Deploy to production environment",
5
+ builder: args => {
6
+ return args;
7
+ },
8
+ handler: (argv) => {
9
+ console.log("TODO - psm migrate deploy ainda não esta implementado");
10
+ }
11
+ };
12
+ module.exports = command;
13
+ //# sourceMappingURL=deploy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.js","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":";AAIA,MAAM,OAAO,GAA+C;IACxD,OAAO,EAAE,gBAAgB;IACzB,QAAQ,EAAE,kCAAkC;IAC5C,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,EAAC,CAAE,IAAI,EAAE,EAAE;QACd,OAAO,CAAC,GAAG,CAAE,uDAAuD,CAAC,CAAA;IACzE,CAAC;CACJ,CAAA;AAED,iBAAS,OAAO,CAAC"}
@@ -0,0 +1,17 @@
1
+ import {CommandModule} from "yargs";
2
+ import {DeployOptions} from "../../tools/deploy";
3
+
4
+
5
+ const command:CommandModule<DeployOptions, DeployOptions> = {
6
+ command: "migrate deploy",
7
+ describe: "Deploy to production environment",
8
+ builder: args => {
9
+ return args;
10
+ },
11
+ handler:( argv) =>{
12
+ console.log( "TODO - psm migrate deploy ainda não esta implementado")
13
+ }
14
+ }
15
+
16
+ export = command;
17
+
@@ -0,0 +1,4 @@
1
+ import { CommandModule } from "yargs";
2
+ declare const command: CommandModule;
3
+ export = command;
4
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["generate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AAIpC,QAAA,MAAM,OAAO,EAAC,aAUb,CAAA;AAED,SAAS,OAAO,CAAC"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ const generate_1 = require("../../tools/generate");
3
+ const command = {
4
+ command: "migrate generate",
5
+ describe: "Generate pre-migrate archives",
6
+ builder: args => {
7
+ return args;
8
+ },
9
+ handler: args => {
10
+ (0, generate_1.generate)();
11
+ }
12
+ };
13
+ module.exports = command;
14
+ //# sourceMappingURL=generate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.js","sourceRoot":"","sources":["generate.ts"],"names":[],"mappings":";AACA,mDAA8C;AAG9C,MAAM,OAAO,GAAiB;IAC1B,OAAO,EAAE,kBAAkB;IAC3B,QAAQ,EAAE,+BAA+B;IAEzC,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,IAAA,mBAAQ,GAAE,CAAC;IACf,CAAC;CACJ,CAAA;AAED,iBAAS,OAAO,CAAC"}
@@ -0,0 +1,18 @@
1
+ import {CommandModule} from "yargs";
2
+ import {generate} from "../../tools/generate";
3
+
4
+
5
+ const command:CommandModule = {
6
+ command: "migrate generate",
7
+ describe: "Generate pre-migrate archives",
8
+
9
+ builder: args => {
10
+ return args;
11
+ },
12
+ handler: args => {
13
+ generate();
14
+ }
15
+ }
16
+
17
+ export = command;
18
+
@@ -0,0 +1,5 @@
1
+ import { CommandModule } from "yargs";
2
+ import { MigrateOptions } from "../../tools/migrate";
3
+ declare const command: CommandModule<MigrateOptions, MigrateOptions>;
4
+ export = command;
5
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["migrate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AACpC,OAAO,EAAU,cAAc,EAAC,MAAM,qBAAqB,CAAC;AAG5D,QAAA,MAAM,OAAO,EAAC,aAAa,CAAC,cAAc,EAAE,cAAc,CA2BzD,CAAA;AAED,SAAS,OAAO,CAAC"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ const command = {
3
+ command: "migrate commit",
4
+ describe: "Migrate next schema structure into dev environment",
5
+ builder: args => {
6
+ args.options("schema", {
7
+ type: "string",
8
+ alias: "s"
9
+ }).options("generate", {
10
+ type: "boolean",
11
+ alias: "g",
12
+ }).options("label", {
13
+ type: "string",
14
+ alias: "l",
15
+ }).options("generate-command", {
16
+ type: "string",
17
+ alias: "c"
18
+ });
19
+ return args;
20
+ },
21
+ handler: (argv) => {
22
+ console.log(argv);
23
+ // migrate(argv).then( value => {
24
+ // }).catch( reason => {
25
+ // })
26
+ }
27
+ };
28
+ module.exports = command;
29
+ //# sourceMappingURL=migrate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.js","sourceRoot":"","sources":["migrate.ts"],"names":[],"mappings":";AAIA,MAAM,OAAO,GAAiD;IAC1D,OAAO,EAAE,gBAAgB;IACzB,QAAQ,EAAE,oDAAoD;IAC9D,OAAO,EAAE,IAAI,CAAC,EAAE;QACZ,IAAI,CAAC,OAAO,CAAE,QAAQ,EAAE;YACpB,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,GAAG;SACb,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE;YACnB,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,GAAG;SACb,CAAC,CAAC,OAAO,CAAE,OAAO,EAAE;YACjB,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,GAAG;SAEb,CAAC,CAAC,OAAO,CAAE,kBAAkB,EAAE;YAC5B,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,GAAG;SACb,CAAC,CAAA;QAEF,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,EAAC,CAAE,IAAI,EAAE,EAAE;QACd,OAAO,CAAC,GAAG,CAAE,IAAI,CAAE,CAAA;QACnB,kCAAkC;QAClC,wBAAwB;QACxB,KAAK;IACT,CAAC;CACJ,CAAA;AAED,iBAAS,OAAO,CAAC"}
@@ -0,0 +1,35 @@
1
+ import {CommandModule} from "yargs";
2
+ import {migrate, MigrateOptions} from "../../tools/migrate";
3
+
4
+
5
+ const command:CommandModule<MigrateOptions, MigrateOptions> = {
6
+ command: "migrate commit",
7
+ describe: "Migrate next schema structure into dev environment",
8
+ builder: args => {
9
+ args.options( "schema", {
10
+ type: "string",
11
+ alias: "s"
12
+ }).options("generate", {
13
+ type: "boolean",
14
+ alias: "g",
15
+ }).options( "label", {
16
+ type: "string",
17
+ alias: "l",
18
+
19
+ }).options( "generate-command", {
20
+ type: "string",
21
+ alias: "c"
22
+ })
23
+
24
+ return args;
25
+ },
26
+ handler:( argv) =>{
27
+ console.log( argv )
28
+ // migrate(argv).then( value => {
29
+ // }).catch( reason => {
30
+ // })
31
+ }
32
+ }
33
+
34
+ export = command;
35
+
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ require("source-map-support").install();
8
+ const path_1 = __importDefault(require("path"));
9
+ require("source-map-support").install();
10
+ const yargs_1 = __importDefault(require("yargs"));
11
+ let ss = (0, yargs_1.default)(process.argv.slice(2))
12
+ //language=file-reference
13
+ .commandDir(path_1.default.join(__dirname, "./commands"))
14
+ .demandCommand()
15
+ .help()
16
+ .argv;
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,EAAE,CAAC;AAExC,gDAAwB;AAExB,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,EAAE,CAAC;AACxC,kDAA0B;AAE1B,IAAI,EAAE,GAAG,IAAA,eAAK,EAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjC,yBAAyB;KACxB,UAAU,CAAC,cAAI,CAAC,IAAI,CAAE,SAAS,EAAE,YAAY,CAAE,CAAE;KACjD,aAAa,EAAE;KACf,IAAI,EAAE;KACN,IAAI,CAAC"}
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ require("source-map-support").install();
3
+
4
+ import Path from "path";
5
+
6
+ require("source-map-support").install();
7
+ import yargs from "yargs";
8
+
9
+ let ss = yargs(process.argv.slice(2))
10
+ //language=file-reference
11
+ .commandDir(Path.join( __dirname, "./commands" ) )
12
+ .demandCommand()
13
+ .help()
14
+ .argv;
@@ -0,0 +1,2 @@
1
+ export declare function check(): void;
2
+ //# sourceMappingURL=check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["check.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,SAEpB"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.check = check;
4
+ function check() {
5
+ }
6
+ //# sourceMappingURL=check.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.js","sourceRoot":"","sources":["check.ts"],"names":[],"mappings":";;AAAA,sBAEC;AAFD,SAAgB,KAAK;AAErB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export function check(){
2
+
3
+ }
@@ -0,0 +1,7 @@
1
+ export interface DeployOptions {
2
+ schema?: string;
3
+ generate?: string;
4
+ label?: string;
5
+ "generate-command": string;
6
+ }
7
+ //# sourceMappingURL=deploy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC1B,MAAM,CAAC,EAAC,MAAM,CAAA;IACd,QAAQ,CAAC,EAAC,MAAM,CAAA;IAChB,KAAK,CAAC,EAAC,MAAM,CAAA;IACb,kBAAkB,EAAC,MAAM,CAAA;CAC5B"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=deploy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.js","sourceRoot":"","sources":["deploy.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export interface DeployOptions {
2
+ schema?:string
3
+ generate?:string
4
+ label?:string
5
+ "generate-command":string
6
+ }
@@ -0,0 +1,2 @@
1
+ export declare function generate(): void;
2
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["generate.ts"],"names":[],"mappings":"AAuBA,wBAAgB,QAAQ,SA+HvB"}
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.generate = generate;
36
+ const fs = __importStar(require("fs"));
37
+ const Path = __importStar(require("node:path"));
38
+ const yaml = __importStar(require("yaml"));
39
+ const generator_helper_1 = require("@prisma/generator-helper");
40
+ const index_1 = require("../extractor/index");
41
+ const nanoid_1 = require("nanoid");
42
+ const docs_1 = require("../docs");
43
+ function write(sql, dirname, file) {
44
+ fs.writeFileSync(Path.join(dirname, file), sql);
45
+ }
46
+ function generate() {
47
+ (0, generator_helper_1.generatorHandler)({
48
+ onManifest() {
49
+ return {
50
+ version: '1.0.0',
51
+ defaultOutput: './psm',
52
+ prettyName: 'PSM - Prisma Safe Migration',
53
+ };
54
+ },
55
+ onGenerate(options) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ var _a, _b;
58
+ try {
59
+ const output = ((_a = options.generator.output) === null || _a === void 0 ? void 0 : _a.value) || "./psm";
60
+ const home = Path.dirname(options.schemaPath);
61
+ const definition = Path.join(output, "definitions");
62
+ const revisions = Path.join(output, "revisions");
63
+ const next = Path.join(output, "next");
64
+ fs.mkdirSync(definition, { recursive: true });
65
+ fs.mkdirSync(revisions, { recursive: true });
66
+ fs.mkdirSync(next, { recursive: true });
67
+ const { datamodels, models } = (0, index_1.extractModels)(options, definition);
68
+ let rand = (0, nanoid_1.customRandom)("abcdefghijklmnopqrstuvwxyz0123456789", 8, nanoid_1.random);
69
+ const migration = rand();
70
+ const configs = options.generator.config;
71
+ const url = process.env[configs.url];
72
+ const driver = yield Promise.resolve(`${configs.driver}`).then(s => __importStar(require(s)));
73
+ let usePrepare = (model) => {
74
+ return Promise.resolve(driver.prepare(model));
75
+ };
76
+ for (let i = 0; i < models.length; i++) {
77
+ const model = models[i];
78
+ model.model = model.name;
79
+ model.name = model.dbName || model.model;
80
+ model.temp = `temp_${i}_${model.name}`;
81
+ if (!!model.documentation) {
82
+ model.psm = (0, docs_1.parsePsmDoc)(model.documentation);
83
+ }
84
+ model.fields.forEach(field => {
85
+ if (!!field.documentation)
86
+ field.psm = (0, docs_1.parsePsmDoc)(field.documentation);
87
+ });
88
+ yield usePrepare(model);
89
+ }
90
+ const opts = {
91
+ models: models,
92
+ indexes: datamodels.indexes,
93
+ migration: migration,
94
+ shadow: `psm_shadow_${rand()}`,
95
+ backup: "backup",
96
+ sys: "sys",
97
+ };
98
+ const generator = driver.generator(opts);
99
+ const check = generator.check();
100
+ const migrate = generator.migrate();
101
+ write(check, next, "migration.next.check.sql");
102
+ let test;
103
+ if (!!url) {
104
+ test = yield driver.migrator({
105
+ migrate: migrate,
106
+ check: check,
107
+ url: url
108
+ }).test();
109
+ }
110
+ console.log(test);
111
+ if (!configs.url) {
112
+ write(migrate, next, "migration.next.sql");
113
+ }
114
+ else if (!!test && test.success) {
115
+ write(migrate, next, "migration.next.sql");
116
+ }
117
+ else {
118
+ console.error(`TEST OF MIGRATION FAILED! Check file migration.next.check.sql`);
119
+ if (fs.existsSync(Path.join(next, "migration.next.sql")))
120
+ fs.unlinkSync(Path.join(next, "migration.next.sql"));
121
+ if (!!((_b = test === null || test === void 0 ? void 0 : test.messages) === null || _b === void 0 ? void 0 : _b.length)) {
122
+ console.log("TESTE MESSAGES>>>>>>>>>>");
123
+ test.messages.forEach(value => {
124
+ console.log(value);
125
+ });
126
+ }
127
+ if (!!(test === null || test === void 0 ? void 0 : test.error)) {
128
+ console.log("TESTE ERROR>>>>>>>>>>");
129
+ console.error(test === null || test === void 0 ? void 0 : test.error);
130
+ }
131
+ }
132
+ const psm = {
133
+ psm: {
134
+ migration: migration,
135
+ driver: options.generator.config.driver,
136
+ url: configs.url,
137
+ output: output,
138
+ schema: options.schemaPath,
139
+ },
140
+ test: {
141
+ check: !!test ? "checked" : "skipped",
142
+ success: test === null || test === void 0 ? void 0 : test.success,
143
+ messages: test === null || test === void 0 ? void 0 : test.messages
144
+ }
145
+ };
146
+ fs.writeFileSync(Path.join(home, "psm.yml"), yaml.stringify(psm, null, {
147
+ version: "next",
148
+ }));
149
+ }
150
+ catch (e) {
151
+ console.error("console.error", e);
152
+ throw e;
153
+ }
154
+ });
155
+ },
156
+ });
157
+ }
158
+ //# sourceMappingURL=generate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.js","sourceRoot":"","sources":["generate.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,4BA+HC;AAtJD,uCAAwB;AACxB,gDAAkC;AAClC,2CAA6B;AAC7B,+DAA+F;AAC/F,8CAAiD;AACjD,mCAA4C;AAU5C,kCAAoC;AAGpC,SAAS,KAAK,CAAE,GAAU,EAAE,OAAc,EAAE,IAAW;IACnD,EAAE,CAAC,aAAa,CAAE,IAAI,CAAC,IAAI,C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,CAAC;oBACR,OAAO,CAAC,KAAK,CAAE,eAAe,EAAE,CAAC,CAAE,CAAC;oBACpC,MAAM,CAAC,CAAA;gBACX,CAAC;YACL,CAAC;SAAA;KACJ,CAAC,CAAA;AACN,CAAC"}
@@ -0,0 +1,151 @@
1
+ import * as fs from 'fs'
2
+ import * as Path from "node:path";
3
+ import * as yaml from "yaml";
4
+ import {generatorHandler, GeneratorManifest, GeneratorOptions} from "@prisma/generator-helper";
5
+ import {extractModels} from "../extractor/index";
6
+ import {customRandom, random} from "nanoid";
7
+ import {
8
+ DriverConfigs,
9
+ ModelOptions,
10
+ PSMDriver,
11
+ PSMField,
12
+ PSMMigrationResult,
13
+ PSMModel,
14
+ PSMParserOptions
15
+ } from "../driver";
16
+ import {parsePsmDoc} from "../docs";
17
+ import {PSMConfigFile} from "../configs";
18
+
19
+ function write( sql:string, dirname:string, file:string){
20
+ fs.writeFileSync( Path.join( dirname, file ), sql );
21
+ }
22
+
23
+
24
+ export function generate(){
25
+ generatorHandler({
26
+ onManifest(): GeneratorManifest {
27
+ return {
28
+ version: '1.0.0',
29
+ defaultOutput: './psm',
30
+ prettyName: 'PSM - Prisma Safe Migration',
31
+ }
32
+ },
33
+ async onGenerate(options: GeneratorOptions) {
34
+ try{
35
+
36
+ const output = options.generator.output?.value || "./psm";
37
+ const home = Path.dirname( options.schemaPath );
38
+ const definition = Path.join( output, "definitions");
39
+ const revisions = Path.join( output, "revisions");
40
+ const next = Path.join( output, "next");
41
+
42
+ fs.mkdirSync( definition, { recursive: true });
43
+ fs.mkdirSync( revisions, { recursive: true });
44
+ fs.mkdirSync( next, { recursive: true });
45
+ const { datamodels, models} = extractModels( options, definition );
46
+
47
+
48
+ let rand = customRandom( "abcdefghijklmnopqrstuvwxyz0123456789",8, random);
49
+ const migration = rand();
50
+ const configs:DriverConfigs = options.generator.config as any;
51
+ const url = process.env[configs.url] as string;
52
+
53
+
54
+ const driver = await import( configs.driver ) as PSMDriver;
55
+
56
+
57
+ let usePrepare= ( model:ModelOptions )=>{
58
+ return Promise.resolve( driver.prepare( model ) )
59
+ }
60
+
61
+ for (let i = 0; i < models.length; i++) {
62
+ const model = models[i] as any as ModelOptions;
63
+ model.model = model.name;
64
+ model.name = model.dbName||model.model;
65
+ model.temp = `temp_${i}_${model.name}`;
66
+
67
+
68
+ if( !!model.documentation ){
69
+ model.psm = parsePsmDoc<PSMModel>( model.documentation );
70
+ }
71
+ model.fields.forEach( field => {
72
+ if( !!field.documentation ) field.psm = parsePsmDoc<PSMField>( field.documentation );
73
+ });
74
+ await usePrepare(model);
75
+ }
76
+
77
+ const opts: PSMParserOptions = {
78
+ models: models as any,
79
+ indexes: datamodels.indexes,
80
+ migration: migration,
81
+ shadow: `psm_shadow_${rand()}`,
82
+ backup: "backup",
83
+ sys: "sys",
84
+ };
85
+
86
+ const generator = driver.generator(opts);
87
+
88
+ const check = generator.check();
89
+ const migrate = generator.migrate();
90
+
91
+ write( check, next, "migration.next.check.sql" );
92
+
93
+
94
+ let test:PSMMigrationResult|undefined;
95
+
96
+ if( !!url ) {
97
+ test = await driver.migrator({
98
+ migrate: migrate,
99
+ check: check,
100
+ url: url as string
101
+ }).test();
102
+ }
103
+
104
+
105
+ console.log( test );
106
+
107
+ if( !configs.url ){
108
+ write( migrate, next, "migration.next.sql" );
109
+ } else if( !!test && test.success ) {
110
+ write( migrate, next, "migration.next.sql" );
111
+ } else {
112
+ console.error( `TEST OF MIGRATION FAILED! Check file migration.next.check.sql` );
113
+ if( fs.existsSync( Path.join( next, "migration.next.sql" ) ) ) fs.unlinkSync( Path.join( next, "migration.next.sql" ) );
114
+
115
+ if( !!test?.messages?.length ){
116
+ console.log("TESTE MESSAGES>>>>>>>>>>");
117
+ test.messages.forEach(value => {
118
+ console.log( value)
119
+ });
120
+ }
121
+
122
+ if( !!test?.error ){
123
+ console.log("TESTE ERROR>>>>>>>>>>");
124
+ console.error( test?.error );
125
+ }
126
+ }
127
+
128
+ const psm:PSMConfigFile = {
129
+ psm: {
130
+ migration: migration,
131
+ driver: options.generator.config.driver as string,
132
+ url: configs.url,
133
+ output: output,
134
+ schema: options.schemaPath,
135
+ },
136
+ test: {
137
+ check: !!test? "checked": "skipped",
138
+ success: test?.success,
139
+ messages: test?.messages
140
+ }
141
+ }
142
+ fs.writeFileSync( Path.join( home, "psm.yml" ), yaml.stringify(psm, null, {
143
+ version: "next",
144
+ }) )
145
+ } catch (e){
146
+ console.error( "console.error", e );
147
+ throw e
148
+ }
149
+ },
150
+ })
151
+ }
@@ -0,0 +1,8 @@
1
+ export interface MigrateOptions {
2
+ schema?: string;
3
+ generate?: string;
4
+ label?: string;
5
+ "generate-command": string;
6
+ }
7
+ export declare function migrate(opts: MigrateOptions): Promise<void>;
8
+ //# sourceMappingURL=migrate.d.ts.map