Skip to content

Commit cc94872

Browse files
committed
simplified migration, one line indices for albums.
1 parent 324a99f commit cc94872

File tree

3 files changed

+17
-34
lines changed

3 files changed

+17
-34
lines changed

beets/dbcore/db.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,19 +1225,11 @@ def _migrate_indices(
12251225
indices are created or replaced.
12261226
"""
12271227
with self.transaction() as tx:
1228-
index_rows = tx.query(f"PRAGMA index_list({table})")
1229-
current_indices = {Index.from_db(tx, row[1]) for row in index_rows}
1230-
1231-
_indices = set(indices)
1232-
1233-
if current_indices.issuperset(_indices):
1234-
return
1235-
1236-
# May also include missing indices.
1237-
changed_indices = _indices - current_indices
1238-
1239-
with self.transaction() as tx:
1240-
for index in changed_indices:
1228+
current = {
1229+
Index.from_db(tx, r[1])
1230+
for r in tx.query(f"PRAGMA index_list({table})")
1231+
}
1232+
for index in set(indices) - current:
12411233
index.recreate(tx, table)
12421234

12431235
# Querying.
@@ -1317,22 +1309,18 @@ class Index(NamedTuple):
13171309
"""
13181310

13191311
name: str
1320-
columns: Sequence[str]
1312+
columns: tuple[str, ...]
13211313

13221314
def recreate(self, tx: Transaction, table: str) -> None:
13231315
"""Recreate the index in the database.
13241316
13251317
This is useful when the index has been changed and needs to be
13261318
updated.
13271319
"""
1328-
tx.script(f"DROP INDEX IF EXISTS {self.name}")
1329-
self.create(tx, table)
1330-
1331-
def create(self, tx: Transaction, table: str) -> None:
1332-
"""Create the index in the database."""
1333-
return tx.script(
1334-
f"CREATE INDEX {self.name} ON {table} ({', '.join(self.columns)})"
1335-
)
1320+
tx.script(f"""
1321+
DROP INDEX IF EXISTS {self.name};
1322+
CREATE INDEX {self.name} ON {table} ({", ".join(self.columns)})
1323+
""")
13361324

13371325
@classmethod
13381326
def from_db(cls, tx: Transaction, name: str) -> Index:
@@ -1342,7 +1330,7 @@ def from_db(cls, tx: Transaction, name: str) -> Index:
13421330
Error will be raised.
13431331
"""
13441332
rows = tx.query(f"PRAGMA index_info({name})")
1345-
columns = [row[2] for row in rows]
1333+
columns = tuple(row[2] for row in rows)
13461334
return cls(name, columns)
13471335

13481336
def __hash__(self) -> int:

beets/library/models.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,7 @@ class Item(LibModel):
716716
"mtime": types.DATE,
717717
"added": types.DATE,
718718
}
719-
_indices = (
720-
dbcore.Index(
721-
name="idx_item_album_id",
722-
columns=("album_id",),
723-
),
724-
)
719+
_indices = (dbcore.Index("idx_item_album_id", ("album_id",)),)
725720

726721
_search_fields = (
727722
"artist",

test/test_dbcore.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,12 @@ def db(self):
800800
@pytest.fixture
801801
def sample_index(self):
802802
"""Fixture for a sample Index object."""
803-
return Index(name="sample_index", columns=["field_one"])
803+
return Index(name="sample_index", columns=("field_one",))
804804

805805
def test_index_creation(self, db, sample_index):
806806
"""Test creating an index and checking its existence."""
807807
with db.transaction() as tx:
808-
sample_index.create(tx, "test")
808+
sample_index.recreate(tx, "test")
809809
indexes = (
810810
db._connection().execute("PRAGMA index_list(test)").fetchall()
811811
)
@@ -814,18 +814,18 @@ def test_index_creation(self, db, sample_index):
814814
def test_from_db(self, db, sample_index):
815815
"""Test retrieving an index from the database."""
816816
with db.transaction() as tx:
817-
sample_index.create(tx, "test")
817+
sample_index.recreate(tx, "test")
818818
retrieved = Index.from_db(tx, sample_index.name)
819819
assert retrieved == sample_index
820820

821821
def test_index_hashing_and_set_behavior(self, sample_index):
822822
"""Test the hashing and set behavior of the Index class."""
823823
index_set = {sample_index}
824-
similar_index = Index(name="sample_index", columns=["field_one"])
824+
similar_index = Index(name="sample_index", columns=("field_one",))
825825

826826
assert similar_index in index_set # Should recognize similar attributes
827827

828-
different_index = Index(name="other_index", columns=["other_field"])
828+
different_index = Index(name="other_index", columns=("other_field",))
829829
index_set.add(different_index)
830830

831831
assert len(index_set) == 2 # Should recognize distinct index

0 commit comments

Comments
 (0)