Skip to content

Commit 9acd45e

Browse files
committed
importer: rgl: Support combining teams
Add support for combining teams that have been linked after already having been imported. This occasionally happens by request. Signed-off-by: Sean Anderson <[email protected]>
1 parent 0f8a44b commit 9acd45e

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

test/create.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
from trends.importer.fetch import DemoFileFetcher, ETF2LFileFetcher, FileFetcher, RGLFileFetcher
1414
from trends.sql import db_connect, db_init, db_schema
1515

16+
# Pretend these two teams aren't linked so we can test combining them
17+
class RGLFirstFetcher(RGLFileFetcher):
18+
def get_team(self, teamid):
19+
ret = super().get_team(teamid)
20+
if teamid == 5574:
21+
ret['linkedTeams'] = []
22+
ret['fetched'] = 1
23+
if teamid == 4882:
24+
ret['linkedTeams'] = []
25+
ret['fetched'] = 1
26+
return ret
27+
28+
class RGLSecondFetcher(RGLFileFetcher):
29+
def get_matchids(self):
30+
yield 4664
31+
1632
def create_test_db(url):
1733
# We use separate connections for importing because we use temporary tables which will alias
1834
# other queries.
@@ -82,9 +98,10 @@ def create_test_db(url):
8298
trends.importer.etf2l.import_etf2l(c, fetcher)
8399

84100
with db_connect(url) as c:
85-
fetcher = RGLFileFetcher(dir=f"{os.path.dirname(__file__)}/rgl")
86-
trends.importer.rgl.import_rgl(c, fetcher)
87-
101+
dir = f"{os.path.dirname(__file__)}/rgl"
102+
trends.importer.rgl.import_rgl(c, RGLFirstFetcher(dir=dir))
103+
trends.importer.rgl.import_rgl(c, RGLSecondFetcher(dir=dir),
104+
filter=trends.importer.rgl.no_filter_matchids)
88105
with db_connect(url) as c:
89106
cur = c.cursor()
90107
cur.execute("ANALYZE;")

trends/importer/league.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ def import_compdiv(c, cd):
3737
(SELECT div_nameid FROM div_name WHERE division = %(division)s), %(tier)s
3838
) ON CONFLICT DO NOTHING;""", cd)
3939

40+
def combine_teams(c, league, teamids):
41+
"""Combine teams
42+
Combine the teams specified into one team. This may occasionally be necessary when RGL teams are
43+
retroactively linked.
44+
"""
45+
46+
new_teamid = min(teamids)
47+
teamids = tuple(teamid for teamid in teamids if teamid != new_teamid)
48+
def set_constraints(behavior):
49+
c.execute(
50+
f"""SET CONSTRAINTS
51+
team_player_league_teamid_fkey,
52+
team_player_league_teamid_compid_fkey,
53+
match_league_compid_teamid1_fkey,
54+
match_league_compid_teamid2_fkey
55+
{behavior};""");
56+
57+
set_constraints("DEFERRED")
58+
for table, col in (
59+
('team_comp_backing', 'teamid'),
60+
('team_player', 'teamid'),
61+
('match', 'teamid1'),
62+
('match', 'teamid2'),
63+
):
64+
c.execute(
65+
f"""UPDATE {table}
66+
SET {col} = %s
67+
WHERE league = %s AND {col} IN %s;""", (new_teamid, league, teamids))
68+
set_constraints("IMMEDIATE")
69+
70+
return new_teamid
71+
4072
def import_team(c, t):
4173
"""Import a team
4274
The competition and division should already be imported. t should be a dict with the following
@@ -69,13 +101,14 @@ def import_team(c, t):
69101
if teamids := t.get('rgl_teamids'):
70102
c.execute("SELECT teamid FROM team_comp WHERE rgl_teamid IN %s GROUP BY teamid",
71103
(teamids,))
72-
if c.rowcount not in (0, 1):
73-
logging.warning("Too many matching teams for linked RGL teamids %s", teamids)
74-
75-
if row := c.fetchone():
76-
t['teamid'] = row[0]
104+
if c.rowcount > 1:
105+
logging.info("Combining RGL teamids %s", teamids)
106+
t['teamid'] = combine_teams(c, t['league'], tuple(row[0] for row in c.fetchall()))
77107
else:
78-
teamid_col = min(teamids)
108+
if row := c.fetchone():
109+
t['teamid'] = row[0]
110+
else:
111+
teamid_col = min(teamids)
79112

80113
c.execute("INSERT INTO team_name (team) VALUES (%(name)s) ON CONFLICT DO NOTHING", t)
81114
c.execute(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BEGIN;
2+
ALTER TABLE team_player
3+
ALTER CONSTRAINT team_player_league_teamid_fkey DEFERRABLE,
4+
ALTER CONSTRAINT team_player_league_teamid_compid_fkey DEFERRABLE;
5+
ALTER TABLE match
6+
ALTER CONSTRAINT match_league_compid_teamid1_fkey DEFERRABLE,
7+
ALTER CONSTRAINT match_league_compid_teamid2_fkey DEFERRABLE;
8+
COMMIT;

trends/schema.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ CREATE TABLE IF NOT EXISTS team_player (
238238
rostered WITH &&,
239239
playerid WITH =
240240
) WHERE (compid ISNULL),
241-
FOREIGN KEY (league, teamid) REFERENCES league_team (league, teamid),
242-
FOREIGN KEY (league, teamid, compid) REFERENCES team_comp_backing (league, teamid, compid),
241+
FOREIGN KEY (league, teamid) REFERENCES league_team (league, teamid) DEFERRABLE,
242+
FOREIGN KEY (league, teamid, compid)
243+
REFERENCES team_comp_backing (league, teamid, compid) DEFERRABLE,
243244
CHECK (league_team_per_comp(league) = (compid NOTNULL))
244245
);
245246

@@ -295,8 +296,10 @@ CREATE TABLE IF NOT EXISTS match (
295296
FOREIGN KEY (league, round_compid, round_seq)
296297
REFERENCES comp_round (league, compid, round_seq),
297298
FOREIGN KEY (league, divid, round_seq) REFERENCES div_round (league, divid, round_seq),
298-
FOREIGN KEY (league, compid, teamid1) REFERENCES team_comp_backing (league, compid, teamid),
299-
FOREIGN KEY (league, compid, teamid2) REFERENCES team_comp_backing (league, compid, teamid),
299+
FOREIGN KEY (league, compid, teamid1)
300+
REFERENCES team_comp_backing (league, compid, teamid) DEFERRABLE,
301+
FOREIGN KEY (league, compid, teamid2)
302+
REFERENCES team_comp_backing (league, compid, teamid) DEFERRABLE,
300303
CHECK (teamid1 < teamid2),
301304
CHECK (league_div_optional(league) OR (divid NOTNULL)),
302305
CHECK (league_time_optional(league) OR forfeit OR (scheduled NOTNULL)),

0 commit comments

Comments
 (0)