54 lines
2.8 KiB
JavaScript
54 lines
2.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { globSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import test from 'node:test';
|
|
|
|
const prisma = fileURLToPath(new URL('../prisma/', import.meta.url));
|
|
const logTables = new Map([
|
|
['MaterialLog', 'material_logs'],
|
|
['MaterialDownloadLog', 'material_download_logs'],
|
|
]);
|
|
|
|
test('material log schemas keep the source identifier without a foreign key', () => {
|
|
const schema = readFileSync(join(prisma, 'schema.prisma'), 'utf8');
|
|
for (const model of logTables.keys()) {
|
|
const body = schema.match(new RegExp('^model ' + model + ' \\{([\\s\\S]*?)^\\}', 'm'))?.[1];
|
|
assert.ok(body, model + ' must remain in the schema');
|
|
assert.match(body, /^\s*materialId\s+\w+/m);
|
|
assert.doesNotMatch(body, /@relation\([^)]*\bfields\s*:\s*\[[^\]]*\bmaterialId\b/s);
|
|
assert.doesNotMatch(body, /^\s*materialId\b[^\r\n]*@relation/m);
|
|
}
|
|
});
|
|
|
|
function materialLogForeignKeys(sql) {
|
|
const tables = [...logTables.values()].join('|');
|
|
const ddl = new RegExp(String.raw`\b(?:ALTER|CREATE)\s+TABLE\s+(?:(?:IF\s+(?:NOT\s+)?EXISTS|ONLY)\s+)*(?:(?:"[\w]+"|\w+)\.)?"?(` + tables + String.raw`)"?\s`, 'i');
|
|
const foreignKey = /\bFOREIGN\s+KEY\s*\([^)]*\bmaterial_?id\b[^)]*\)\s*REFERENCES\b/i;
|
|
const inlineReference = /(?:\(|,|\bADD\s+(?:COLUMN\s+)?)\s*"?material_?id"?\s+[^,;]*?\bREFERENCES\b/i;
|
|
return sql.replace(/\/\*[\s\S]*?\*\/|--[^\r\n]*/g, '').split(';').flatMap(statement => {
|
|
const table = statement.match(ddl)?.[1];
|
|
return table && (foreignKey.test(statement) || inlineReference.test(statement)) ? [table.toLowerCase()] : [];
|
|
});
|
|
}
|
|
|
|
test('accepted historical migrations do not gain new material-log foreign keys', () => {
|
|
const historical = 'migrations/20251206000000_add_soft_delete_and_favorites/migration.sql';
|
|
for (const file of globSync('migrations/**/*.sql', { cwd: prisma })) {
|
|
const relative = file.replaceAll('\\', '/');
|
|
const actual = materialLogForeignKeys(readFileSync(join(prisma, file), 'utf8')).sort();
|
|
const expected = relative === historical ? [...logTables.values()].sort() : [];
|
|
assert.deepEqual(actual, expected, relative);
|
|
}
|
|
});
|
|
|
|
test('migration check detects ALTER and inline CREATE foreign keys on either log table', () => {
|
|
assert.deepEqual(materialLogForeignKeys([
|
|
'ALTER TABLE ONLY "public"."material_logs" ADD CONSTRAINT new_key FOREIGN KEY ("materialId") REFERENCES "materials"("id")',
|
|
'CREATE TABLE material_download_logs (material_id TEXT REFERENCES materials(id))',
|
|
'ALTER TABLE material_logs DROP CONSTRAINT material_id_fkey',
|
|
'ALTER TABLE material_logs ADD FOREIGN KEY ("userId") REFERENCES users(id)',
|
|
'-- ALTER TABLE material_logs ADD FOREIGN KEY ("materialId") REFERENCES materials(id)\nSELECT 1',
|
|
].join(';\n')), ['material_logs', 'material_download_logs']);
|
|
});
|