Skip to content

Commit 4a0d74d

Browse files
committed
Add tests for table migration
1 parent 2b986d1 commit 4a0d74d

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

plugins/tell.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,31 @@ def mark_read(self, now=None):
7878

7979
@hook.on_start(priority=Priority.HIGHEST)
8080
def migrate_tables(db):
81-
table = Table(
81+
inspector = sa.inspect(db.bind)
82+
if not inspector.has_table("tells"):
83+
return
84+
85+
table = sa.Table(
8286
"tells",
8387
database.metadata,
84-
Column("connection", String),
85-
Column("sender", String),
86-
Column("target", String),
87-
Column("message", String),
88-
Column("is_read", Boolean),
89-
Column("time_sent", DateTime),
90-
Column("time_read", DateTime),
88+
autoload_with=db.bind,
9189
)
9290

93-
if not table.exists(db.bind):
94-
return
95-
96-
if TellMessage.__table__.exists(db.bin):
91+
if (
92+
inspector.has_table(TellMessage.__tablename__)
93+
and db.query(TellMessage).count() > 0
94+
):
9795
raise Exception(
98-
f"Can't migrate table {table.name} to {TellMessage.__table__.name}, destination already exists"
96+
f"Can't migrate table {table.name} to {TellMessage.__tablename__}, destination already exists"
9997
)
10098

101-
data = db.execute(table.select())
99+
data = [dict(row) for row in db.execute(table.select())]
100+
for item in data:
101+
item["conn"] = item.pop("connection")
102+
102103
db.bulk_insert_mappings(TellMessage, data, return_defaults=True)
103104
db.commit()
105+
104106
table.drop(db.bind)
105107

106108

tests/plugin_tests/test_tell.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import datetime
22
from unittest.mock import MagicMock, call, patch
33

4+
import sqlalchemy as sa
45
from irclib.parser import Prefix
56

7+
from cloudbot.util import database
68
from plugins import tell
79
from tests.util.mock_conn import MockConn
810

@@ -18,6 +20,65 @@ def init_tables(mock_db):
1820
tell.load_ignores(session)
1921

2022

23+
def test_migrate_db(mock_db, freeze_time):
24+
init_tables(mock_db)
25+
session = mock_db.session()
26+
27+
tbl = sa.Table(
28+
"tells",
29+
database.metadata,
30+
sa.Column("connection", sa.String),
31+
sa.Column("sender", sa.String),
32+
sa.Column("target", sa.String),
33+
sa.Column("message", sa.String),
34+
sa.Column("is_read", sa.Boolean),
35+
sa.Column("time_sent", sa.DateTime),
36+
sa.Column("time_read", sa.DateTime),
37+
)
38+
39+
tbl.create(mock_db.engine)
40+
mock_db.add_row(
41+
tbl,
42+
connection="conn",
43+
sender="foo",
44+
target="bar",
45+
message="foobar",
46+
is_read=False,
47+
time_sent=datetime.datetime.now(),
48+
time_read=None,
49+
)
50+
51+
database.metadata.clear()
52+
53+
assert mock_db.get_data(tbl) == [
54+
(
55+
"conn",
56+
"foo",
57+
"bar",
58+
"foobar",
59+
False,
60+
datetime.datetime(2019, 8, 22, 13, 14, 36),
61+
None,
62+
)
63+
]
64+
65+
tell.migrate_tables(session)
66+
67+
assert not sa.inspect(mock_db.engine).has_table(tbl.name)
68+
assert mock_db.get_data(tell.TellMessage.__table__) == [
69+
(
70+
1,
71+
"conn",
72+
"foo",
73+
"bar",
74+
"foobar",
75+
False,
76+
datetime.datetime(2019, 8, 22, 13, 14, 36),
77+
None,
78+
)
79+
]
80+
81+
2182
def test_tellcmd(mock_db):
2283
init_tables(mock_db)
2384
session = mock_db.session()

0 commit comments

Comments
 (0)