Skip to content

Commit

Permalink
chore: validate all tables have primary keys (#6005)
Browse files Browse the repository at this point in the history
## About the changes
This is a helpful reminder to have primary keys in all our tables.

Related to [#4303](#4303)
  • Loading branch information
gastonfournier authored Jan 23, 2024
1 parent 82ac5a4 commit 9f55033
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/test/e2e/migrator.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { IDBOption } from '../../lib/types';

log.setLogLevel('error');

const schema = 'up_n_down_migrations_test';

async function initSchema(db: IDBOption): Promise<void> {
const client = new Client(db);
await client.connect();
Expand All @@ -15,13 +17,42 @@ async function initSchema(db: IDBOption): Promise<void> {
await client.end();
}

async function validateTablesHavePrimaryKeys(db: IDBOption) {
const client = new Client(db);
await client.connect();
const tables = await client.query<{ table_name: string }>(
`SELECT
t.table_name
FROM
information_schema.tables t
LEFT JOIN
information_schema.table_constraints tc ON t.table_schema = tc.table_schema
AND t.table_name = tc.table_name
AND tc.constraint_type = 'PRIMARY KEY'
WHERE
t.table_type = 'BASE TABLE'
AND t.table_schema = '${schema}'
AND t.table_schema NOT IN ('pg_catalog', 'information_schema')
AND tc.constraint_name IS NULL;
`,
);
await client.end();
if ((tables.rowCount ?? 0) > 0) {
throw new Error(
`The following tables do not have a primary key defined: ${tables.rows
.map((r) => r.table_name)
.join(', ')}`,
);
}
}

test('Up & down migrations work', async () => {
jest.setTimeout(15000);
const config = createTestConfig({
db: {
...getDbConfig(),
pool: { min: 1, max: 4 },
schema: 'up_n_down_migrations_test',
schema: schema,
ssl: false,
},
});
Expand All @@ -42,5 +73,6 @@ test('Up & down migrations work', async () => {
});

await dbm.up();
await validateTablesHavePrimaryKeys(config.db);
await dbm.reset();
});

0 comments on commit 9f55033

Please sign in to comment.