|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs'; |
| 4 | +import { resolve, dirname, join } from 'node:path'; |
| 5 | +import { execSync } from 'node:child_process'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +async function main() { |
| 11 | + let migrationPath = null; |
| 12 | + const args = process.argv.slice(2); |
| 13 | + |
| 14 | + // Parse arguments |
| 15 | + let migrationName = 'install-eql'; |
| 16 | + let drizzleDir = 'drizzle'; |
| 17 | + let showHelp = false; |
| 18 | + |
| 19 | + for (let i = 0; i < args.length; i++) { |
| 20 | + const arg = args[i]; |
| 21 | + if (arg === '--help' || arg === '-h') { |
| 22 | + showHelp = true; |
| 23 | + } else if (arg === '--name' || arg === '-n') { |
| 24 | + migrationName = args[++i]; |
| 25 | + } else if (arg === '--out' || arg === '-o') { |
| 26 | + drizzleDir = args[++i]; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (showHelp) { |
| 31 | + console.log(` |
| 32 | +Usage: generate-eql-migration [options] |
| 33 | +
|
| 34 | +Generate a Drizzle migration that installs CipherStash EQL |
| 35 | +
|
| 36 | +Options: |
| 37 | + -n, --name <name> Migration name (default: "install-eql") |
| 38 | + -o, --out <dir> Output directory (default: "drizzle") |
| 39 | + -h, --help Display this help message |
| 40 | +
|
| 41 | +Examples: |
| 42 | + npx generate-eql-migration |
| 43 | + npx generate-eql-migration --name setup-eql |
| 44 | + npx generate-eql-migration --out migrations |
| 45 | + |
| 46 | + # Or with your package manager: |
| 47 | + pnpm generate-eql-migration |
| 48 | + yarn generate-eql-migration |
| 49 | + bun generate-eql-migration |
| 50 | +`); |
| 51 | + process.exit(0); |
| 52 | + } |
| 53 | + |
| 54 | + console.log('🔐 Generating EQL migration for Drizzle...\n'); |
| 55 | + |
| 56 | + // Step 1: Generate custom migration with drizzle-kit |
| 57 | + try { |
| 58 | + console.log(`📝 Generating custom migration: ${migrationName}`); |
| 59 | + execSync(`npx drizzle-kit generate --custom --name=${migrationName}`, { |
| 60 | + stdio: 'inherit' |
| 61 | + }); |
| 62 | + } catch (error) { |
| 63 | + console.error('❌ Failed to generate custom migration'); |
| 64 | + console.error('Make sure drizzle-kit is installed in your project.'); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + // Step 2: Find the SQL file from @cipherstash/schema package |
| 70 | + const schemaPackagePath = resolve(__dirname, '../../schema'); |
| 71 | + const sqlFileName = 'cipherstash-encrypt-2-1-8.sql'; |
| 72 | + const sqlSourcePath = join(schemaPackagePath, sqlFileName); |
| 73 | + |
| 74 | + if (!existsSync(sqlSourcePath)) { |
| 75 | + throw new Error(`Could not find EQL SQL file at: ${sqlSourcePath}`); |
| 76 | + } |
| 77 | + |
| 78 | + // Step 3: Read the EQL SQL content |
| 79 | + const eqlSql = readFileSync(sqlSourcePath, 'utf-8'); |
| 80 | + |
| 81 | + // Step 4: Find the generated migration file and write EQL SQL to it |
| 82 | + // Drizzle generates migrations in format: 0001_migration_name.sql |
| 83 | + const drizzlePath = resolve(process.cwd(), drizzleDir); |
| 84 | + |
| 85 | + if (!existsSync(drizzlePath)) { |
| 86 | + throw new Error(`Drizzle directory not found: ${drizzlePath}\nMake sure to run this command from your project root.`); |
| 87 | + } |
| 88 | + |
| 89 | + // Find the latest migration file with the specified name |
| 90 | + const fs = await import('node:fs/promises'); |
| 91 | + const files = await fs.readdir(drizzlePath); |
| 92 | + const migrationFile = files |
| 93 | + .filter(f => f.endsWith('.sql') && f.includes(migrationName)) |
| 94 | + .sort() |
| 95 | + .pop(); |
| 96 | + |
| 97 | + if (!migrationFile) { |
| 98 | + throw new Error(`Could not find migration file for: ${migrationName}\nLooked in: ${drizzlePath}`); |
| 99 | + } |
| 100 | + |
| 101 | + migrationPath = join(drizzlePath, migrationFile); |
| 102 | + console.log(`\n📄 Writing EQL SQL to: ${migrationFile}`); |
| 103 | + |
| 104 | + writeFileSync(migrationPath, eqlSql, 'utf-8'); |
| 105 | + |
| 106 | + console.log(`\n✅ Successfully created EQL migration!`); |
| 107 | + console.log(`\nNext steps:`); |
| 108 | + console.log(` 1. Review the migration: ${migrationPath}`); |
| 109 | + console.log(` 2. Run migrations: npx drizzle-kit migrate`); |
| 110 | + console.log(` (or use your package manager: pnpm/yarn/bun drizzle-kit migrate)`); |
| 111 | + } catch (error) { |
| 112 | + // Cleanup: remove the migration file if it was created |
| 113 | + if (migrationPath && existsSync(migrationPath)) { |
| 114 | + try { |
| 115 | + unlinkSync(migrationPath); |
| 116 | + console.error(`\n🗑️ Cleaned up migration file: ${migrationPath}`); |
| 117 | + } catch (cleanupError) { |
| 118 | + console.error(`\n⚠️ Failed to cleanup migration file: ${migrationPath}`); |
| 119 | + } |
| 120 | + } |
| 121 | + throw error; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +main().catch(error => { |
| 126 | + console.error('❌ Error:', error.message); |
| 127 | + process.exit(1); |
| 128 | +}); |
0 commit comments