Skip to content

Commit

Permalink
Made all Datetime timezone aware
Browse files Browse the repository at this point in the history
  • Loading branch information
Armolas committed Jul 19, 2024
1 parent 2697e0b commit 4a9c42c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 10 deletions.
30 changes: 30 additions & 0 deletions alembic/versions/9c01926f167f_updated_profile_user_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""updated profile user columns
Revision ID: 9c01926f167f
Revises: e36c5525f3b6
Create Date: 2024-07-19 06:56:07.545775
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '9c01926f167f'
down_revision: Union[str, None] = 'e36c5525f3b6'
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! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
96 changes: 96 additions & 0 deletions alembic/versions/e36c5525f3b6_updated_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""updated columns
Revision ID: e36c5525f3b6
Revises: ba7a518767c3
Create Date: 2024-07-19 06:49:15.962971
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'e36c5525f3b6'
down_revision: Union[str, None] = 'ba7a518767c3'
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.alter_column('organizations', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.alter_column('organizations', 'updated_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.add_column('products', sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True))
op.alter_column('products', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=False)
op.alter_column('profiles', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True)
op.alter_column('profiles', 'updated_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True)
op.create_unique_constraint(None, 'profiles', ['user_id'])
op.alter_column('users', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.alter_column('users', 'updated_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
existing_server_default=sa.text('now()'))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('users', 'updated_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.alter_column('users', 'created_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.drop_constraint(None, 'profiles', type_='unique')
op.alter_column('profiles', 'updated_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True)
op.alter_column('profiles', 'created_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True)
op.alter_column('products', 'created_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=False)
op.drop_column('products', 'updated_at')
op.alter_column('organizations', 'updated_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
op.alter_column('organizations', 'created_at',
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text('now()'))
# ### end Alembic commands ###
4 changes: 2 additions & 2 deletions api/v1/models/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Organization(Base):
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
name = Column(String(50), unique=True, nullable=False)
description = Column(Text, nullable=True)
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

users = relationship(
"User",
Expand Down
3 changes: 2 additions & 1 deletion api/v1/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ class Product(Base):
name = Column(String, nullable=False)
description = Column(Text, nullable=True)
price = Column(Numeric, nullable=False)
created_at = Column(DateTime, nullable=False, default=func.now())
created_at = Column(DateTime(timezone=True), nullable=False, default=func.now())
updated_at = Column(DateTime(timezone=True), default=func.now(), onupdate=func.now())
8 changes: 4 additions & 4 deletions api/v1/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Profile(Base):
__tablename__ = 'profiles'

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
user_id = Column(UUID(as_uuid=True), ForeignKey('users.id'), nullable=False)
user_id = Column(UUID(as_uuid=True), ForeignKey('users.id'), unique=True, nullable=False)
bio = Column(Text, nullable=True)
phone_number = Column(String(50), nullable=True)
avatar_url = Column(String(100), nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

#user = relationship("User", back_populates="profile")
user = relationship("User", back_populates="profile")
6 changes: 3 additions & 3 deletions api/v1/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class User(Base):
password = Column(String(255), nullable=False)
first_name = Column(String(50))
last_name = Column(String(50))
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

profile = relationship("Profile", uselist=False)
profile = relationship("Profile", uselist=False, back_populates="user")
organizations = relationship(
"Organization",
secondary=user_organization_association,
Expand Down
1 change: 1 addition & 0 deletions db_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
for user in users:
print(user.password)
print(profile_1.user_id)
print(profile_1.user)

0 comments on commit 4a9c42c

Please sign in to comment.