Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions schema/create_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ CREATE TABLE IF NOT EXISTS release (
CREATE TABLE IF NOT EXISTS release_artist (
release_id integer NOT NULL REFERENCES release(id) ON DELETE CASCADE,
artist_name text NOT NULL,
extra integer DEFAULT 0, -- 0 = main artist, 1 = extra credit
UNIQUE (release_id, artist_name)
extra integer DEFAULT 0 -- 0 = main artist, 1 = extra credit
);

-- Tracks on releases
Expand All @@ -47,8 +46,7 @@ CREATE TABLE IF NOT EXISTS release_track (
CREATE TABLE IF NOT EXISTS release_track_artist (
release_id integer NOT NULL REFERENCES release(id) ON DELETE CASCADE,
track_sequence integer NOT NULL,
artist_name text NOT NULL,
UNIQUE (release_id, track_sequence, artist_name)
artist_name text NOT NULL
);

-- ============================================
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ def test_fk_constraints_with_cascade(self) -> None:
}
assert expected_fk_tables.issubset(fk_tables)

def test_no_unique_constraints_on_child_tables(self) -> None:
"""Child tables must not have UNIQUE constraints (Python-level dedup handles this).

UNIQUE constraints on text columns cause btree overflow when artist_name
exceeds ~900 bytes. Dedup is handled by import_csv.py's unique_key filtering.
"""
conn = self._connect()
with conn.cursor() as cur:
cur.execute("""
SELECT tc.table_name, tc.constraint_name
FROM information_schema.table_constraints tc
WHERE tc.constraint_type = 'UNIQUE'
AND tc.table_name IN ('release_artist', 'release_track_artist')
""")
unique_constraints = cur.fetchall()
conn.close()
assert unique_constraints == [], f"Unexpected UNIQUE constraints: {unique_constraints}"

def test_schema_is_idempotent(self) -> None:
"""Running the schema twice doesn't error (IF NOT EXISTS)."""
conn = psycopg.connect(self.db_url, autocommit=True)
Expand Down