Skip to content

Commit 4d0d0c2

Browse files
fix: add NOT NULL constraints to artist_library_crossreference FK columns
Junction table FK columns were nullable, which is meaningless for a cross-reference and also defeats the unique index (NULL != NULL in SQL). Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 108d1bf commit 4d0d0c2

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

shared/database/src/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ export type ArtistLibraryCrossreference = InferSelectModel<typeof artist_library
369369
export const artist_library_crossreference = wxyc_schema.table(
370370
'artist_library_crossreference',
371371
{
372-
artist_id: integer('artist_id').references(() => artists.id),
373-
library_id: integer('library_id').references(() => library.id),
372+
artist_id: integer('artist_id').references(() => artists.id).notNull(),
373+
library_id: integer('library_id').references(() => library.id).notNull(),
374374
},
375375
(table) => [uniqueIndex('library_id_artist_id').on(table.artist_id, table.library_id)]
376376
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
const schemaSource = fs.readFileSync(
5+
path.resolve(__dirname, '../../../shared/database/src/schema.ts'),
6+
'utf-8'
7+
);
8+
9+
function extractTableBlock(source: string, tableName: string): string {
10+
const pattern = new RegExp(
11+
`export const ${tableName}\\s*=\\s*wxyc_schema\\.table\\([\\s\\S]*?\\);`
12+
);
13+
const match = source.match(pattern);
14+
if (!match) throw new Error(`Table "${tableName}" not found in schema.ts`);
15+
return match[0];
16+
}
17+
18+
describe('artist_library_crossreference schema', () => {
19+
const block = extractTableBlock(schemaSource, 'artist_library_crossreference');
20+
21+
it('artist_id column has .notNull()', () => {
22+
const artistIdLine = block
23+
.split('\n')
24+
.find((line) => line.includes("artist_id:") || line.includes("'artist_id'"));
25+
expect(artistIdLine).toBeDefined();
26+
expect(artistIdLine).toContain('.notNull()');
27+
});
28+
29+
it('library_id column has .notNull()', () => {
30+
const libraryIdLine = block
31+
.split('\n')
32+
.find((line) => line.includes("library_id:") || line.includes("'library_id'"));
33+
expect(libraryIdLine).toBeDefined();
34+
expect(libraryIdLine).toContain('.notNull()');
35+
});
36+
});

0 commit comments

Comments
 (0)