UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var index_1 = require("../index");
5var ConnectionOptionsReader_1 = require("../connection/ConnectionOptionsReader");
6var cli_highlight_1 = require("cli-highlight");
7var chalk = require("chalk");
8/**
9 * Shows sql to be executed by schema:sync command.
10 */
11var SchemaLogCommand = /** @class */ (function () {
12 function SchemaLogCommand() {
13 this.command = "schema:log";
14 this.describe = "Shows sql to be executed by schema:sync command. It shows sql log only for your default connection. " +
15 "To run update queries on a concrete connection use -c option.";
16 }
17 SchemaLogCommand.prototype.builder = function (args) {
18 return args
19 .option("c", {
20 alias: "connection",
21 default: "default",
22 describe: "Name of the connection of which schema sync log should be shown."
23 })
24 .option("f", {
25 alias: "config",
26 default: "ormconfig",
27 describe: "Name of the file with connection configuration."
28 });
29 };
30 SchemaLogCommand.prototype.handler = function (args) {
31 return tslib_1.__awaiter(this, void 0, void 0, function () {
32 var connection, connectionOptionsReader, connectionOptions, sqlInMemory, lengthSeparators, err_1;
33 return tslib_1.__generator(this, function (_a) {
34 switch (_a.label) {
35 case 0:
36 connection = undefined;
37 _a.label = 1;
38 case 1:
39 _a.trys.push([1, 6, , 7]);
40 connectionOptionsReader = new ConnectionOptionsReader_1.ConnectionOptionsReader({
41 root: process.cwd(),
42 configName: args.config
43 });
44 return [4 /*yield*/, connectionOptionsReader.get(args.connection)];
45 case 2:
46 connectionOptions = _a.sent();
47 Object.assign(connectionOptions, {
48 synchronize: false,
49 migrationsRun: false,
50 dropSchema: false,
51 logging: false
52 });
53 return [4 /*yield*/, index_1.createConnection(connectionOptions)];
54 case 3:
55 connection = _a.sent();
56 return [4 /*yield*/, connection.driver.createSchemaBuilder().log()];
57 case 4:
58 sqlInMemory = _a.sent();
59 if (sqlInMemory.upQueries.length === 0) {
60 console.log(chalk.yellow("Your schema is up to date - there are no queries to be executed by schema syncronization."));
61 }
62 else {
63 lengthSeparators = String(sqlInMemory.upQueries.length).split("").map(function (char) { return "-"; }).join("");
64 console.log(chalk.yellow("---------------------------------------------------------------" + lengthSeparators));
65 console.log(chalk.yellow.bold("-- Schema syncronization will execute following sql queries (" + chalk.white(sqlInMemory.upQueries.length) + "):"));
66 console.log(chalk.yellow("---------------------------------------------------------------" + lengthSeparators));
67 sqlInMemory.upQueries.forEach(function (upQuery) {
68 var sqlString = upQuery.query;
69 sqlString = sqlString.trim();
70 sqlString = sqlString.substr(-1) === ";" ? sqlString : sqlString + ";";
71 console.log(cli_highlight_1.highlight(sqlString));
72 });
73 }
74 return [4 /*yield*/, connection.close()];
75 case 5:
76 _a.sent();
77 return [3 /*break*/, 7];
78 case 6:
79 err_1 = _a.sent();
80 if (connection)
81 console.log(chalk.black.bgRed("Error during schema synchronization:"));
82 console.error(err_1);
83 process.exit(1);
84 return [3 /*break*/, 7];
85 case 7: return [2 /*return*/];
86 }
87 });
88 });
89 };
90 return SchemaLogCommand;
91}());
92exports.SchemaLogCommand = SchemaLogCommand;
93
94//# sourceMappingURL=SchemaLogCommand.js.map