Skip to content

Commit

Permalink
Migrate to supporting database v133 with table removals (#983)
Browse files Browse the repository at this point in the history
* Migrate to supporting database v133 with table removals

* Remove unused import

* Remove obsolete tests
  • Loading branch information
Sheikah45 committed Nov 12, 2023
1 parent b9d5755 commit 62e52aa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- cron: '0 0 * * *'

env:
FAF_DB_VERSION: v122
FAF_DB_VERSION: v133
FLYWAY_VERSION: 7.5.4

jobs:
Expand Down
19 changes: 0 additions & 19 deletions server/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,6 @@
Column("validity", Integer, nullable=False),
)

global_rating = Table(
"global_rating", metadata,
Column("id", Integer, ForeignKey("login.id"), primary_key=True),
Column("mean", Float),
Column("deviation", Float),
Column("numGames", Integer, nullable=False),
Column("is_active", Boolean, nullable=False),
)

group_permission = Table(
"group_permission", metadata,
Column("id", Integer, primary_key=True),
Expand Down Expand Up @@ -188,16 +179,6 @@
Column("last_login", TIMESTAMP)
)

ladder1v1_rating = Table(
"ladder1v1_rating", metadata,
Column("id", Integer, ForeignKey("login.id"), primary_key=True),
Column("mean", Float),
Column("deviation", Float),
Column("numGames", Integer, nullable=False),
Column("winGames", Integer, nullable=False),
Column("is_active", Boolean, nullable=False)
)

