|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { $ } from 'bun'; |
| 3 | +import { parseArgs } from 'util'; |
| 4 | +import { execSync } from 'node:child_process'; |
| 5 | + |
| 6 | +const VALID_NAME_REGEX = /^[a-z][a-z0-9_]*$/; |
| 7 | + |
| 8 | +function showUsage() { |
| 9 | + console.error('Usage: bun run generate-migrations [--name <descriptive-name>]'); |
| 10 | + console.error(''); |
| 11 | + console.error('If --name is omitted on a non-main branch, the name is derived from'); |
| 12 | + console.error('the branch name automatically. On main, --name is required.'); |
| 13 | + console.error(''); |
| 14 | + console.error('Examples:'); |
| 15 | + console.error( |
| 16 | + ' bun run generate-migrations # auto-derives name from branch' |
| 17 | + ); |
| 18 | + console.error(' bun run generate-migrations --name add_user_preferences'); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +const { values } = parseArgs({ |
| 23 | + args: process.argv.slice(2), |
| 24 | + options: { |
| 25 | + name: { type: 'string' }, |
| 26 | + }, |
| 27 | + strict: false, |
| 28 | + allowPositionals: true, |
| 29 | +}); |
| 30 | + |
| 31 | +let name = values.name as string | undefined; |
| 32 | + |
| 33 | +if (!name) { |
| 34 | + // Determine current branch |
| 35 | + let branch: string; |
| 36 | + try { |
| 37 | + branch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim(); |
| 38 | + } catch { |
| 39 | + console.error('Error: Could not determine current git branch.'); |
| 40 | + console.error('Please provide --name explicitly.'); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + if (branch === 'main' || branch === 'master') { |
| 45 | + console.error('Error: --name is required when running on the main/master branch.'); |
| 46 | + console.error('Automatic naming is only available on feature branches.'); |
| 47 | + showUsage(); |
| 48 | + } |
| 49 | + |
| 50 | + if (branch === 'HEAD') { |
| 51 | + console.error('Error: Detached HEAD detected. Please provide --name explicitly.'); |
| 52 | + showUsage(); |
| 53 | + } |
| 54 | + |
| 55 | + name = deriveNameFromBranch(branch); |
| 56 | + console.log(`No --name provided; derived migration name from branch: ${name}`); |
| 57 | +} |
| 58 | + |
| 59 | +if (!VALID_NAME_REGEX.test(name)) { |
| 60 | + console.error(`Error: Migration name "${name}" is invalid.`); |
| 61 | + console.error('Names must be snake_case: lowercase letters, numbers, and underscores only.'); |
| 62 | + console.error('They must start with a letter.'); |
| 63 | + process.exit(1); |
| 64 | +} |
| 65 | + |
| 66 | +console.log(`Generating SQLite migrations with name: ${name}`); |
| 67 | +await $`cd packages/backend && node node_modules/drizzle-kit/bin.cjs generate --name ${name} --config drizzle.config.sqlite.ts`; |
| 68 | + |
| 69 | +console.log(`Generating Postgres migrations with name: ${name}`); |
| 70 | +await $`cd packages/backend && node node_modules/drizzle-kit/bin.cjs generate --name ${name} --config drizzle.config.postgres.ts`; |
| 71 | + |
| 72 | +console.log('Done!'); |
| 73 | + |
| 74 | +/** |
| 75 | + * Derive a descriptive migration name from a git branch name. |
| 76 | + * |
| 77 | + * Examples: |
| 78 | + * pi/issue-424-1779050379120 → auto_issue_424 |
| 79 | + * feat/quota-checkers → auto_quota_checkers |
| 80 | + * fix/user-index → auto_user_index |
| 81 | + * 424-migration-naming → auto_424_migration_naming |
| 82 | + */ |
| 83 | +function deriveNameFromBranch(branch: string): string { |
| 84 | + // Strip common VCS/automation prefixes (pi/, feat/, fix/, feature/, bugfix/, etc.) |
| 85 | + let derived = branch.replace( |
| 86 | + /^(pi|feat|feature|fix|bugfix|hotfix|chore|refactor|docs|test|ci)\//, |
| 87 | + '' |
| 88 | + ); |
| 89 | + |
| 90 | + // Strip trailing long numeric hashes (e.g., 1779050379120) likely added by automation |
| 91 | + derived = derived.replace(/[-_]?\d{10,}$/, ''); |
| 92 | + |
| 93 | + // Replace non-alphanumeric characters with underscores |
| 94 | + derived = derived.replace(/[^a-zA-Z0-9]+/g, '_'); |
| 95 | + |
| 96 | + // Collapse multiple consecutive underscores |
| 97 | + derived = derived.replace(/_+/g, '_'); |
| 98 | + |
| 99 | + // Strip leading/trailing underscores |
| 100 | + derived = derived.replace(/^_+|_+$/g, ''); |
| 101 | + |
| 102 | + // Lowercase |
| 103 | + derived = derived.toLowerCase(); |
| 104 | + |
| 105 | + // Prefix with auto_ so lint-migrations recognizes it |
| 106 | + derived = `auto_${derived}`; |
| 107 | + |
| 108 | + // Safety check: if we ended up with just "auto_" or empty, fall back |
| 109 | + if (derived === 'auto_' || derived.length <= 5) { |
| 110 | + console.error(`Error: Could not derive a meaningful name from branch "${branch}".`); |
| 111 | + console.error('Please provide --name explicitly.'); |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + return derived; |
| 116 | +} |
0 commit comments