| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise first startup and upgrades while preserving existing game data.""" |
| 3 | from pathlib import Path |
| 4 | import shutil |
| 5 | import sqlite3 |
| 6 | import subprocess |
| 7 | import tempfile |
| 8 | |
| 9 | WEB = Path(__file__).resolve().parents[1] |
| 10 | ARTIFACTS = WEB / 'tests/artifacts' |
| 11 | ARTIFACTS.mkdir(exist_ok=True) |
| 12 | with tempfile.TemporaryDirectory(prefix='startup-', dir=ARTIFACTS) as tmp: |
| 13 | root = Path(tmp) |
| 14 | shutil.copytree(WEB / 'lib', root / 'lib', ignore=shutil.ignore_patterns('tablebase')) |
| 15 | shutil.copytree(WEB / 'assets/avatars', root / 'assets/avatars') |
| 16 | (root / 'data').mkdir() |
| 17 | def startup(): |
| 18 | result = subprocess.run(['php', '-r', 'require $argv[1]; db();', str(root / 'lib/bootstrap.php')], capture_output=True, text=True, check=True) |
| 19 | assert not result.stderr, result.stderr |
| 20 | log = root / 'data/php-errors.log' |
| 21 | assert not log.exists() or not log.read_text(), 'Startup emitted PHP errors' |
| 22 | startup() |
| 23 | con = sqlite3.connect(root / 'data/microchess.sqlite') |
| 24 | con.executescript(""" |
| 25 | INSERT INTO users VALUES('a','Alice'),('b','Bob'); |
| 26 | INSERT INTO games(a,b,status,created) VALUES('a','b','active',123); |
| 27 | DROP TRIGGER games_activity_insert; |
| 28 | DROP TRIGGER games_activity_update; |
| 29 | ALTER TABLE games DROP COLUMN activity_at; |
| 30 | """) |
| 31 | startup() |
| 32 | row = con.execute('SELECT a,b,status,created,activity_at FROM games').fetchone() |
| 33 | assert row[:4] == ('a','b','active',123) and row[4] > 0, row |
| 34 | con.executescript(""" |
| 35 | DROP TRIGGER games_activity_insert; |
| 36 | DROP TRIGGER games_activity_update; |
| 37 | CREATE TRIGGER games_activity_insert AFTER INSERT ON games BEGIN UPDATE games SET activity_at=unixepoch() WHERE id=NEW.id; END; |
| 38 | CREATE TRIGGER games_activity_update AFTER UPDATE OF version ON games BEGIN UPDATE games SET activity_at=unixepoch() WHERE id=NEW.id; END; |
| 39 | UPDATE games SET activity_at=123; |
| 40 | """) |
| 41 | startup() |
| 42 | assert con.execute('SELECT activity_at FROM games').fetchone()[0] == 123 |
| 43 | triggers = con.execute("SELECT sql FROM sqlite_master WHERE type='trigger' AND name IN ('games_activity_insert','games_activity_update')").fetchall() |
| 44 | assert len(triggers) == 2 and all('unixepoch(' not in sql.lower() for sql, in triggers) |
| 45 | con.execute('UPDATE games SET version=version+1') |
| 46 | assert con.execute('SELECT activity_at FROM games').fetchone()[0] > 123 |
| 47 | con.execute("INSERT INTO games(a,b,status,created) VALUES('b','a','active',456)") |
| 48 | assert con.execute('SELECT activity_at FROM games WHERE id=2').fetchone()[0] > 0 |
| 49 | con.commit() |
| 50 | startup() |
| 51 | assert con.execute('SELECT count(*) FROM games').fetchone()[0] == 2 |
| 52 | con.close() |
| 53 | print('Startup: fresh database, legacy migration, trigger upgrade and repeated startup passed.') |
projects-timfalken-com microchess