Skip to content

Commit

Permalink
🗃️ changed UUID to String(36) for IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
agn-7 committed Nov 18, 2023
1 parent be1fd54 commit feb9a77
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
83 changes: 83 additions & 0 deletions alembic/versions/1df6ff5028e4_change_uuid_to_str_for_pks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""change UUID to Str for PKs
Revision ID: 1df6ff5028e4
Revises: b77bb559f678
Create Date: 2023-11-18 12:02:17.505398
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "1df6ff5028e4"
down_revision = "b77bb559f678"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###

# Drop the constraint
op.execute('ALTER TABLE message DROP CONSTRAINT message_interaction_id_fkey')

# Change the type of 'id' in 'interaction' table
op.alter_column(
"interaction",
"id",
existing_type=postgresql.UUID(),
type_=sa.String(length=36),
existing_nullable=False,
)

# Change the type of 'id' and 'interaction_id' in 'message' table
op.alter_column(
"message",
"id",
existing_type=postgresql.UUID(),
type_=sa.String(length=36),
existing_nullable=False,
)
op.alter_column(
"message",
"interaction_id",
existing_type=postgresql.UUID(),
type_=sa.String(),
existing_nullable=True,
)

# Add the constraints back
op.create_unique_constraint(None, "interaction", ["id"])
op.create_unique_constraint(None, "message", ["id"])
op.execute('ALTER TABLE message ADD CONSTRAINT message_interaction_id_fkey FOREIGN KEY(interaction_id) REFERENCES interaction(id)')

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "message", type_="unique")
op.alter_column(
"message",
"interaction_id",
existing_type=sa.String(),
type_=postgresql.UUID(),
existing_nullable=True,
)
op.alter_column(
"message",
"id",
existing_type=sa.String(length=36),
type_=postgresql.UUID(),
existing_nullable=False,
)
op.drop_constraint(None, "interaction", type_="unique")
op.alter_column(
"interaction",
"id",
existing_type=sa.String(length=36),
type_=postgresql.UUID(),
existing_nullable=False,
)
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions ifsguid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def __tablename__(cls) -> str:
return str(cls.__name__.lower())

id = Column(
UUID(as_uuid=True),
String(36),
primary_key=True,
default=uuid.uuid4,
default=lambda: str(uuid.uuid4()),
unique=True,
nullable=False,
)
Expand All @@ -51,6 +51,6 @@ class Message(Base):
created_at = Column(DateTime, index=True)
role = Column(String, index=True)
content = Column(String)
interaction_id = Column(UUID(as_uuid=True), ForeignKey("interaction.id"))
interaction_id = Column(String, ForeignKey("interaction.id"))

interaction = relationship("Interaction", back_populates="messages")

0 comments on commit feb9a77

Please sign in to comment.