Skip to content

Commit

Permalink
Merge pull request #114 from eoyilmaz/112-shotscenes-should-be-shotscene
Browse files Browse the repository at this point in the history
[#112] Shot and Scene relation is now many-to-one.
  • Loading branch information
eoyilmaz authored Nov 18, 2024
2 parents 89e251b + 7e03437 commit feef80f
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import sqlalchemy as sa



# revision identifiers, used by Alembic.
revision = '019378697b5b'
down_revision = 'feca9bac7d5a'
revision = "019378697b5b"
down_revision = "feca9bac7d5a"


def upgrade():
Expand Down
36 changes: 17 additions & 19 deletions alembic/versions/4400871fa852_scene_is_now_deriving_from_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import sqlalchemy as sa



# revision identifiers, used by Alembic.
revision = '4400871fa852'
down_revision = 'ec1eb2151bb9'
revision = "4400871fa852"
down_revision = "ec1eb2151bb9"


def upgrade():
Expand Down Expand Up @@ -57,21 +56,24 @@ def upgrade():
)
)
# Insert the same data to the Entities
op.execute("""
op.execute(
"""
INSERT INTO "Entities" (id) VALUES (
(SELECT "SimpleEntities".id FROM "SimpleEntities" WHERE "SimpleEntities".name = 'Scene Statuses')
)
""")
"""
)
# Insert the same to the StatusLists
op.execute(
"""INSERT INTO "StatusLists" (id, target_entity_type) VALUES (
"""INSERT INTO "StatusLists" (id, target_entity_type) VALUES (
(SELECT "SimpleEntities".id FROM "SimpleEntities" WHERE "SimpleEntities".name = 'Scene Statuses'),
'Scene'
)
""")
"""
)
# Create the same StatusList -> Status relation of a Task
op.execute(
"""INSERT INTO "StatusList_Statuses" (status_list_id, status_id)
"""INSERT INTO "StatusList_Statuses" (status_list_id, status_id)
SELECT
"SimpleEntities".id,
"StatusList_Statuses".status_id
Expand All @@ -87,7 +89,8 @@ def upgrade():
# we need create a Task for each Scene in the database,
# with the same id of the Scene
# carry on the data: id, project_id
op.execute("""
op.execute(
"""
INSERT INTO "Tasks" (
id,
project_id,
Expand All @@ -107,7 +110,8 @@ def upgrade():
'effort',
0
FROM "Scenes"
""")
"""
)

# drop the project_id column in Scenes table
with op.batch_alter_table("Scenes", schema=None) as batch_op:
Expand All @@ -129,9 +133,7 @@ def downgrade():
# set the project_id column not nullable
op.execute("""ALTER TABLE "Scenes" ALTER COLUMN project_id SET NOT NULL""")
# Remove the scene entries from Tasks table
op.execute(
"""DELETE FROM "Tasks" WHERE id IN (SELECT id FROM "Scenes")"""
)
op.execute("""DELETE FROM "Tasks" WHERE id IN (SELECT id FROM "Scenes")""")
# Remove the StatusList entries from StatusList_Statuses
op.execute(
"""DELETE FROM "StatusList_Statuses" WHERE status_list_id = (
Expand All @@ -141,9 +143,7 @@ def downgrade():
"""
)
# Remove the StatusList from StatusLists Table
op.execute(
"""DELETE FROM "StatusLists" WHERE target_entity_type = 'Scene'"""
)
op.execute("""DELETE FROM "StatusLists" WHERE target_entity_type = 'Scene'""")
# Remove the StatusList from Entities Table
op.execute(
"""DELETE FROM "Entities" WHERE id IN (
Expand All @@ -155,9 +155,7 @@ def downgrade():
"""
)
# Remove the StatusList from SimpleEntities Table
op.execute(
"""DELETE FROM "SimpleEntities" WHERE name = 'Scene Statuses'"""
)
op.execute("""DELETE FROM "SimpleEntities" WHERE name = 'Scene Statuses'""")

# Update the Scenes.id to be a foreign key to Entities.id
op.drop_constraint("Scenes_id_fkey", "Scenes", type_="foreignkey")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Shot Scene relation is now many-to-one
Revision ID: 5078390e5527
Revises: e25ec9930632
Create Date: 2024-11-18 11:35:10.872216
"""

from alembic import op

import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "5078390e5527"
down_revision = "e25ec9930632"


def upgrade():
"""Upgrade the tables."""
# Add scene_id column
op.add_column("Shots", sa.Column("scene_id", sa.Integer(), nullable=True))

# Create foreign key constraint
op.create_foreign_key(None, "Shots", "Scenes", ["scene_id"], ["id"])

# Migrate the data
op.execute(
"""UPDATE "Shots" SET scene_id = (
SELECT scene_id
FROM "Shot_Scenes"
WHERE "Shot_Scenes".shot_id = "Shots".id LIMIT 1
)"""
)

# Drop Shot_Scenes Table
op.execute("""DROP TABLE "Shot_Scenes" """)


def downgrade():
"""Downgrade the tables."""
# Add Shot_Scenes Table
op.create_table(
"Shot_Scenes",
sa.Column("shot_id", sa.Integer(), nullable=False),
sa.Column("scene_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["shot_id"],
["Shots.id"],
),
sa.ForeignKeyConstraint(
["scene_id"],
["Scenes.id"],
),
)

# Transfer Data
op.execute(
"""
UPDATE "Shot_Scenes" SET shot_id, scene_id = (
SELECT id, scene_id FROM "Shots" WHERE "Shots".scene_id != NULL
)
"""
)

# Drop foreign key constraint
op.drop_constraint("Shots_scene_id_fkey", "Shots", type_="foreignkey")

# drop Shots.scene_id column
op.drop_column("Shots", "scene_id")
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,34 @@


# revision identifiers, used by Alembic.
revision = 'e25ec9930632'
down_revision = '4400871fa852'
revision = "e25ec9930632"
down_revision = "4400871fa852"


def upgrade():
"""Upgrade the tables."""
# Add sequence_id column
op.add_column('Shots', sa.Column('sequence_id', sa.Integer(), nullable=True))
# Add sequence_id column
op.add_column("Shots", sa.Column("sequence_id", sa.Integer(), nullable=True))

# Create foreign key constraint
op.create_foreign_key(None, 'Shots', 'Sequences', ['sequence_id'], ['id'])
# Create foreign key constraint
op.create_foreign_key(None, "Shots", "Sequences", ["sequence_id"], ["id"])

# Migrate the data
op.execute("""UPDATE "Shots" SET sequence_id = (
# Migrate the data
op.execute(
"""UPDATE "Shots" SET sequence_id = (
SELECT sequence_id
FROM "Shot_Sequences"
WHERE "Shot_Sequences".shot_id = "Shots".id LIMIT 1
)""")
)"""
)

# Drop Shot_Sequences Table
op.execute("""DROP TABLE "Shot_Sequences" """)


def downgrade():
"""Downgrade the tables."""
# Add Shot_Sequences Table
# Add Shot_Sequences Table
op.create_table(
"Shot_Sequences",
sa.Column("shot_id", sa.Integer(), nullable=False),
Expand All @@ -51,15 +53,17 @@ def downgrade():
),
)

# Transfer Data
op.execute("""
# Transfer Data
op.execute(
"""
UPDATE "Shot_Sequences" SET shot_id, sequence_id = (
SELECT id, sequence_id FROM "Shots" WHERE "Shots".sequence_id != NULL
)
""")
"""
)

# Drop foreign key constraint
op.drop_constraint("Shots_sequence_id_fkey", "Shots", type_="foreignkey")

# drop Shots.sequence_id column
op.drop_column("Shots", "sequence_id")
op.drop_column("Shots", "sequence_id")
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@
import sqlalchemy as sa



# revision identifiers, used by Alembic.
revision = 'ec1eb2151bb9'
down_revision = '019378697b5b'
revision = "ec1eb2151bb9"
down_revision = "019378697b5b"


def upgrade():
"""Upgrade the tables."""
op.alter_column(
"Versions", "take_name", new_column_name="variant_name"
)
op.alter_column("Versions", "take_name", new_column_name="variant_name")


def downgrade():
"""Downgrade the tables."""
op.alter_column(
"Versions", "variant_name", new_column_name="take_name"
)
op.alter_column("Versions", "variant_name", new_column_name="take_name")
5 changes: 2 additions & 3 deletions alembic/versions/feca9bac7d5a_renamed_osx_to_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import sqlalchemy as sa



# revision identifiers, used by Alembic.
revision = 'feca9bac7d5a'
down_revision = 'bf67e6a234b4'
revision = "feca9bac7d5a"
down_revision = "bf67e6a234b4"


def upgrade():
Expand Down
6 changes: 3 additions & 3 deletions examples/flat_project_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
flat_struct = Structure(
name="Flat Project Structure",
templates=[flat_task_template] # we need another template for Assets,
# Shots and Sequences but I'm skipping it
# for now
# Shots and Sequences but I'm skipping it
# for now
)

# query a couple of statuses
Expand Down Expand Up @@ -155,7 +155,7 @@

# now create new tasks for the normal project
seq1 = Sequence(name="Sequence", code="SEQ001", project=p2)
shot1 = Shot(name="SEQ001_0010", code="SEQ001_0010", parent=seq1, sequences=[seq1])
shot1 = Shot(name="SEQ001_0010", code="SEQ001_0010", parent=seq1, sequence=seq1)
comp = Task(name="Comp", parent=shot1)
# you probably will supply a different name/code

Expand Down
2 changes: 1 addition & 1 deletion src/stalker/db/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
logger: logging.Logger = log.get_logger(__name__)

# TODO: Try to get it from the API (it was not working inside a package before)
alembic_version: str = "e25ec9930632"
alembic_version: str = "5078390e5527"


def setup(settings: Optional[Dict[str, Any]] = None) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/stalker/models/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Scene(Task, CodeMixin):
)

shots: Mapped[Optional[List["Shot"]]] = relationship(
secondary="Shot_Scenes",
back_populates="scenes",
primaryjoin="Shots.c.scene_id==Scenes.c.id",
back_populates="scene",
doc="""The :class:`.Shot` s that is related with this Scene.
It is a list of :class:`.Shot` instances.
Expand Down
Loading

0 comments on commit feef80f

Please sign in to comment.