UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

95 lines (94 loc) 5.39 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDumpForMigration = createDumpForMigration; exports.getSqLiteFileFromBackup = getSqLiteFileFromBackup; const child_process_1 = require("child_process"); const fs_1 = __importDefault(require("fs")); const util = __importStar(require("node:util")); const path_1 = __importStar(require("path")); const types_1 = require("../../types"); const logger_1 = require("../../util/logger"); const postgresql_1 = require("../drizzle/postgresql"); function createDumpForMigration(config) { const sqliteFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE); const backupFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE_BACKUP); if (!fs_1.default.existsSync(sqliteFilePath)) { (0, logger_1.log)('SQLite database not found, skipping migration dump creation'); return; } (0, logger_1.log)('Creating dump for migration from SQLite to PostgreSQL'); for (const table of Object.keys(postgresql_1.POSTGRESQL_SCHEMA)) { const tableName = postgresql_1.POSTGRESQL_SCHEMA[table]._.name; (0, logger_1.log)(`Creating dump for table ${tableName}`); const dumpFileName = util.format(types_1.storageFiles.DUMP, tableName); const dumpFilePath = path_1.default.join(config.dbFolder, dumpFileName); (0, child_process_1.execSync)(`sqlite3 ${sqliteFilePath} ".output ${dumpFilePath}" ".dump ${tableName}" ".output stdout"`); let modifiedContent = fs_1.default.readFileSync(dumpFilePath).toString(); // 1. Remove SQLite-specific PRAGMA statements modifiedContent = modifiedContent.replace(/PRAGMA foreign_keys=OFF;\n/g, ''); // 2. Adjust transaction syntax for PostgreSQL modifiedContent = modifiedContent.replace(/BEGIN TRANSACTION;\n/g, ''); modifiedContent = modifiedContent.replace(/COMMIT TRANSACTION;\n/g, ''); // 3. Ensure tables are only created if they don't already exist modifiedContent = modifiedContent .replace(/CREATE TABLE IF NOT EXISTS/g, 'CREATE TABLE') // Remove duplicate IF NOT EXISTS .replace(/CREATE TABLE/g, 'CREATE TABLE IF NOT EXISTS'); // Add IF NOT EXISTS if missing // 4. Add `ON CONFLICT DO NOTHING` to INSERT statements to handle conflicts gracefully modifiedContent = modifiedContent.replace(/(INSERT INTO [^;]+)(;)/g, '$1 ON CONFLICT DO NOTHING;'); // 5. Convert SQLite-specific data types to PostgreSQL equivalents modifiedContent = modifiedContent.replace(/integer not null primary key autoincrement/gi, 'serial primary key'); modifiedContent = modifiedContent.replace(/varchar not null primary key/gi, 'varchar primary key'); // 6. Remove SQLite-specific function replace() modifiedContent = modifiedContent.replace(/replace\s*\(\s*'([^']*(?:'{2}[^']*)*)',\s*'\\n',\s*char\s*\(\s*10\s*\)\s*\)/gi, "'$1'"); // 7. Convert SQLite backticks to PostgreSQL double quotes modifiedContent = modifiedContent.replace(/`([^`]+)`/g, '"$1"'); // 8. Remove VARCHAR length restrictions (VARCHAR(255) -> VARCHAR) modifiedContent = modifiedContent.replace(/VARCHAR\(\d+\)/gi, 'VARCHAR'); fs_1.default.writeFileSync(dumpFilePath, modifiedContent, { encoding: 'utf8', flag: 'w' }); } fs_1.default.renameSync(sqliteFilePath, backupFilePath); } function getSqLiteFileFromBackup(config) { const sqliteFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE); const backupFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE_BACKUP); if (fs_1.default.existsSync(backupFilePath) && !fs_1.default.existsSync(sqliteFilePath)) { (0, logger_1.log)('Restoring SQLite database from backup'); fs_1.default.renameSync(backupFilePath, sqliteFilePath); } }