Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Add a unique index to genres.name.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardr committed Feb 2, 2018
1 parent b094d79 commit 112f203
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
10 changes: 10 additions & 0 deletions migration/20180202-genre-name-is-unique.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- genres.name should be indexed and unique.
DO $$
BEGIN
BEGIN
create unique index ix_genres_name on genres (name);
EXCEPTION
WHEN OTHERS THEN RAISE NOTICE 'WARNING: it looks like ix_genres_name already exists; it was probably created on initial database creation.';
END;
END;
$$;
4 changes: 2 additions & 2 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5876,7 +5876,7 @@ class Genre(Base, HasFullTableCache):
"""
__tablename__ = 'genres'
id = Column(Integer, primary_key=True)
name = Column(Unicode)
name = Column(Unicode, unique=True, index=True)

# One Genre may have affinity with many Subjects.
subjects = relationship("Subject", backref="genre")
Expand Down Expand Up @@ -8708,7 +8708,7 @@ def cautious_http_get(cls, url, headers, **kwargs):

# Sites that cause problems for us if we make automated
# HTTP requests to them while trying to find free books.
AVOID_WHEN_CAUTIOUS_DOMAINS = ['gutenberg.org']
AVOID_WHEN_CAUTIOUS_DOMAINS = ['gutenberg.org', 'books.google.com']

@classmethod
def get_would_be_useful(
Expand Down
9 changes: 8 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,14 @@ def exploding_create_hook():
# No exception.
eq_(drama, drama2)
eq_(False, is_new)


def test_name_is_unique(self):
g1, ignore = Genre.lookup(self._db, "A Genre", autocreate=True)
g2, ignore = Genre.lookup(self._db, "A Genre", autocreate=True)
eq_(g1, g2)

assert_raises(IntegrityError, create, self._db, Genre, name="A Genre")

def test_default_fiction(self):
sf, ignore = Genre.lookup(self._db, "Science Fiction")
nonfiction, ignore = Genre.lookup(self._db, "History")
Expand Down

0 comments on commit 112f203

Please sign in to comment.