diff --git a/fastapi_book_server/app/alembic/env.py b/fastapi_book_server/app/alembic/env.py index 9844b85..cd89223 100644 --- a/fastapi_book_server/app/alembic/env.py +++ b/fastapi_book_server/app/alembic/env.py @@ -53,7 +53,7 @@ def run_migrations_online(): with connectable.connect() as connection: context.configure( - connection=connection, target_metadata=target_metadata + connection=connection, target_metadata=target_metadata, compare_type=True ) with context.begin_transaction(): diff --git a/fastapi_book_server/app/alembic/versions/6d0dbf1e4998_.py b/fastapi_book_server/app/alembic/versions/6d0dbf1e4998_.py deleted file mode 100644 index ab15fea..0000000 --- a/fastapi_book_server/app/alembic/versions/6d0dbf1e4998_.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: 6d0dbf1e4998 -Revises: 9c4b98440632 -Create Date: 2021-11-07 20:23:39.656518 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '6d0dbf1e4998' -down_revision = '9c4b98440632' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_unique_constraint('uc_sequence_infos_sequence_position', 'sequence_infos', ['sequence', 'position']) - op.create_unique_constraint('uc_translations_book_position', 'translations', ['book', 'position']) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint('uc_translations_book_position', 'translations', type_='unique') - op.drop_constraint('uc_sequence_infos_sequence_position', 'sequence_infos', type_='unique') - # ### end Alembic commands ### diff --git a/fastapi_book_server/app/alembic/versions/9c4b98440632_.py b/fastapi_book_server/app/alembic/versions/9c4b98440632_.py deleted file mode 100644 index 105d293..0000000 --- a/fastapi_book_server/app/alembic/versions/9c4b98440632_.py +++ /dev/null @@ -1,35 +0,0 @@ -"""empty message - -Revision ID: 9c4b98440632 -Revises: d172032adeaf -Create Date: 2021-11-07 16:21:08.210333 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '9c4b98440632' -down_revision = 'd172032adeaf' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('books_authors', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('author', sa.Integer(), nullable=True), - sa.Column('book', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['author'], ['authors.id'], name='fk_books_authors_authors_author_id', onupdate='CASCADE', ondelete='CASCADE'), - sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_books_authors_books_book_id', onupdate='CASCADE', ondelete='CASCADE'), - sa.PrimaryKeyConstraint('id') - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('books_authors') - # ### end Alembic commands ### diff --git a/fastapi_book_server/app/alembic/versions/d172032adeaf_.py b/fastapi_book_server/app/alembic/versions/b44117a41998_.py similarity index 57% rename from fastapi_book_server/app/alembic/versions/d172032adeaf_.py rename to fastapi_book_server/app/alembic/versions/b44117a41998_.py index 963dded..6805c18 100644 --- a/fastapi_book_server/app/alembic/versions/d172032adeaf_.py +++ b/fastapi_book_server/app/alembic/versions/b44117a41998_.py @@ -1,16 +1,17 @@ """empty message -Revision ID: d172032adeaf +Revision ID: b44117a41998 Revises: -Create Date: 2021-11-05 17:22:11.717389 +Create Date: 2021-11-18 18:25:06.921287 """ from alembic import op import sqlalchemy as sa +from sqlalchemy.sql.schema import UniqueConstraint # revision identifiers, used by Alembic. -revision = 'd172032adeaf' +revision = 'b44117a41998' down_revision = None branch_labels = None depends_on = None @@ -40,13 +41,25 @@ def upgrade(): sa.Column('source', sa.SmallInteger(), nullable=False), sa.Column('remote_id', sa.Integer(), nullable=False), sa.Column('title', sa.String(length=256), nullable=False), - sa.Column('lang', sa.String(length=2), nullable=False), + sa.Column('lang', sa.String(length=3), nullable=False), sa.Column('file_type', sa.String(length=4), nullable=False), sa.Column('uploaded', sa.Date(), nullable=False), + sa.Column('is_deleted', sa.Boolean(), server_default=sa.text('false'), nullable=False), sa.ForeignKeyConstraint(['source'], ['sources.id'], name='fk_books_sources_id_source'), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('source', 'remote_id', name='uc_books_source_remote_id') ) + op.create_table('genres', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('source', sa.SmallInteger(), nullable=False), + sa.Column('remote_id', sa.Integer(), nullable=False), + sa.Column('code', sa.String(length=45), nullable=False), + sa.Column('description', sa.String(length=99), nullable=False), + sa.Column('meta', sa.String(length=45), nullable=False), + sa.ForeignKeyConstraint(['source'], ['sources.id'], name='fk_genres_sources_id_source'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('source', 'remote_id', name='uc_genres_source_remote_id') + ) op.create_table('sequences', sa.Column('id', sa.Integer(), nullable=False), sa.Column('source', sa.SmallInteger(), nullable=False), @@ -76,25 +89,43 @@ def upgrade(): sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('book') ) - op.create_table('sequence_infos', + op.create_table('book_authors', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('book', sa.Integer(), nullable=False), - sa.Column('sequence', sa.Integer(), nullable=False), - sa.Column('position', sa.SmallInteger(), nullable=False), - sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_sequence_infos_books_id_book'), - sa.ForeignKeyConstraint(['sequence'], ['sequences.id'], name='fk_sequence_infos_sequences_id_sequence'), + sa.Column('author', sa.Integer(), nullable=True), + sa.Column('book', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['author'], ['authors.id'], name='fk_book_authors_authors_author_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_book_authors_books_book_id', onupdate='CASCADE', ondelete='CASCADE'), sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('book', 'sequence', name='uc_sequence_infos_book_sequence') + sa.UniqueConstraint('book', 'author', name='uc_book_authors_book_author'), + ) + op.create_table('book_genres', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('genre', sa.Integer(), nullable=True), + sa.Column('book', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_book_genres_books_book_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.ForeignKeyConstraint(['genre'], ['genres.id'], name='fk_book_genres_genres_genre_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('book', 'genre', name='uc_book_genres_book_genre'), + ) + op.create_table('book_sequences', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('position', sa.SmallInteger(), nullable=False), + sa.Column('sequence', sa.Integer(), nullable=True), + sa.Column('book', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_book_sequences_books_book_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.ForeignKeyConstraint(['sequence'], ['sequences.id'], name='fk_book_sequences_sequences_sequence_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('book', 'sequence', name='uc_book_sequences_book_sequence'), ) op.create_table('translations', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('book', sa.Integer(), nullable=False), - sa.Column('translator', sa.Integer(), nullable=False), sa.Column('position', sa.SmallInteger(), nullable=False), - sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_translations_books_id_book'), - sa.ForeignKeyConstraint(['translator'], ['authors.id'], name='fk_translations_authors_id_translator'), + sa.Column('author', sa.Integer(), nullable=True), + sa.Column('book', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['author'], ['authors.id'], name='fk_translations_authors_author_id', onupdate='CASCADE', ondelete='CASCADE'), + sa.ForeignKeyConstraint(['book'], ['books.id'], name='fk_translations_books_book_id', onupdate='CASCADE', ondelete='CASCADE'), sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('book', 'translator', name='uc_translations_book_translator') + sa.UniqueConstraint('book', 'author', name='uc_translations_book_author'), ) # ### end Alembic commands ### @@ -102,10 +133,13 @@ def upgrade(): def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_table('translations') - op.drop_table('sequence_infos') + op.drop_table('book_sequences') + op.drop_table('book_genres') + op.drop_table('book_authors') op.drop_table('book_annotations') op.drop_table('author_annotations') op.drop_table('sequences') + op.drop_table('genres') op.drop_table('books') op.drop_table('authors') op.drop_table('sources') diff --git a/fastapi_book_server/app/models.py b/fastapi_book_server/app/models.py index 953c331..251d265 100644 --- a/fastapi_book_server/app/models.py +++ b/fastapi_book_server/app/models.py @@ -1,6 +1,7 @@ from datetime import date import ormar +from sqlalchemy import text from core.db import metadata, database @@ -19,6 +20,23 @@ class Source(ormar.Model): name: str = ormar.String(max_length=32, nullable=False, unique=True) # type: ignore +class Genre(ormar.Model): + class Meta(BaseMeta): + tablename = "genres" + constraints = [ + ormar.UniqueColumns("source", "remote_id"), + ] + + id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore + + source: Source = ormar.ForeignKey(Source, nullable=False) + remote_id: int = ormar.Integer(minimum=0, nullable=False) # type: ignore + + code: str = ormar.String(max_length=45, nullable=False) # type: ignore + description: str = ormar.String(max_length=99, nullable=False) # type: ignore + meta: str = ormar.String(max_length=45, nullable=False) # type: ignore + + class Author(ormar.Model): class Meta(BaseMeta): tablename = "authors" @@ -33,7 +51,7 @@ class Author(ormar.Model): first_name: str = ormar.String(max_length=256, nullable=False) # type: ignore last_name: str = ormar.String(max_length=256, nullable=False) # type: ignore - middle_name: str = ormar.String(max_length=256, nullable=True, default="") # type: ignore + middle_name: str = ormar.String(max_length=256, nullable=True) # type: ignore class AuthorAnnotation(ormar.Model): @@ -49,54 +67,6 @@ class AuthorAnnotation(ormar.Model): file: str = ormar.String(max_length=256, nullable=True) # type: ignore -class Book(ormar.Model): - class Meta(BaseMeta): - tablename = "books" - constraints = [ - ormar.UniqueColumns("source", "remote_id"), - ] - - id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore - - source: Source = ormar.ForeignKey(Source, nullable=False) - remote_id: int = ormar.Integer(minimum=0, nullable=False) # type: ignore - - title: str = ormar.String(max_length=256, nullable=False) # type: ignore - lang: str = ormar.String(max_length=2, nullable=False) # type: ignore - file_type: str = ormar.String(max_length=4, nullable=False) # type: ignore - uploaded: date = ormar.Date() # type: ignore - - authors = ormar.ManyToMany(Author) - - -class BookAnnotation(ormar.Model): - class Meta(BaseMeta): - tablename = "book_annotations" - - id = ormar.Integer(primary_key=True, nullable=False) - - book: Book = ormar.ForeignKey(Book, nullable=False, unique=True) - - title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore - text: str = ormar.Text(nullable=False, default="") # type: ignore - file: str = ormar.String(max_length=256, nullable=True) # type: ignore - - -class Translation(ormar.Model): - class Meta(BaseMeta): - tablename = "translations" - constraints = [ - ormar.UniqueColumns("book", "translator"), - ormar.UniqueColumns("book", "position"), - ] - - id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore - - book: Book = ormar.ForeignKey(Book, nullable=False) - translator: Author = ormar.ForeignKey(Author, nullable=False) - position: int = ormar.SmallInteger(nullable=False) # type: ignore - - class Sequence(ormar.Model): class Meta(BaseMeta): tablename = "sequences" @@ -112,16 +82,73 @@ class Sequence(ormar.Model): name: str = ormar.String(max_length=256, nullable=False) # type: ignore -class SequenceInfo(ormar.Model): +class BookAuthors(ormar.Model): class Meta(BaseMeta): - tablename = "sequence_infos" + tablename = "book_authors" + + id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore + + +class BookGenres(ormar.Model): + class Meta(BaseMeta): + tablename = "book_genres" + + id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore + + +class BookSequences(ormar.Model): + class Meta(BaseMeta): + tablename = "book_sequences" + + id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore + + position: int = ormar.SmallInteger(minimum=0, nullable=False) # type: ignore + + +class Translation(ormar.Model): + class Meta(BaseMeta): + tablename = "translations" constraints = [ - ormar.UniqueColumns("book", "sequence"), - ormar.UniqueColumns("sequence", "position"), + # ormar.UniqueColumns("book", "translator"), ] id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore - book: Book = ormar.ForeignKey(Book, nullable=False) - sequence: Sequence = ormar.ForeignKey(Sequence, nullable=False) - position: int = ormar.SmallInteger(minimum=0, nullable=False) # type: ignore + position: int = ormar.SmallInteger(nullable=False) # type: ignore + + +class Book(ormar.Model): + class Meta(BaseMeta): + tablename = "books" + constraints = [ + ormar.UniqueColumns("source", "remote_id"), + ] + + id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore + + source: Source = ormar.ForeignKey(Source, nullable=False) + remote_id: int = ormar.Integer(minimum=0, nullable=False) # type: ignore + + title: str = ormar.String(max_length=256, nullable=False) # type: ignore + lang: str = ormar.String(max_length=3, nullable=False) # type: ignore + file_type: str = ormar.String(max_length=4, nullable=False) # type: ignore + uploaded: date = ormar.Date() # type: ignore + is_deleted: bool = ormar.Boolean(default=False, server_default=text("false"), nullable=False) + + authors = ormar.ManyToMany(Author, through=BookAuthors) + translators = ormar.ManyToMany(Author, through=Translation, related_name="translated_books") + genres = ormar.ManyToMany(Genre, through=BookGenres) + sequences = ormar.ManyToMany(Sequence, through=BookSequences) + + +class BookAnnotation(ormar.Model): + class Meta(BaseMeta): + tablename = "book_annotations" + + id = ormar.Integer(primary_key=True, nullable=False) + + book: Book = ormar.ForeignKey(Book, nullable=False, unique=True) + + title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore + text: str = ormar.Text(nullable=False, default="") # type: ignore + file: str = ormar.String(max_length=256, nullable=True) # type: ignore diff --git a/poetry.lock b/poetry.lock index e0ecac9..b75de76 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,3 +1,14 @@ +[[package]] +name = "aiologger" +version = "0.6.1" +description = "Asynchronous logging for python and asyncio" +category = "main" +optional = false +python-versions = ">=3.6" + +[package.extras] +aiofiles = ["aiofiles (==0.4.0)"] + [[package]] name = "aiosqlite" version = "0.17.0" @@ -156,13 +167,13 @@ pydantic = ">=1.7.2" [package.extras] gino = ["gino[starlette] (>=1.0.1)", "SQLAlchemy (>=1.3.20)"] -all = ["gino[starlette] (>=1.0.1)", "SQLAlchemy (>=1.3.20)", "databases[mysql,postgresql,sqlite] (>=0.4.0)", "orm (>=0.1.5)", "tortoise-orm[aiosqlite,asyncpg,aiomysql] (>=0.16.18,<0.18.0)", "asyncpg (>=0.24.0)", "ormar (>=0.10.5)", "Django (<3.3.0)", "piccolo (>=0.29,<0.35)", "motor (>=2.5.1,<3.0.0)"] +all = ["gino[starlette] (>=1.0.1)", "SQLAlchemy (>=1.3.20)", "databases[mysql,postgresql,sqlite] (>=0.4.0)", "orm (>=0.1.5)", "tortoise-orm[asyncpg,aiomysql,aiosqlite] (>=0.16.18,<0.18.0)", "asyncpg (>=0.24.0)", "ormar (>=0.10.5)", "Django (<3.3.0)", "piccolo (>=0.29,<0.35)", "motor (>=2.5.1,<3.0.0)"] sqlalchemy = ["SQLAlchemy (>=1.3.20)"] asyncpg = ["SQLAlchemy (>=1.3.20)", "asyncpg (>=0.24.0)"] databases = ["databases[mysql,postgresql,sqlite] (>=0.4.0)"] orm = ["databases[mysql,postgresql,sqlite] (>=0.4.0)", "orm (>=0.1.5)", "typesystem (>=0.2.0,<0.3.0)"] django = ["databases[mysql,postgresql,sqlite] (>=0.4.0)", "Django (<3.3.0)"] -tortoise = ["tortoise-orm[aiosqlite,asyncpg,aiomysql] (>=0.16.18,<0.18.0)"] +tortoise = ["tortoise-orm[asyncpg,aiomysql,aiosqlite] (>=0.16.18,<0.18.0)"] ormar = ["ormar (>=0.10.5)"] piccolo = ["piccolo (>=0.29,<0.35)"] motor = ["motor (>=2.5.1,<3.0.0)"] @@ -431,9 +442,12 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "c4ef74338cbe7b2849d529fc28d7ca77e8f7fa8cb3dc0b490b3ebf99cb40d9f3" +content-hash = "919b54dfe0b4ebf05cdfc1e60ae3e29af5278390aa68443b1332d96daf2b3f75" [metadata.files] +aiologger = [ + {file = "aiologger-0.6.1.tar.gz", hash = "sha256:1b6b8f00d74a588339b657ff60ffa9f64c53873887a008934c66e1a673ea68cd"}, +] aiosqlite = [ {file = "aiosqlite-0.17.0-py3-none-any.whl", hash = "sha256:6c49dc6d3405929b1d08eeccc72306d3677503cc5e5e43771efc1e00232e8231"}, {file = "aiosqlite-0.17.0.tar.gz", hash = "sha256:f0e6acc24bc4864149267ac82fb46dfb3be4455f99fe21df82609cc6e6baee51"}, diff --git a/pyproject.toml b/pyproject.toml index 2636aea..d89b7e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ alembic = "^1.7.4" asyncpg = "^0.24.0" psycopg2 = "^2.9.1" fastapi-pagination = {extras = ["ormar"], version = "^0.9.0"} +aiologger = "^0.6.1" [tool.poetry.dev-dependencies] pytest = "^5.2"