Skip to content

Commit

Permalink
Fix created columns (#726)
Browse files Browse the repository at this point in the history
The default was wrong, the `"now()"` was executed when building the
database, resulting in a default of a constant value instead of the
function `now()`
  • Loading branch information
evroon authored May 14, 2024
1 parent 1cb0c42 commit 2e9e434
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
33 changes: 33 additions & 0 deletions backend/alembic/versions/1961954c0320_fix_created_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""fix created columns
Revision ID: 1961954c0320
Revises: 19ddf67a4eeb
Create Date: 2024-05-14 18:52:54.234776
"""

from alembic import op

# revision identifiers, used by Alembic.
revision: str | None = "1961954c0320"
down_revision: str | None = "19ddf67a4eeb"
branch_labels: str | None = None
depends_on: str | None = None


def upgrade() -> None:
op.execute("ALTER TABLE clubs ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE tournaments ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE stages ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE stage_items ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE rounds ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE matches ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE teams ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE players ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE users ALTER COLUMN created SET DEFAULT NOW();")
op.execute("ALTER TABLE courts ALTER COLUMN created SET DEFAULT NOW();")


def downgrade() -> None:
# No rollback because it will introduce bugs.
pass
22 changes: 11 additions & 11 deletions backend/bracket/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, ForeignKey, Integer, String, Table
from sqlalchemy import Column, ForeignKey, Integer, String, Table, func
from sqlalchemy.orm import declarative_base # type: ignore[attr-defined]
from sqlalchemy.sql.sqltypes import BigInteger, Boolean, DateTime, Enum, Float, Text

Expand All @@ -11,15 +11,15 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True, autoincrement=True),
Column("name", String, nullable=False, index=True),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
)

tournaments = Table(
"tournaments",
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", String, nullable=False, index=True),
Column("created", DateTimeTZ, nullable=False, server_default="now()"),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("start_time", DateTimeTZ, nullable=False),
Column("club_id", BigInteger, ForeignKey("clubs.id"), index=True, nullable=False),
Column("dashboard_public", Boolean, nullable=False),
Expand All @@ -36,7 +36,7 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", String, nullable=False, index=True),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("tournament_id", BigInteger, ForeignKey("tournaments.id"), index=True, nullable=False),
Column("is_active", Boolean, nullable=False, server_default="false"),
)
Expand All @@ -46,7 +46,7 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", Text, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default="now()"),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("stage_id", BigInteger, ForeignKey("stages.id"), index=True, nullable=False),
Column("team_count", Integer, nullable=False),
Column(
Expand Down Expand Up @@ -84,7 +84,7 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", Text, nullable=False),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("is_draft", Boolean, nullable=False),
Column("is_active", Boolean, nullable=False, server_default="false"),
Column("stage_item_id", BigInteger, ForeignKey("stage_items.id"), nullable=False),
Expand All @@ -95,7 +95,7 @@
"matches",
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("start_time", DateTimeTZ, nullable=True),
Column("duration_minutes", Integer, nullable=True),
Column("margin_minutes", Integer, nullable=True),
Expand Down Expand Up @@ -125,7 +125,7 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", String, nullable=False, index=True),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("tournament_id", BigInteger, ForeignKey("tournaments.id"), index=True, nullable=False),
Column("active", Boolean, nullable=False, index=True, server_default="t"),
Column("elo_score", Float, nullable=False, server_default="0"),
Expand All @@ -141,7 +141,7 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", String, nullable=False, index=True),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("tournament_id", BigInteger, ForeignKey("tournaments.id"), index=True, nullable=False),
Column("elo_score", Float, nullable=False),
Column("swiss_score", Float, nullable=False),
Expand All @@ -158,7 +158,7 @@
Column("email", String, nullable=False, index=True, unique=True),
Column("name", String, nullable=False),
Column("password_hash", String, nullable=False),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column(
"account_type",
Enum(
Expand Down Expand Up @@ -201,6 +201,6 @@
metadata,
Column("id", BigInteger, primary_key=True, index=True),
Column("name", Text, nullable=False),
Column("created", DateTimeTZ, nullable=False),
Column("created", DateTimeTZ, nullable=False, server_default=func.now()),
Column("tournament_id", BigInteger, ForeignKey("tournaments.id"), nullable=False, index=True),
)

0 comments on commit 2e9e434

Please sign in to comment.