leaderboard = Table(
"leaderboard", metadata,
Column("id", Integer, primary_key=True),
Expand Down
58 changes: 0 additions & 58 deletions server/player_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from server.db import FAFDatabase
from server.decorators import with_logger
from server.players import Player, PlayerState
from server.rating import RatingType
from server.timing import at_interval

from .core import Service
Expand All @@ -23,10 +22,8 @@
avatars_list,
clan,
clan_membership,
global_rating,
group_permission,
group_permission_assignment,
ladder1v1_rating,
leaderboard,
leaderboard_rating,
login,
Expand Down Expand Up @@ -149,61 +146,6 @@ async def _fetch_player_ratings(self, player, conn):
player.ratings[rating_type] = rating
player.game_count[rating_type] = total_games

types_not_found = [
rating_type for rating_type in (
RatingType.GLOBAL, RatingType.LADDER_1V1
)
if rating_type not in retrieved_ratings
]
await self._fetch_player_legacy_rating(player, types_not_found, conn)

async def _fetch_player_legacy_rating(self, player, rating_types, conn):
if not rating_types:
return

sql = select(
global_rating.c.mean,
global_rating.c.deviation,
global_rating.c.numGames,
ladder1v1_rating.c.mean,
ladder1v1_rating.c.deviation,
ladder1v1_rating.c.numGames,
).select_from(
login.outerjoin(ladder1v1_rating).outerjoin(global_rating)
).where(
login.c.id == player.id
)
result = await conn.execute(sql)
row = result.fetchone()

if row is None:
self._logger.info("Found no ratings for Player with id %i", player.id)
return

row = row._mapping

table_map = {
RatingType.GLOBAL: global_rating,
RatingType.LADDER_1V1: ladder1v1_rating,
}
for rating_type in rating_types:
if rating_type not in table_map:
raise ValueError(f"Unknown rating type {rating_type}.")

table = table_map[rating_type]
if row[table.c.mean] is None:
self._logger.info(
"Found no %s ratings for Player with id %i",
rating_type, player.id
)
continue

player.ratings[rating_type] = (
row[table.c.mean],
row[table.c.deviation]
)
player.game_count[rating_type] = row[table.c.numGames]

def remove_player(self, player: Player):
if player.id in self._players:
del self._players[player.id]
Expand Down
102 changes: 22 additions & 80 deletions tests/data/test-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ DELETE FROM moderation_report;
DELETE FROM teamkills;
DELETE FROM unique_id_users;
DELETE FROM uniqueid;
DELETE FROM global_rating;
DELETE FROM ladder1v1_rating;
DELETE FROM uniqueid_exempt;
DELETE FROM friends_and_foes;
DELETE FROM ladder_map;
DELETE FROM tutorial;
DELETE FROM map_version_review;
DELETE FROM map_version_reviews_summary;
Expand All @@ -25,7 +22,6 @@ DELETE FROM mod_version_reviews_summary;
DELETE FROM mod_version;
DELETE FROM `mod`;
DELETE FROM mod_stats;
DELETE FROM oauth_clients;
DELETE FROM updates_faf;
DELETE FROM updates_faf_files;
DELETE FROM avatars;
Expand All @@ -38,8 +34,6 @@ DELETE FROM game_review;
DELETE FROM game_reviews_summary;
DELETE FROM game_stats;
DELETE FROM game_featuredMods;
DELETE FROM ladder_division_score;
DELETE FROM ladder_division;
DELETE FROM name_history;
DELETE FROM user_group_assignment;
DELETE FROM login;
Expand Down Expand Up @@ -136,31 +130,6 @@ insert into leaderboard_rating (login_id, mean, deviation, total_games, leaderbo
(106, 900, 75, 20, 3)
;

-- legacy table for global rating
insert into global_rating (id, mean, deviation, numGames, is_active) values
(1, 2000, 125, 5, 1),
(2, 1500, 75, 2, 1),
(3, 1650, 62.52, 2, 1),
(50, 1201, 250, 42, 1),
(51, 1201, 250, 42, 1),
(52, 1201, 250, 42, 1),
(100, 1501, 500, 0, 1),
(101, 1501, 500, 0, 1),
(102, 1501, 500, 0, 1)
;

-- legacy ladder rating
insert into ladder1v1_rating (id, mean, deviation, numGames, is_active) values
(1, 2000, 125, 5, 1),
(2, 1500, 75, 2, 1),
(3, 1650, 62.52, 2, 1),
(50, 1301, 400, 12, 1),
(51, 1301, 400, 12, 1),
(100, 1501, 500, 0, 1),
(101, 1501, 500, 0, 1),
(102, 1501, 500, 0, 1)
;

-- UniqueID_exempt
insert into uniqueid_exempt (user_id, reason) values (1, 'Because test');

Expand All @@ -175,23 +144,23 @@ insert into unique_id_users (user_id, uniqueid_hash) values (2, 'another_id');
insert into unique_id_users (user_id, uniqueid_hash) values (3, 'some_id');

-- Sample maps
insert into map (id, display_name, map_type, battle_type, author) values
(1, 'SCMP_001', 'FFA', 'skirmish', 1),
(2, 'SCMP_002', 'FFA', 'skirmish', 1),
(3, 'SCMP_003', 'FFA', 'skirmish', 1),
(4, 'SCMP_004', 'FFA', 'skirmish', 1),
(5, 'SCMP_005', 'FFA', 'skirmish', 1),
(6, 'SCMP_006', 'FFA', 'skirmish', 2),
(7, 'SCMP_007', 'FFA', 'skirmish', 2),
(8, 'SCMP_008', 'FFA', 'skirmish', 2),
(9, 'SCMP_009', 'FFA', 'skirmish', 2),
(10, 'SCMP_010', 'FFA', 'skirmish', 3),
(11, 'SCMP_011', 'FFA', 'skirmish', 3),
(12, 'SCMP_012', 'FFA', 'skirmish', 3),
(13, 'SCMP_013', 'FFA', 'skirmish', 3),
(14, 'SCMP_014', 'FFA', 'skirmish', 3),
(15, 'SCMP_015', 'FFA', 'skirmish', 3),
(16, 'neroxis_map_generator_sneaky_map', 'FFA', 'skirmish', 1);
insert into map (id, display_name, map_type, battle_type, author, license) values
(1, 'SCMP_001', 'FFA', 'skirmish', 1, 1),
(2, 'SCMP_002', 'FFA', 'skirmish', 1, 1),
(3, 'SCMP_003', 'FFA', 'skirmish', 1, 1),
(4, 'SCMP_004', 'FFA', 'skirmish', 1, 1),
(5, 'SCMP_005', 'FFA', 'skirmish', 1, 1),
(6, 'SCMP_006', 'FFA', 'skirmish', 2, 1),
(7, 'SCMP_007', 'FFA', 'skirmish', 2, 1),
(8, 'SCMP_008', 'FFA', 'skirmish', 2, 1),
(9, 'SCMP_009', 'FFA', 'skirmish', 2, 1),
(10, 'SCMP_010', 'FFA', 'skirmish', 3, 1),
(11, 'SCMP_011', 'FFA', 'skirmish', 3, 1),
(12, 'SCMP_012', 'FFA', 'skirmish', 3, 1),
(13, 'SCMP_013', 'FFA', 'skirmish', 3, 1),
(14, 'SCMP_014', 'FFA', 'skirmish', 3, 1),
(15, 'SCMP_015', 'FFA', 'skirmish', 3, 1),
(16, 'neroxis_map_generator_sneaky_map', 'FFA', 'skirmish', 1, 1);

insert into map_version (id, description, max_players, width, height, version, filename, hidden, ranked, map_id) values
(1, 'SCMP 001', 8, 1024, 1024, 1, 'maps/scmp_001.zip', 0, 1, 1),
Expand All @@ -213,10 +182,6 @@ insert into map_version (id, description, max_players, width, height, version, f
(17, 'SCMP 015', 8, 512, 512, 3, 'maps/scmp_015.v0003.zip', 0, 1, 15),
(18, 'Sneaky_Map', 8, 512, 512, 1, 'maps/neroxis_map_generator_sneaky_map.zip', 0, 0, 16);

insert into ladder_map (id, idmap) values
(1,1),
(2,2);

INSERT INTO `coop_map` (`type`, `name`, `description`, `version`, `filename`) VALUES
(0, 'FA Campaign map', 'A map from the FA campaign', 2, 'maps/scmp_coop_123.v0002.zip'),
(1, 'Aeon Campaign map', 'A map from the Aeon campaign', 0, 'maps/scmp_coop_124.v0000.zip'),
Expand Down Expand Up @@ -351,11 +316,11 @@ insert into friends_and_foes (user_id, subject_id, `status`) values
(2, 1, 'FRIEND'),
(10, 1, 'FRIEND');

insert into `mod` (id, display_name, author) VALUES
(1, 'test-mod', 'baz'),
(2, 'test-mod2', 'baz'),
(3, 'test-mod3', 'baz'),
(100, 'Mod without icon', 'foo');
insert into `mod` (id, display_name, author, license) VALUES
(1, 'test-mod', 'baz', 1),
(2, 'test-mod2', 'baz', 1),
(3, 'test-mod3', 'baz', 1),
(100, 'Mod without icon', 'foo', 1);

insert into mod_version (id, mod_id, uid, version, description, type, filename, icon) VALUES
(1, 1, 'foo', 1, '', 'UI', 'foobar.zip', 'foobar.png'),
Expand Down Expand Up @@ -411,12 +376,6 @@ insert into clan_membership (clan_id, player_id) values
(3, 1),
(1, 50);

-- sample oauth_client for Postman
insert into oauth_clients (id, name, client_secret, redirect_uris, default_redirect_uri, default_scope) VALUES
('3bc8282c-7730-11e5-8bcf-feff819cdc9f ', 'Downlord''s FAF Client', '{noop}6035bd78-7730-11e5-8bcf-feff819cdc9f', '', '', 'read_events read_achievements upload_map'),
('faf-website', 'faf-website', '{noop}banana', 'http://localhost:8020', 'http://localhost:8020', 'public_profile write_account_data create_user'),
('postman', 'postman', '{noop}postman', 'http://localhost https://www.getpostman.com/oauth2/callback', 'https://www.getpostman.com/oauth2/callback', 'read_events read_achievements upload_map upload_mod write_account_data');

insert into updates_faf (id, filename, path) values
(1, 'ForgedAlliance.exe', 'bin'),
(11, 'effects.nx2', 'gamedata'),
Expand Down Expand Up @@ -447,21 +406,4 @@ insert into mod_version_review (id, text, user_id, score, mod_version_id) VALUES
(2, 'Like it', 2, 4, 1),
(3, 'Funny', 3, 4, 1);

INSERT INTO ladder_division VALUES
(1, 'League 1 - Division A', 1, 10.0),
(2, 'League 1 - Division B', 1, 30.0),
(3, 'League 1 - Division C', 1, 50.0),
(4, 'League 2 - Division D', 2, 20.0),
(5, 'League 2 - Division E', 2, 60.0),
(6, 'League 2 - Division F', 2, 100.0),
(7, 'League 3 - Division D', 3, 100.0),
(8, 'League 3 - Division E', 3, 200.0),
(9, 'League 3 - Division F', 3, 9999.0);

INSERT INTO ladder_division_score (season, user_id, league, score, games) VALUES
(1, 1, 1, 9.5, 4),
(1, 2, 1, 49.5, 70),
(1, 3, 2, 0.0, 39),
(1, 4, 3, 10.0, 121);

INSERT INTO email_domain_blacklist VALUES ('spam.org');
12 changes: 0 additions & 12 deletions tests/unit_tests/test_player_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@ async def test_fetch_player_data(player_factory, player_service):
assert player.avatar == {"url": "https://content.faforever.com/faf/avatars/UEF.png", "tooltip": "UEF"}


async def test_fetch_player_data_legacy_rating(player_factory, player_service):
# Player 51 should only have legacy rating entries,
# but no `leaderboard_rating` entries.
player = player_factory(player_id=51)

await player_service.fetch_player_data(player)
assert player.ratings[RatingType.GLOBAL] == (1201, 250)
assert player.ratings[RatingType.LADDER_1V1] == (1301, 400)


async def test_fetch_ratings_nonexistent(player_factory, player_service):
player = player_factory(player_id=-1)
player_service._logger = mock.Mock()

async with player_service._db.acquire() as conn:
await player_service._fetch_player_ratings(player, conn)

player_service._logger.info.assert_called_once()
assert player.ratings[RatingType.GLOBAL] == (1500, 500)


Expand All @@ -44,7 +33,6 @@ async def test_fetch_ratings_partially_nonexistent(player_factory, player_servic
async with player_service._db.acquire() as conn:
await player_service._fetch_player_ratings(player, conn)

player_service._logger.info.assert_called_once()
assert player.ratings[RatingType.LADDER_1V1] == (1500, 500)


Expand Down

0 comments on commit 62e52aa

Please sign in to comment.