|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +const cwd = process.cwd(); |
| 5 | +const upPath = path.join(cwd, 'migrations', '0001_create_api_keys_and_vaults.up.sql'); |
| 6 | +const downPath = path.join(cwd, 'migrations', '0001_create_api_keys_and_vaults.down.sql'); |
| 7 | + |
| 8 | +function assertMatch(sql, regex, message) { |
| 9 | + if (!regex.test(sql)) { |
| 10 | + throw new Error(message); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function assertNoMatch(sql, regex, message) { |
| 15 | + if (regex.test(sql)) { |
| 16 | + throw new Error(message); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const up = fs.readFileSync(upPath, 'utf8'); |
| 21 | +const down = fs.readFileSync(downPath, 'utf8'); |
| 22 | + |
| 23 | +assertMatch(up, /create table api_keys/i, 'api_keys table is missing'); |
| 24 | +assertMatch(up, /\buser_id\b/i, 'api_keys.user_id is missing'); |
| 25 | +assertMatch(up, /\bapi_id\b/i, 'api_keys.api_id is missing'); |
| 26 | +assertMatch(up, /\bkey_hash\b/i, 'api_keys.key_hash is missing'); |
| 27 | +assertMatch(up, /\bprefix\b/i, 'api_keys.prefix is missing'); |
| 28 | +assertMatch(up, /\bscopes\b/i, 'api_keys.scopes is missing'); |
| 29 | +assertMatch(up, /\brate_limit_per_minute\b/i, 'api_keys.rate_limit_per_minute is missing'); |
| 30 | +assertMatch(up, /\bcreated_at\b/i, 'api_keys.created_at is missing'); |
| 31 | +assertMatch(up, /\blast_used_at\b/i, 'api_keys.last_used_at is missing'); |
| 32 | +assertMatch(up, /unique\s*\(\s*user_id\s*,\s*api_id\s*\)/i, 'api_keys unique(user_id, api_id) is missing'); |
| 33 | +assertMatch( |
| 34 | + up, |
| 35 | + /create index idx_api_keys_user_prefix on api_keys\s*\(\s*user_id\s*,\s*prefix\s*\)/i, |
| 36 | + 'api_keys index(user_id, prefix) is missing' |
| 37 | +); |
| 38 | +assertNoMatch(up, /\bapi_key\b/i, 'raw api_key column detected'); |
| 39 | +assertNoMatch(up, /\braw_key\b/i, 'raw_key column detected'); |
| 40 | + |
| 41 | +assertMatch(up, /create table vaults/i, 'vaults table is missing'); |
| 42 | +assertMatch(up, /\buser_id\b/i, 'vaults.user_id is missing'); |
| 43 | +assertMatch(up, /\bstellar_vault_contract_id\b/i, 'vaults.stellar_vault_contract_id is missing'); |
| 44 | +assertMatch(up, /\bnetwork\b/i, 'vaults.network is missing'); |
| 45 | +assertMatch(up, /\bbalance_snapshot\b/i, 'vaults.balance_snapshot is missing'); |
| 46 | +assertMatch(up, /\blast_synced_at\b/i, 'vaults.last_synced_at is missing'); |
| 47 | +assertMatch(up, /\bcreated_at\b/i, 'vaults.created_at is missing'); |
| 48 | +assertMatch(up, /\bupdated_at\b/i, 'vaults.updated_at is missing'); |
| 49 | +assertMatch(up, /unique\s*\(\s*user_id\s*,\s*network\s*\)/i, 'vaults unique(user_id, network) is missing'); |
| 50 | + |
| 51 | +assertMatch(down, /drop table if exists vaults/i, 'down migration must drop vaults'); |
| 52 | +assertMatch(down, /drop table if exists api_keys/i, 'down migration must drop api_keys'); |
| 53 | + |
| 54 | +console.log('Issue #9 migration validation passed.'); |
0 commit comments