mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Add genres, recreate migrations
This commit is contained in:
@@ -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():
|
||||
|
||||
@@ -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 ###
|
||||
@@ -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 ###
|
||||
@@ -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')
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user