Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 6 additions & 4 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class BaseAppSettings(BaseSettings):
BASE_DIR: Path = Path(__file__).parent.parent
PATH_TO_DB: str = str(BASE_DIR / "database" / "source" / "theater.db")
PATH_TO_MOVIES_CSV: str = str(BASE_DIR / "database" / "seed_data" / "imdb_movies.csv")
PATH_TO_MOVIES_CSV: str = str(
BASE_DIR / "database" / "seed_data" / "imdb_movies.csv"
)


class Settings(BaseAppSettings):
Expand All @@ -22,11 +24,11 @@ class Settings(BaseAppSettings):
class TestingSettings(BaseAppSettings):

def model_post_init(self, __context: dict[str, Any] | None = None) -> None:
object.__setattr__(self, 'PATH_TO_DB', ":memory:")
object.__setattr__(self, "PATH_TO_DB", ":memory:")
object.__setattr__(
self,
'PATH_TO_MOVIES_CSV',
str(self.BASE_DIR / "database" / "seed_data" / "test_data.csv")
"PATH_TO_MOVIES_CSV",
str(self.BASE_DIR / "database" / "seed_data" / "test_data.csv"),
)


Expand Down
9 changes: 3 additions & 6 deletions src/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import os

from database.models import (
Base,
MovieModel
)
from database.models import Base, MovieModel
from database.session_sqlite import reset_sqlite_database as reset_database

environment = os.getenv("ENVIRONMENT", "developing")

if environment == "testing":
from database.session_sqlite import (
get_sqlite_db_contextmanager as get_db_contextmanager,
get_sqlite_db as get_db
get_sqlite_db as get_db,
)
else:
from database.session_postgresql import (
get_postgresql_db_contextmanager as get_db_contextmanager,
get_postgresql_db as get_db
get_postgresql_db as get_db,
)
6 changes: 3 additions & 3 deletions src/database/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from alembic import context

from database import models # noqa: F401
from database import models # noqa: F401
from database.models import Base
from database.session_postgresql import postgresql_engine

Expand Down Expand Up @@ -47,7 +47,7 @@ def run_migrations_offline() -> None:
connection=connection,
target_metadata=target_metadata,
compare_type=True,
compare_server_default=True
compare_server_default=True,
)

with context.begin_transaction():
Expand All @@ -68,7 +68,7 @@ def run_migrations_online() -> None:
connection=connection,
target_metadata=target_metadata,
compare_type=True,
compare_server_default=True
compare_server_default=True,
)

with context.begin_transaction():
Expand Down
142 changes: 80 additions & 62 deletions src/database/migrations/versions/ea3a65568bd9_initial_migration.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,114 @@
"""initial migration

Revision ID: ea3a65568bd9
Revises:
Revises:
Create Date: 2025-01-02 21:07:13.284837

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'ea3a65568bd9'
revision: str = "ea3a65568bd9"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('actors',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
op.create_table(
"actors",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_table('countries',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('code', sa.String(length=3), nullable=False),
sa.Column('name', sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code')
op.create_table(
"countries",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=3), nullable=False),
sa.Column("name", sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("code"),
)
op.create_table('genres',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
op.create_table(
"genres",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_table('languages',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
op.create_table(
"languages",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_table('movies',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('date', sa.Date(), nullable=False),
sa.Column('score', sa.Float(), nullable=False),
sa.Column('overview', sa.Text(), nullable=False),
sa.Column('status', sa.Enum('RELEASED', 'POST_PRODUCTION', 'IN_PRODUCTION', name='moviestatusenum'), nullable=False),
sa.Column('budget', sa.DECIMAL(precision=15, scale=2), nullable=False),
sa.Column('revenue', sa.Float(), nullable=False),
sa.Column('country_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['country_id'], ['countries.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name', 'date', name='unique_movie_constraint')
op.create_table(
"movies",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("date", sa.Date(), nullable=False),
sa.Column("score", sa.Float(), nullable=False),
sa.Column("overview", sa.Text(), nullable=False),
sa.Column(
"status",
sa.Enum(
"RELEASED", "POST_PRODUCTION", "IN_PRODUCTION", name="moviestatusenum"
),
nullable=False,
),
sa.Column("budget", sa.DECIMAL(precision=15, scale=2), nullable=False),
sa.Column("revenue", sa.Float(), nullable=False),
sa.Column("country_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["country_id"],
["countries.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name", "date", name="unique_movie_constraint"),
)
op.create_table('actors_movies',
sa.Column('movie_id', sa.Integer(), nullable=False),
sa.Column('actor_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['actor_id'], ['actors.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('movie_id', 'actor_id')
op.create_table(
"actors_movies",
sa.Column("movie_id", sa.Integer(), nullable=False),
sa.Column("actor_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(["actor_id"], ["actors.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("movie_id", "actor_id"),
)
op.create_table('movies_genres',
sa.Column('movie_id', sa.Integer(), nullable=False),
sa.Column('genre_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['genre_id'], ['genres.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('movie_id', 'genre_id')
op.create_table(
"movies_genres",
sa.Column("movie_id", sa.Integer(), nullable=False),
sa.Column("genre_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(["genre_id"], ["genres.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("movie_id", "genre_id"),
)
op.create_table('movies_languages',
sa.Column('movie_id', sa.Integer(), nullable=False),
sa.Column('language_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['language_id'], ['languages.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('movie_id', 'language_id')
op.create_table(
"movies_languages",
sa.Column("movie_id", sa.Integer(), nullable=False),
sa.Column("language_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(["language_id"], ["languages.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("movie_id", "language_id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('movies_languages')
op.drop_table('movies_genres')
op.drop_table('actors_movies')
op.drop_table('movies')
op.drop_table('languages')
op.drop_table('genres')
op.drop_table('countries')
op.drop_table('actors')
op.drop_table("movies_languages")
op.drop_table("movies_genres")
op.drop_table("actors_movies")
op.drop_table("movies")
op.drop_table("languages")
op.drop_table("genres")
op.drop_table("countries")
op.drop_table("actors")
# ### end Alembic commands ###
Loading