UNPKG

stackpress

Version:

Incept is a content management framework.

93 lines (92 loc) 4.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = push; const Revisions_js_1 = __importDefault(require("../client/Revisions.js")); const helpers_js_1 = require("../sql/helpers.js"); const schema_js_1 = __importDefault(require("../sql/schema.js")); function push(server, database, cli) { return __awaiter(this, void 0, void 0, function* () { const config = server.config('client') || {}; if (!config.revisions) { return; } const queries = []; const revisions = new Revisions_js_1.default(config.revisions, server.loader); const from = yield revisions.last(-1); const to = yield revisions.last(); (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.system('Updating database...'); if (!from && to) { (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.system(' with DROP/CREATE ...'); const models = Array.from(to.registry.model.values()); const order = (0, helpers_js_1.sequence)(models); for (const model of order) { queries.push(database.dialect.drop(model.snake)); } for (const model of order.reverse()) { const exists = models.find(map => map.name === model.name); if (exists) { const schema = (0, schema_js_1.default)(exists); schema.engine = database; queries.push(...schema.query()); } } if (queries.length) { yield database.transaction((connection) => __awaiter(this, void 0, void 0, function* () { for (const query of queries) { (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.info(query.query); yield connection.query(query); } })); } } else if (from && to) { (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.system(' with ALTER ...'); const previous = Array.from(from.registry.model.values()).map(model => (0, schema_js_1.default)(model)); const current = Array.from(to.registry.model.values()).map(model => (0, schema_js_1.default)(model)); const queries = []; for (const schema of current) { const name = schema.build().table; const before = previous.find(from => from.build().table === name); if (!before) { schema.engine = database; queries.push(...schema.query()); continue; } try { queries.push(...database.diff(before, schema).query()); } catch (e) { } } for (const schema of previous) { const name = schema.build().table; const after = current.find(to => to.build().table === name); if (!after) { queries.push(database.dialect.drop(name)); continue; } } if (queries.length) { yield database.transaction((connection) => __awaiter(this, void 0, void 0, function* () { for (const query of queries) { (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.info(query.query); yield connection.query(query); } })); } } (cli === null || cli === void 0 ? void 0 : cli.verbose) && cli.control.success('Database Updated.'); }); } ;