Preserve NUL text through PostgreSQL migration and resume
Metadata integrity checks / metadata (push) Successful in 1m7s
Metadata integrity checks / metadata (push) Successful in 1m7s
This commit is contained in:
@@ -14,6 +14,7 @@ import zlib
|
||||
from paper_atlas.db.submissions import timestamp
|
||||
from paper_atlas.postgres.client import connect, SEARCH_VECTOR
|
||||
from paper_atlas.postgres.schema import TABLES, inspect, fingerprint, quote, select_sql, table_sql
|
||||
from paper_atlas.postgres import text
|
||||
|
||||
|
||||
def digest_file(path):
|
||||
@@ -96,6 +97,41 @@ def sequences(pg, source, stage, plan):
|
||||
return result
|
||||
|
||||
|
||||
def upgrade_text_codec(pg, control, stage, plan):
|
||||
"""Upgrade committed legacy staging rows once; never recopy their source."""
|
||||
changed = {}
|
||||
with pg.transaction():
|
||||
copied = {r[0] for r in pg.execute('SELECT name FROM ' + qualify(control, 'tables'))}
|
||||
if not copied <= plan.keys():
|
||||
raise ValueError('Unknown committed staging tables')
|
||||
# An older run may have reached constraint validation. Replacing SOH
|
||||
# can temporarily equal another legacy key even though the final mapping
|
||||
# is bijective. Rebuild unpublished constraints/indexes after the upgrade.
|
||||
constraints = pg.execute('''SELECT cl.relname,co.conname FROM pg_constraint co
|
||||
JOIN pg_class cl ON cl.oid=co.conrelid JOIN pg_namespace ns ON ns.oid=cl.relnamespace
|
||||
WHERE ns.nspname=%s AND co.contype IN ('f','p','u')
|
||||
ORDER BY CASE WHEN co.contype='f' THEN 0 ELSE 1 END''', (stage,)).fetchall()
|
||||
for table, name in constraints:
|
||||
pg.execute('ALTER TABLE ' + qualify(stage, table) + ' DROP CONSTRAINT ' + quote(name))
|
||||
indexes = pg.execute('''SELECT cl.relname FROM pg_index i JOIN pg_class cl ON cl.oid=i.indexrelid
|
||||
JOIN pg_namespace ns ON ns.oid=cl.relnamespace WHERE ns.nspname=%s AND i.indisunique''', (stage,)).fetchall()
|
||||
for name, in indexes:
|
||||
pg.execute('DROP INDEX ' + qualify(stage, name))
|
||||
for table in plan:
|
||||
if table not in copied:
|
||||
continue
|
||||
columns = [quote(c['name']) for c in plan[table]['columns'] if c['type'] == 'TEXT']
|
||||
if columns:
|
||||
assignments = ','.join(c + '=replace(' + c + ',%s,%s)' for c in columns)
|
||||
predicate = ' OR '.join('strpos(' + c + ',%s)>0' for c in columns)
|
||||
params = [v for c in columns for v in (text.ESCAPE, text.encode(text.ESCAPE))]
|
||||
params += [text.ESCAPE] * len(columns)
|
||||
changed[table] = pg.execute('UPDATE ' + qualify(stage, table) + ' SET ' + assignments +
|
||||
' WHERE ' + predicate, params).rowcount
|
||||
pg.execute('UPDATE ' + qualify(control, 'run') + ' SET text_codec=%s WHERE id=1', (text.CODEC,))
|
||||
return changed
|
||||
|
||||
|
||||
def migrate(db, service, out, *, schema='research', storage_path=None, min_free_gib=8):
|
||||
if not re.fullmatch(r'[a-z][a-z0-9_]{0,35}', schema):
|
||||
raise ValueError('Use a short lowercase target schema name')
|
||||
@@ -129,20 +165,28 @@ def migrate(db, service, out, *, schema='research', storage_path=None, min_free_
|
||||
'(id INTEGER PRIMARY KEY CHECK(id=1), source_json TEXT NOT NULL, stage TEXT NOT NULL, status TEXT NOT NULL)')
|
||||
pg.execute('CREATE TABLE IF NOT EXISTS ' + qualify(control, 'tables') +
|
||||
'(name TEXT PRIMARY KEY, rows BIGINT NOT NULL, sha256 TEXT NOT NULL, verified BOOLEAN NOT NULL DEFAULT false)')
|
||||
run = pg.execute('SELECT source_json,stage,status FROM ' + qualify(control, 'run') + ' WHERE id=1').fetchone()
|
||||
pg.execute('ALTER TABLE ' + qualify(control, 'run') + " ADD COLUMN IF NOT EXISTS text_codec TEXT NOT NULL DEFAULT 'legacy'")
|
||||
run = pg.execute('SELECT source_json,stage,status,text_codec FROM ' + qualify(control, 'run') + ' WHERE id=1').fetchone()
|
||||
if run:
|
||||
if json.loads(run[0]) != identity or run[2] == 'complete':
|
||||
raise ValueError('Resume requires the exact same unpublished SQLite snapshot')
|
||||
stage = run[1]
|
||||
if run[3] == 'legacy':
|
||||
report['legacy_text_upgrade_rows'] = upgrade_text_codec(pg, control, stage, plan)
|
||||
elif run[3] != text.CODEC:
|
||||
raise ValueError('Unsupported staging text storage codec')
|
||||
else:
|
||||
stage = schema + '_stage_' + uuid.uuid4().hex[:12]
|
||||
with pg.transaction():
|
||||
pg.execute('CREATE SCHEMA ' + quote(stage))
|
||||
for table, spec in plan.items():
|
||||
pg.execute(table_sql(stage, table, spec))
|
||||
pg.execute('INSERT INTO ' + qualify(control, 'run') + ' VALUES(1,%s,%s,%s)',
|
||||
(json.dumps(identity, sort_keys=True), stage, 'copying'))
|
||||
report.update(status='copying', stage=stage); save(out, report)
|
||||
pg.execute('INSERT INTO ' + qualify(control, 'run') +
|
||||
'(id,source_json,stage,status,text_codec) VALUES(1,%s,%s,%s,%s)',
|
||||
(json.dumps(identity, sort_keys=True), stage, 'copying', text.CODEC))
|
||||
report.update(status='copying', stage=stage, text_codec=text.CODEC,
|
||||
content_hash_scope='Decoded original SQLite values; storage escapes are reversible.')
|
||||
save(out, report)
|
||||
try:
|
||||
for table, spec in plan.items():
|
||||
free_space(storage_path, min_free_gib)
|
||||
@@ -157,7 +201,7 @@ def migrate(db, service, out, *, schema='research', storage_path=None, min_free_
|
||||
columns = ','.join(quote(c['name']) for c in spec['columns'])
|
||||
with pg.cursor().copy('COPY ' + qualify(stage, table) + '(' + columns + ') FROM STDIN') as copy:
|
||||
for row in source.execute(select_sql(None, table, spec)):
|
||||
copy.write_row(row); update_hash(digest, row); rows += 1
|
||||
copy.write_row(tuple(map(text.encode, row))); update_hash(digest, row); rows += 1
|
||||
if rows % 10000 == 0:
|
||||
free_space(storage_path, min_free_gib)
|
||||
if rows % 100000 == 0:
|
||||
@@ -172,14 +216,16 @@ def migrate(db, service, out, *, schema='research', storage_path=None, min_free_
|
||||
report['sequences'] = sequences(pg, source, stage, plan)
|
||||
report['status'] = 'verifying_contents'; save(out, report)
|
||||
for table, spec in plan.items():
|
||||
digest = hashlib.sha256(); rows = 0
|
||||
digest = hashlib.sha256(); storage_digest = hashlib.sha256(); rows = 0
|
||||
with pg.transaction(), pg.cursor(name='verify_' + table) as cursor:
|
||||
cursor.itersize = 512
|
||||
cursor.execute(select_sql(stage, table, spec))
|
||||
for row in cursor:
|
||||
update_hash(digest, row); rows += 1
|
||||
update_hash(storage_digest, row)
|
||||
update_hash(digest, tuple(map(text.decode, row))); rows += 1
|
||||
entry = report['tables'][table]
|
||||
entry.update(target_rows=rows, target_sha256=digest.hexdigest())
|
||||
entry.update(target_rows=rows, target_sha256=digest.hexdigest(),
|
||||
target_storage_sha256=storage_digest.hexdigest())
|
||||
if rows != entry['source_rows'] or digest.hexdigest() != entry['source_sha256']:
|
||||
raise ValueError('Complete content verification failed for ' + table)
|
||||
pg.execute('UPDATE ' + qualify(control, 'tables') + ' SET verified=true WHERE name=%s', (table,))
|
||||
@@ -207,6 +253,7 @@ def migrate(db, service, out, *, schema='research', storage_path=None, min_free_
|
||||
report.update(status='prepared', foreign_keys='all_validated', checks='all_validated', native_search_index=True)
|
||||
save(out, report)
|
||||
with pg.transaction():
|
||||
pg.execute('COMMENT ON SCHEMA ' + quote(stage) + " IS 'paper-atlas:text-codec=" + text.CODEC + "'")
|
||||
pg.execute('ALTER SCHEMA ' + quote(stage) + ' RENAME TO ' + quote(schema))
|
||||
pg.execute('UPDATE ' + qualify(control, 'run') + " SET status='complete' WHERE id=1")
|
||||
report.update(status='complete', finished_at=timestamp())
|
||||
|
||||
Reference in New Issue
Block a user