diff --git a/package.json b/package.json index 2978483448..08f77016fb 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,8 @@ "security:audit": "node scripts/audit.js", "postinstall": "husky install", "submodules:load": "git submodule init && git submodule update && yarn", - "submodules:unload": "git submodule deinit --all && yarn" + "submodules:unload": "git submodule deinit --all && yarn", + "add-app-migration": "node scripts/add-app-migration.js" }, "workspaces": { "packages": [ diff --git a/packages/server/src/appMigrations/migrations.ts b/packages/server/src/appMigrations/migrations.ts new file mode 100644 index 0000000000..a39f679a17 --- /dev/null +++ b/packages/server/src/appMigrations/migrations.ts @@ -0,0 +1,3 @@ +// This file should never be manually modified, use `yarn add-app-migration` in order to add a new one + +export const MIGRATIONS: Record Promise }> = {} diff --git a/scripts/add-app-migration.js b/scripts/add-app-migration.js new file mode 100644 index 0000000000..8d56701033 --- /dev/null +++ b/scripts/add-app-migration.js @@ -0,0 +1,69 @@ +const fs = require("fs") +const path = require("path") + +const generateTimestamp = () => { + const now = new Date() + const year = now.getFullYear() + const month = String(now.getMonth() + 1).padStart(2, "0") + const day = String(now.getDate()).padStart(2, "0") + const hours = String(now.getHours()).padStart(2, "0") + const minutes = String(now.getMinutes()).padStart(2, "0") + const seconds = String(now.getSeconds()).padStart(2, "0") + + return `${year}${month}${day}${hours}${minutes}${seconds}` +} + +const createMigrationFile = () => { + const timestamp = generateTimestamp() + const migrationsDir = "../packages/server/src/appMigrations" + + const template = `const migration = async () => { + // Add your migration logic here +} + +export default migration +` + + const newMigrationPath = path.join( + migrationsDir, + "migrations", + `${timestamp}.ts` + ) + fs.writeFileSync(path.resolve(__dirname, newMigrationPath), template) + + console.log(`New migration created: ${newMigrationPath}`) + + // Append the new migration to the main migrations file + const migrationsFilePath = path.join(migrationsDir, "migrations.ts") + + const migrationDir = fs.readdirSync( + path.join(__dirname, migrationsDir, "migrations") + ) + const migrations = migrationDir + .filter(m => m.endsWith(".ts")) + .map(m => m.substring(0, m.length - 3)) + + let migrationFileContent = + "// This file should never be manually modified, use `yarn add-app-migration` in order to add a new one\n\n" + + for (const migration of migrations) { + migrationFileContent += `import m${migration} from "./migrations/${migration}"\n` + } + + migrationFileContent += `\nexport const MIGRATIONS: Record Promise }> = {\n` + + for (const migration of migrations) { + migrationFileContent += ` [${migration}]: { migration: m${migration} },\n` + } + + migrationFileContent += `}\n` + + fs.writeFileSync( + path.resolve(__dirname, migrationsFilePath), + migrationFileContent + ) + + console.log(`Main migrations file updated: ${migrationsFilePath}`) +} + +createMigrationFile()