|
|
|
@@ -5,6 +5,7 @@ import hashlib
|
|
|
|
|
import json
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import sqlite3
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from paper_atlas.db.backend import database_arguments, open_read_connection, require_sqlite_writable
|
|
|
|
|
from paper_atlas.db.events import has_event_key, pinned_www_fall
|
|
|
|
@@ -21,7 +22,39 @@ def values(rows):
|
|
|
|
|
return [[r[i] for i in range(len(r))] for r in rows]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def inspect(c):
|
|
|
|
|
def exact_counts(c, *, page_size=5000, progress=None):
|
|
|
|
|
"""Count every row in the same snapshot through bounded primary-key pages."""
|
|
|
|
|
if not isinstance(page_size, int) or page_size < 1:
|
|
|
|
|
raise ValueError('Positive count page size required')
|
|
|
|
|
result = {}
|
|
|
|
|
for table in TABLES:
|
|
|
|
|
if table == 'schema_migrations':
|
|
|
|
|
continue
|
|
|
|
|
if getattr(c, 'postgres', False):
|
|
|
|
|
columns = [r[0] for r in c.execute("SELECT a.attname FROM pg_constraint pc JOIN pg_attribute a ON a.attrelid=pc.conrelid AND a.attnum=ANY(pc.conkey) WHERE pc.conrelid=?::regclass AND pc.contype='p' ORDER BY array_position(pc.conkey,a.attnum)", (table,))]
|
|
|
|
|
else:
|
|
|
|
|
columns = [r[1] for r in sorted(c.execute('PRAGMA table_info('+quote(table)+')'), key=lambda r:r[5]) if r[5]]
|
|
|
|
|
if not columns:
|
|
|
|
|
raise ValueError('Primary key required for exact count: '+table)
|
|
|
|
|
fields = ','.join(quote(column) for column in columns)
|
|
|
|
|
last = None; count = 0; pages = 0
|
|
|
|
|
if progress:
|
|
|
|
|
progress({'table':table, 'rows_counted':0, 'complete':False})
|
|
|
|
|
while True:
|
|
|
|
|
where = (' WHERE ('+fields+') > ('+','.join('?' for _ in columns)+')') if last is not None else ''
|
|
|
|
|
batch = values(c.execute('SELECT '+fields+' FROM '+quote(table)+where+' ORDER BY '+fields+' LIMIT ?', (*last, page_size) if last is not None else (page_size,)).fetchall())
|
|
|
|
|
count += len(batch); pages += 1
|
|
|
|
|
complete = len(batch) < page_size
|
|
|
|
|
if progress and (complete or pages % 20 == 0):
|
|
|
|
|
progress({'table':table, 'rows_counted':count, 'complete':complete})
|
|
|
|
|
if complete:
|
|
|
|
|
break
|
|
|
|
|
last = batch[-1]
|
|
|
|
|
result[table] = count
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def inspect(c, *, progress=None):
|
|
|
|
|
enabled = has_event_key(c)
|
|
|
|
|
if getattr(c, 'postgres', False):
|
|
|
|
|
columns = [r[0] for r in c.execute("SELECT column_name FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='editions' ORDER BY ordinal_position")]
|
|
|
|
@@ -38,7 +71,7 @@ def inspect(c):
|
|
|
|
|
raise ValueError('Unexpected edition schema; require a reviewed migration')
|
|
|
|
|
rows = values(c.execute('SELECT id,venue_id,year,track FROM editions ORDER BY id'))
|
|
|
|
|
keys = values(c.execute('SELECT id,event_key FROM editions ORDER BY id')) if enabled else [[r[0], ''] for r in rows]
|
|
|
|
|
counts = {table:c.execute('SELECT count(*) FROM '+table).fetchone()[0] for table in TABLES if table != 'schema_migrations'}
|
|
|
|
|
counts = exact_counts(c, progress=progress)
|
|
|
|
|
targets = values(c.execute("SELECT e.id FROM editions e JOIN venues v ON v.id=e.venue_id WHERE v.acronym='WWW' AND e.year=1995 AND e.track='main' ORDER BY e.id"))
|
|
|
|
|
assignments = []
|
|
|
|
|
for (eid,) in targets:
|
|
|
|
@@ -77,18 +110,18 @@ def database_identity(c, db=None):
|
|
|
|
|
return {'engine':'sqlite', 'path':str(Path(db).resolve())}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plan(db=None, *, pg_service=None, pg_schema=None):
|
|
|
|
|
def plan(db=None, *, pg_service=None, pg_schema=None, progress=None):
|
|
|
|
|
with closing(open_read_connection(db, pg_service=pg_service, pg_schema=pg_schema)) as c:
|
|
|
|
|
return {'format':1, 'database':database_identity(c, db), 'before':inspect(c)}
|
|
|
|
|
return {'format':1, 'database':database_identity(c, db), 'before':inspect(c, progress=progress)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_plan(c, proposal):
|
|
|
|
|
def apply_plan(c, proposal, *, progress=None):
|
|
|
|
|
before = proposal['before']
|
|
|
|
|
if proposal.get('format') != 1:
|
|
|
|
|
raise ValueError('Unsupported event identity plan')
|
|
|
|
|
if getattr(c, 'postgres', False):
|
|
|
|
|
c.execute('LOCK TABLE editions IN ACCESS EXCLUSIVE MODE')
|
|
|
|
|
current = inspect(c)
|
|
|
|
|
current = inspect(c, progress=progress)
|
|
|
|
|
signature = digest(proposal)
|
|
|
|
|
marker = MARKER + signature
|
|
|
|
|
if c.execute('SELECT 1 FROM schema_migrations WHERE name=?', (marker,)).fetchone():
|
|
|
|
@@ -124,7 +157,7 @@ def apply_plan(c, proposal):
|
|
|
|
|
if result.rowcount != 1:
|
|
|
|
|
raise ValueError('Edition assignment changed during upgrade')
|
|
|
|
|
c.execute('INSERT INTO schema_migrations(name) VALUES(?)', (marker,))
|
|
|
|
|
after = inspect(c)
|
|
|
|
|
after = inspect(c, progress=progress)
|
|
|
|
|
expected_keys = dict(before['event_keys'])
|
|
|
|
|
expected_keys.update({a['edition_id']:a['event_key'] for a in before['assignments']})
|
|
|
|
|
if (after['editions_sha256'] != before['editions_sha256'] or after['protected'] != before['protected']
|
|
|
|
@@ -135,14 +168,14 @@ def apply_plan(c, proposal):
|
|
|
|
|
return {'status':'applied', 'updated_editions':len(before['assignments']), 'after':after}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade(proposal, db=None, *, pg_service=None, pg_schema=None):
|
|
|
|
|
def upgrade(proposal, db=None, *, pg_service=None, pg_schema=None, progress=None):
|
|
|
|
|
if pg_service:
|
|
|
|
|
from paper_atlas.postgres.writer import WriteConnection
|
|
|
|
|
with closing(WriteConnection(pg_service, pg_schema)) as c:
|
|
|
|
|
if proposal['database'] != database_identity(c):
|
|
|
|
|
raise ValueError('Plan targets a different database')
|
|
|
|
|
try:
|
|
|
|
|
result = apply_plan(c, proposal); c.commit(); return result
|
|
|
|
|
result = apply_plan(c, proposal, progress=progress); c.commit(); return result
|
|
|
|
|
except BaseException:
|
|
|
|
|
c.rollback(); raise
|
|
|
|
|
require_sqlite_writable(db)
|
|
|
|
@@ -154,7 +187,7 @@ def upgrade(proposal, db=None, *, pg_service=None, pg_schema=None):
|
|
|
|
|
c.execute('PRAGMA foreign_keys=OFF') # Outside the transaction; no cascading deletes.
|
|
|
|
|
try:
|
|
|
|
|
c.execute('BEGIN IMMEDIATE')
|
|
|
|
|
result = apply_plan(c, proposal); c.commit(); return result
|
|
|
|
|
result = apply_plan(c, proposal, progress=progress); c.commit(); return result
|
|
|
|
|
except BaseException:
|
|
|
|
|
c.rollback(); raise
|
|
|
|
|
finally:
|
|
|
|
@@ -171,13 +204,14 @@ def main(argv=None, *, prog=None):
|
|
|
|
|
target = Path(args.out)
|
|
|
|
|
if target.exists():
|
|
|
|
|
raise ValueError('Refusing to overwrite an existing plan/report')
|
|
|
|
|
progress = lambda event: print(json.dumps({'phase':'exact_row_count', **event}), file=sys.stderr, flush=True)
|
|
|
|
|
if args.apply_plan:
|
|
|
|
|
raw = Path(args.apply_plan).read_bytes()
|
|
|
|
|
if hashlib.sha256(raw).hexdigest() != args.plan_sha256:
|
|
|
|
|
raise ValueError('Plan file SHA256 differs from approved bytes')
|
|
|
|
|
result = upgrade(json.loads(raw), args.db, pg_service=args.pg_service, pg_schema=args.pg_schema)
|
|
|
|
|
result = upgrade(json.loads(raw), args.db, pg_service=args.pg_service, pg_schema=args.pg_schema, progress=progress)
|
|
|
|
|
else:
|
|
|
|
|
result = plan(args.db, pg_service=args.pg_service, pg_schema=args.pg_schema)
|
|
|
|
|
result = plan(args.db, pg_service=args.pg_service, pg_schema=args.pg_schema, progress=progress)
|
|
|
|
|
target.write_text(json.dumps(result, ensure_ascii=False, indent=2)+'\n')
|
|
|
|
|
print(json.dumps({'out':str(target), 'sha256':hashlib.sha256(target.read_bytes()).hexdigest(),
|
|
|
|
|
'status':result.get('status','planned')}), flush=True)
|
|
|
|
|