Skip to content
Snippets Groups Projects
Commit 8b79d794 authored by Jiří Setnička's avatar Jiří Setnička
Browse files

DB: Zavedení podkol a podsoutěží

Bohužel bylo potřeba zrušit DB constrainy na unikátnost (year, category, seq).

Nakonec snadněji vychází, když hlavní kola/soutěže mají v parent_id NULL.

Issue #178
parent 957bee38
Branches
No related tags found
1 merge request!38Draft: Dělená kola
This commit is part of merge request !38. Comments created here will be created in the context of that merge request.
...@@ -90,6 +90,7 @@ CREATE TYPE score_mode AS ENUM ( ...@@ -90,6 +90,7 @@ CREATE TYPE score_mode AS ENUM (
CREATE TABLE rounds ( CREATE TABLE rounds (
round_id serial PRIMARY KEY, round_id serial PRIMARY KEY,
master_round_id int DEFAULT NULL REFERENCES rounds(round_id),
year int NOT NULL, -- ročník MO year int NOT NULL, -- ročník MO
category varchar(2) NOT NULL, -- "A", "Z5" apod. category varchar(2) NOT NULL, -- "A", "Z5" apod.
seq int NOT NULL, -- 1=domácí kolo atd. seq int NOT NULL, -- 1=domácí kolo atd.
...@@ -104,12 +105,12 @@ CREATE TABLE rounds ( ...@@ -104,12 +105,12 @@ CREATE TABLE rounds (
score_mode score_mode NOT NULL DEFAULT 'basic', -- mód výsledkovky score_mode score_mode NOT NULL DEFAULT 'basic', -- mód výsledkovky
score_winner_limit int DEFAULT NULL, -- bodový limit na označení za vítěze score_winner_limit int DEFAULT NULL, -- bodový limit na označení za vítěze
score_successful_limit int DEFAULT NULL, -- bodový limit na označení za úspěšného řešitele score_successful_limit int DEFAULT NULL, -- bodový limit na označení za úspěšného řešitele
UNIQUE (year, category, seq)
); );
-- Soutěže (instance kola v konkrétním místě) -- Soutěže (instance kola v konkrétním místě)
CREATE TABLE contests ( CREATE TABLE contests (
contest_id serial PRIMARY KEY, contest_id serial PRIMARY KEY,
master_contest_id int DEFAULT NULL REFERENCES contests(contest_id),
round_id int NOT NULL REFERENCES rounds(round_id), round_id int NOT NULL REFERENCES rounds(round_id),
place_id int NOT NULL REFERENCES places(place_id), place_id int NOT NULL REFERENCES places(place_id),
UNIQUE (round_id, place_id) UNIQUE (round_id, place_id)
......
SET ROLE 'mo_osmo';
ALTER TABLE rounds
ADD COLUMN master_round_id int DEFAULT NULL REFERENCES rounds(round_id);
ALTER TABLE contests
ADD COLUMN master_contest_id int DEFAULT NULL REFERENCES contests(contest_id);
ALTER TABLE rounds
DROP CONSTRAINT "rounds_year_category_seq_key";
UPDATE rounds SET master_round_id=round_id;
UPDATE contests SET master_contest_id=contest_id;
...@@ -186,11 +186,9 @@ round_score_mode_names = { ...@@ -186,11 +186,9 @@ round_score_mode_names = {
class Round(Base): class Round(Base):
__tablename__ = 'rounds' __tablename__ = 'rounds'
__table_args__ = (
UniqueConstraint('year', 'category', 'seq'),
)
round_id = Column(Integer, primary_key=True, server_default=text("nextval('rounds_round_id_seq'::regclass)")) round_id = Column(Integer, primary_key=True, server_default=text("nextval('rounds_round_id_seq'::regclass)"))
master_round_id = Column(Integer, ForeignKey('rounds.round_id'))
year = Column(Integer, nullable=False) year = Column(Integer, nullable=False)
category = Column(String(2), nullable=False) category = Column(String(2), nullable=False)
seq = Column(Integer, nullable=False) seq = Column(Integer, nullable=False)
...@@ -206,12 +204,17 @@ class Round(Base): ...@@ -206,12 +204,17 @@ class Round(Base):
score_winner_limit = Column(Integer) score_winner_limit = Column(Integer)
score_successful_limit = Column(Integer) score_successful_limit = Column(Integer)
master = relationship('Round', primaryjoin='Round.master_round_id == Round.round_id', remote_side='Round.round_id')
def round_code(self): def round_code(self):
return f"{self.year}-{self.category}-{self.seq}" return f"{self.year}-{self.category}-{self.seq}"
def has_tasks(self): def has_tasks(self):
return self.tasks_file return self.tasks_file
def is_subround(self) -> bool:
return self.master_round_id != self.round_id
def long_state(self) -> str: def long_state(self) -> str:
details = "" details = ""
if self.state == RoundState.preparing: if self.state == RoundState.preparing:
...@@ -270,17 +273,19 @@ class User(Base): ...@@ -270,17 +273,19 @@ class User(Base):
class Contest(Base): class Contest(Base):
__tablename__ = 'contests' __tablename__ = 'contests'
__table_args__ = (
UniqueConstraint('round_id', 'place_id'),
)
contest_id = Column(Integer, primary_key=True, server_default=text("nextval('contests_contest_id_seq'::regclass)")) contest_id = Column(Integer, primary_key=True, server_default=text("nextval('contests_contest_id_seq'::regclass)"))
master_contest_id = Column(Integer, ForeignKey('contests.contest_id'))
round_id = Column(Integer, ForeignKey('rounds.round_id'), nullable=False) round_id = Column(Integer, ForeignKey('rounds.round_id'), nullable=False)
place_id = Column(Integer, ForeignKey('places.place_id'), nullable=False) place_id = Column(Integer, ForeignKey('places.place_id'), nullable=False)
master = relationship('Contest', primaryjoin='Contest.master_contest_id == Contest.contest_id', remote_side='Contest.contest_id')
place = relationship('Place') place = relationship('Place')
round = relationship('Round') round = relationship('Round')
def is_subcontest(self) -> bool:
return self.master_contest_id != self.contest_id
class LogType(MOEnum): class LogType(MOEnum):
general = auto() general = auto()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment