@mtg-tracker/common 1.0.0
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/build/functions/runMigrations.js +60 -0
- package/build/index.js +17 -0
- package/package.json +32 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.runMigrations = runMigrations;
|
|
16
|
+
const fs_1 = __importDefault(require("fs"));
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
function runMigrations(pool) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
console.log('Running database migrations...');
|
|
21
|
+
// Create migrations tracking table if it doesn't exist
|
|
22
|
+
yield pool.query(`
|
|
23
|
+
CREATE TABLE IF NOT EXISTS migrations_users (
|
|
24
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
25
|
+
filename VARCHAR(255) NOT NULL UNIQUE,
|
|
26
|
+
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
27
|
+
)
|
|
28
|
+
`);
|
|
29
|
+
// Get all migration files
|
|
30
|
+
const migrationsDir = path_1.default.join(__dirname);
|
|
31
|
+
const files = fs_1.default.readdirSync(migrationsDir)
|
|
32
|
+
.filter(file => file.endsWith('_up.sql'))
|
|
33
|
+
.sort(); // Sort to ensure correct order
|
|
34
|
+
// Check which migrations have already been run
|
|
35
|
+
const [executedMigrations] = yield pool.query('SELECT filename FROM migrations_users');
|
|
36
|
+
const executedFiles = new Set(executedMigrations.map(row => row.filename));
|
|
37
|
+
// Run pending migrations
|
|
38
|
+
for (const file of files) {
|
|
39
|
+
if (executedFiles.has(file)) {
|
|
40
|
+
console.log(`Migration ${file} already executed, skipping...`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
console.log(`Running migration: ${file}`);
|
|
44
|
+
const filePath = path_1.default.join(migrationsDir, file);
|
|
45
|
+
const sql = fs_1.default.readFileSync(filePath, 'utf-8');
|
|
46
|
+
try {
|
|
47
|
+
// Execute the migration
|
|
48
|
+
yield pool.query(sql);
|
|
49
|
+
// Record that this migration was executed
|
|
50
|
+
yield pool.query('INSERT INTO migrations_users (filename) VALUES (?)', [file]);
|
|
51
|
+
console.log(`Migration ${file} completed successfully`);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error(`Error running migration ${file}:`, error);
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
console.log('All migrations completed');
|
|
59
|
+
});
|
|
60
|
+
}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./functions/runMigrations"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mtg-tracker/common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"clean": "del ./build/*",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"publish": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish --access public && git push && git push --tags"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "zhamburglar",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"del-cli": "^7.0.0",
|
|
20
|
+
"typescript": "^5.9.3"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@types/cookie-session": "^2.0.49",
|
|
24
|
+
"@types/express": "^4.17.25",
|
|
25
|
+
"@types/jsonwebtoken": "^9.0.7",
|
|
26
|
+
"cookie-session": "^2.1.0",
|
|
27
|
+
"express": "^4.21.0",
|
|
28
|
+
"express-validator": "^7.2.0",
|
|
29
|
+
"jsonwebtoken": "^9.0.2",
|
|
30
|
+
"mysql2": "^3.15.3"
|
|
31
|
+
}
|
|
32
|
+
}
|