<s> from __future__ import print_function <EOL> from contextlib import contextmanager <EOL> import re <EOL> import sys <EOL> import traceback <EOL> from six . moves import input <EOL> from postgres import Postgres <EOL> from postgres . cursors import SimpleCursorBase <EOL> from psycopg2 import IntegrityError , ProgrammingError <EOL> @ contextmanager <EOL> def just_yield ( obj ) : <EOL> yield obj <EOL> class DB ( Postgres ) : <EOL> def get_cursor ( self , cursor = None , ** kw ) : <EOL> if cursor : <EOL> if kw : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return just_yield ( cursor ) <EOL> return super ( DB , self ) . get_cursor ( ** kw ) <EOL> def self_check ( self ) : <EOL> with self . get_cursor ( ) as cursor : <EOL> check_db ( cursor ) <EOL> def check_db ( cursor ) : <EOL> """<STR_LIT>""" <EOL> _check_balances ( cursor ) <EOL> _check_tips ( cursor ) <EOL> _check_bundles ( cursor ) <EOL> def _check_tips ( cursor ) : <EOL> """<STR_LIT>""" <EOL> conflicting_tips = cursor . one ( """<STR_LIT>""" ) <EOL> assert conflicting_tips == <NUM_LIT:0> <EOL> def _check_balances ( cursor ) : <EOL> """<STR_LIT>""" <EOL> b = cursor . all ( """<STR_LIT>""" ) <EOL> assert len ( b ) == <NUM_LIT:0> , "<STR_LIT>" . format ( b ) <EOL> def _check_bundles ( cursor ) : <EOL> """<STR_LIT>""" <EOL> b = cursor . all ( """<STR_LIT>""" ) <EOL> assert len ( b ) == <NUM_LIT:0> , "<STR_LIT>" . format ( b ) <EOL> def run_migrations ( db ) : <EOL> v = <NUM_LIT:0> <EOL> db_meta = db . one ( "<STR_LIT>" ) <EOL> if db_meta : <EOL> v = db . one ( "<STR_LIT>" ) <EOL> migrations = open ( '<STR_LIT>' ) . read ( ) . split ( '<STR_LIT>' ) <EOL> for m in migrations [ <NUM_LIT:1> : ] : <EOL> n , sql = m . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> n = int ( n ) <EOL> if v >= n : <EOL> continue <EOL> print ( '<STR_LIT>' % n ) <EOL> try : <EOL> db . run ( sql ) <EOL> except ( IntegrityError , ProgrammingError ) : <EOL> traceback . print_exc ( ) <EOL> r = input ( '<STR_LIT>' ) <EOL> if r . lower ( ) != '<STR_LIT:y>' : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> db . run ( """<STR_LIT>""" , ( n , ) ) <EOL> if db . one ( "<STR_LIT>" ) == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> db . run ( open ( '<STR_LIT>' ) . read ( ) ) <EOL> print ( '<STR_LIT>' if n != v else '<STR_LIT>' ) <EOL> return n - v <EOL> def show_table ( db , table ) : <EOL> assert re . match ( r'<STR_LIT>' , table ) <EOL> print ( '<STR_LIT>' . format ( table ) ) <EOL> data = db . all ( '<STR_LIT>' + table ) <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> return <EOL> widths = list ( len ( k ) for k in data [ <NUM_LIT:0> ] . _fields ) <EOL> for row in data : <EOL> for i , v in enumerate ( row ) : <EOL> widths [ i ] = max ( widths [ i ] , len ( str ( v ) ) ) <EOL> for k , w in zip ( data [ <NUM_LIT:0> ] . _fields , widths ) : <EOL> print ( "<STR_LIT>" . format ( str ( k ) , width = w ) , end = '<STR_LIT>' ) <EOL> print ( ) <EOL> for row in data : <EOL> for v , w in zip ( row , widths ) : <EOL> print ( "<STR_LIT>" . format ( str ( v ) , width = w ) , end = '<STR_LIT>' ) <EOL> print ( ) <EOL> SimpleCursorBase . show_table = show_table <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from liberapay import wireup <EOL> db = wireup . minimal_algorithm . run ( ) [ '<STR_LIT>' ] <EOL> print ( '<STR_LIT>' ) <EOL> db . self_check ( ) <EOL> r = run_migrations ( db ) <EOL> if r : <EOL> print ( '<STR_LIT>' ) <EOL> db . self_check ( ) </s>
