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

Add a unique index to genres.name. #794

Merged
merged 1 commit into from
Feb 2, 2018
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
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']
Copy link
Contributor Author

@leonardr leonardr Feb 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change to make sure an OPDS import from unglue.it doesn't try to download PDFs from Google Books. (It's impossible for an automated client to get a PDF out of Google Books, so no point in trying.)


@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