Skip to content

Commit

Permalink
Rename users table
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 27, 2023
1 parent 4062c8e commit e907396
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
30 changes: 30 additions & 0 deletions schemes/migrations/versions/4015380a8cf6_rename_user_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Rename user table
Revision ID: 4015380a8cf6
Revises: bfc3cbc1bb13
Create Date: 2023-10-27 11:53:59.542995
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "4015380a8cf6"
down_revision: Union[str, None] = "bfc3cbc1bb13"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.rename_table("users", "user")
op.alter_column("user", "id", new_column_name="user_id")
if op.get_context().dialect.name == "postgresql":
op.execute("ALTER SEQUENCE users_id_seq RENAME TO user_user_id_seq")


def downgrade() -> None:
if op.get_context().dialect.name == "postgresql":
op.execute("ALTER SEQUENCE user_user_id_seq RENAME TO users_id_seq")
op.alter_column("user", "user_id", new_column_name="id")
op.rename_table("user", "users")
12 changes: 6 additions & 6 deletions schemes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def get_all(self) -> List[User]:

def add_tables(metadata: MetaData) -> None:
Table(
"users",
"user",
metadata,
Column("id", Integer, primary_key=True),
Column("user_id", Integer, primary_key=True),
Column("email", String(length=256), nullable=False, unique=True),
)

Expand All @@ -44,21 +44,21 @@ def __init__(self, engine: Engine):
def add(self, *users: User) -> None:
with self._engine.begin() as connection:
for user in users:
connection.execute(text("INSERT INTO users (email) VALUES (:email)"), {"email": user.email})
connection.execute(text("INSERT INTO user (email) VALUES (:email)"), {"email": user.email})

def clear(self) -> None:
with self._engine.begin() as connection:
connection.execute(text("DELETE FROM users"))
connection.execute(text("DELETE FROM user"))

def get_by_email(self, email: str) -> User | None:
with self._engine.connect() as connection:
result = connection.execute(text("SELECT email FROM users WHERE email = :email"), {"email": email})
result = connection.execute(text("SELECT email FROM user WHERE email = :email"), {"email": email})
row = result.one_or_none()
return User(row.email) if row else None

def get_all(self) -> List[User]:
with self._engine.connect() as connection:
result = connection.execute(text("SELECT email FROM users"))
result = connection.execute(text("SELECT email FROM user"))
return [User(row.email) for row in result]


Expand Down

0 comments on commit e907396

Please sign in to comment.