@nyffels/mynodeorm
Advanced tools
Comparing version
@@ -384,4 +384,4 @@ #! /usr/bin/env node | ||
scriptLines.push(`DROP TABLE IF EXISTS __myNodeORM;`); | ||
scriptLines.push(`CREATE TABLE __myNodeORM (version INT NOT NULL, DATE DATETIME NOT NULL DEFAULT NOW());`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) VALUES (${(latestMigrationVersion !== null && latestMigrationVersion !== void 0 ? latestMigrationVersion : "").split(".").find(x => x)});`); | ||
scriptLines.push(`CREATE TABLE __myNodeORM (version VARCHAR(255) NOT NULL, DATE DATETIME NOT NULL DEFAULT NOW());`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) VALUES (${(latestMigrationVersion !== null && latestMigrationVersion !== void 0 ? latestMigrationVersion : "").split("_").find(x => x)});`); | ||
/* Save the script */ | ||
@@ -388,0 +388,0 @@ const saveLocationPath = (_y = (_x = args.find((a) => a.includes('--output='))) === null || _x === void 0 ? void 0 : _x.replace('--output=', '')) !== null && _y !== void 0 ? _y : "./"; |
@@ -12,2 +12,2 @@ /** | ||
*/ | ||
export declare function updateDatabase(migrationLocationPath: string, version?: number | null): Promise<void>; | ||
export declare function updateDatabase(migrationLocationPath: string, version?: string | null): Promise<void>; |
@@ -13,3 +13,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { mkdirSync } from "fs"; | ||
import { difference, intersection, orderBy, sortBy, uniq } from "lodash-es"; | ||
import { difference, intersection, sortBy, uniq } from "lodash-es"; | ||
import { DeclarationStorage, ForeignKeyOption, MigrationFileBuilder } from "../models/index.js"; | ||
@@ -49,3 +49,3 @@ import { doQuery, setConnection } from "./mysql.logic.js"; | ||
} | ||
const migrationName = `${version}.${getDateFormat()}_${name}`; | ||
const migrationName = `${getDateFormat()}_${name}`; | ||
console.log("• Retrieving current schema..."); | ||
@@ -468,3 +468,2 @@ const currentSchema = DeclarationStorage.get() | ||
return __awaiter(this, arguments, void 0, function* (migrationLocationPath, version = null) { | ||
var _a, _b, _c; | ||
const migrationLocation = path.join(process.cwd(), migrationLocationPath, "migrations"); | ||
@@ -480,8 +479,8 @@ console.log("• Update database requested."); | ||
for (const folder of folders) { | ||
const vStr = folder.split(".").find(x => x); | ||
if (vStr === undefined || isNaN(+vStr)) { | ||
const v = folder.split("_").find(x => x); | ||
if (v === undefined) { | ||
continue; | ||
} | ||
versions.push({ | ||
version: +vStr, | ||
version: v !== null && v !== void 0 ? v : '', | ||
name: folder | ||
@@ -497,16 +496,25 @@ }); | ||
} | ||
let currentVersion = null; | ||
// let currentVersion: number | null = null; | ||
// await setConnection(); | ||
// const nodeCountRes = await doQuery<{ nodeCount: number }>('SELECT count(*) as nodeCount FROM information_schema.TABLES WHERE TABLE_NAME = \'__myNodeORM\''); | ||
// if ((nodeCountRes.find(x => x)?.nodeCount ?? 0) > 0) { | ||
// const versionRes = await doQuery<{ version: number }>('SELECT version FROM __myNodeORM ORDER BY date DESC LIMIT 1;'); | ||
// currentVersion = versionRes.find(x => x)?.version as number; | ||
// } | ||
// | ||
// if (currentVersion != null) { | ||
// versions = versions.filter(v => v.version > currentVersion); | ||
// } | ||
// | ||
// if (version != null) { | ||
// versions = versions.filter(v => v.version <= version); | ||
// } | ||
// TODO Collect all version available | ||
yield setConnection(); | ||
const nodeCountRes = yield doQuery('SELECT count(*) as nodeCount FROM information_schema.TABLES WHERE TABLE_NAME = \'__myNodeORM\''); | ||
if (((_b = (_a = nodeCountRes.find(x => x)) === null || _a === void 0 ? void 0 : _a.nodeCount) !== null && _b !== void 0 ? _b : 0) > 0) { | ||
const versionRes = yield doQuery('SELECT version FROM __myNodeORM ORDER BY version DESC LIMIT 1;'); | ||
currentVersion = (_c = versionRes.find(x => x)) === null || _c === void 0 ? void 0 : _c.version; | ||
} | ||
if (currentVersion != null) { | ||
versions = versions.filter(v => v.version > currentVersion); | ||
} | ||
if (version != null) { | ||
versions = versions.filter(v => v.version <= version); | ||
} | ||
for (const v of orderBy(versions, version => version)) { | ||
const [ivs] = yield doQuery('SELECT * FROM __myNodeORM'); | ||
const unappliedVersion = versions.map(v => v.version).filter((item) => !(ivs !== null && ivs !== void 0 ? ivs : []).map(x => x.version).includes(item)); | ||
for (const v of versions) { | ||
if (!unappliedVersion.includes(v.version)) { | ||
continue; | ||
} | ||
console.log("• migrate to version " + v.version + "."); | ||
@@ -535,9 +543,26 @@ let migrationPlanPath = path.join(migrationLocation, v.name, "migration-plan.js"); | ||
const date = new Date(); | ||
const year = date.getFullYear() | ||
.toString(); | ||
const month = (date.getMonth() + 1).toString(); | ||
const day = date.getDate() | ||
.toString(); | ||
return year + month + day; | ||
const year = date.getFullYear().toString().padStart(4, '0'); | ||
const month = (date.getMonth() + 1).toString().padStart(2, '0'); | ||
const day = date.getDate().toString().padStart(2, '0'); | ||
const hours = date.getHours().toString().padStart(2, '0'); | ||
const minutes = date.getMinutes().toString().padStart(2, '0'); | ||
const seconds = date.getSeconds().toString().padStart(2, '0'); | ||
return `${year}${month}${day}${hours}${minutes}${seconds}`; | ||
} | ||
function stringToDate(dateString) { | ||
if (dateString.length !== 14) { | ||
return null; // Ongeldig formaat | ||
} | ||
const year = parseInt(dateString.substring(0, 4), 10); | ||
const month = parseInt(dateString.substring(4, 6), 10) - 1; | ||
const day = parseInt(dateString.substring(6, 8), 10); | ||
const hours = parseInt(dateString.substring(8, 10), 10); | ||
const minutes = parseInt(dateString.substring(10, 12), 10); | ||
const seconds = parseInt(dateString.substring(12, 14), 10); | ||
const date = new Date(year, month, day, hours, minutes, seconds); | ||
if (isNaN(date.getTime())) { | ||
return null; | ||
} | ||
return date; | ||
} | ||
//# sourceMappingURL=migration.logic.js.map |
@@ -0,0 +0,0 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
@@ -7,3 +7,3 @@ export declare abstract class MigrationFileBuilder { | ||
addQuery(query: string): void; | ||
execute(version: number): Promise<void>; | ||
execute(version: string): Promise<void>; | ||
} |
@@ -17,3 +17,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
private _builder = new MigrationBuilder(); | ||
private _version = {{{{VERSION}}}} | ||
private _version = "{{{{VERSION}}}}"; | ||
@@ -52,12 +52,8 @@ public async migrate() { | ||
try { | ||
if (version === 0) { | ||
yield connection.execute("DROP TABLE IF EXISTS __myNodeORM;"); | ||
yield connection.execute("CREATE TABLE __myNodeORM (version INT NOT NULL, date DATETIME NOT NULL DEFAULT NOW());"); | ||
yield connection.execute(`INSERT INTO __myNodeORM (version) | ||
VALUES (${version});`); | ||
const [countRows] = yield connection.query("SELECT COUNT(*) as count FROM information_schema.TABLES WHERE TABLE_NAME = '__myNodeORM';"); | ||
const count = countRows[0].count; | ||
if (count <= 0) { | ||
yield connection.execute("CREATE TABLE __myNodeORM (version VARCHAR(255) NOT NULL, date DATETIME NOT NULL DEFAULT NOW());"); | ||
} | ||
else { | ||
yield connection.execute(`INSERT INTO __myNodeORM (version) | ||
VALUES (${version});`); | ||
} | ||
yield connection.execute(`INSERT INTO __myNodeORM (version) VALUES (${version});`); | ||
yield connection.commit(); | ||
@@ -64,0 +60,0 @@ console.log(`✅ Migration ${version} executed successfully.`); |
{ | ||
"name": "@nyffels/mynodeorm", | ||
"version": "1.0.0-alpha146", | ||
"version": "1.0.0-alpha147", | ||
"description": "A full-fledged ORM framework for NodeJS and MySQL with develop friendly code aimed to handle database migrations, MySQL Query builder / helper and property mapping.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -0,0 +0,0 @@ # MyNodeORM |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
201708
1.29%2786
0.76%