<s> import logging <EOL> import psycopg2 <EOL> import psycopg2 . extensions <EOL> from migrator import DatabaseHandler <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class PostgresDatabaseHandler ( DatabaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def __get_open_connection ( self , connection_hash ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> hsh = connection_hash <EOL> c = template . format ( hsh [ "<STR_LIT:host>" ] , hsh [ "<STR_LIT>" ] , hsh [ "<STR_LIT:user>" ] , hsh [ "<STR_LIT:password>" ] ) <EOL> return psycopg2 . connect ( c ) <EOL> def delete_make_new ( self , system_connection_hash , database_name ) : <EOL> """<STR_LIT>""" <EOL> conn = self . __get_open_connection ( system_connection_hash ) <EOL> conn . set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> cursor = conn . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" . format ( database_name ) ) <EOL> cursor . execute ( "<STR_LIT>" . format ( database_name ) ) <EOL> cursor . close ( ) <EOL> conn . close ( ) <EOL> def __execute ( self , conn_hash , sql ) : <EOL> """<STR_LIT>""" <EOL> conn = self . __get_open_connection ( conn_hash ) <EOL> conn . set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> cursor = conn . cursor ( ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> except Exception as e : <EOL> logger . error ( "<STR_LIT>" % e ) <EOL> raise <EOL> finally : <EOL> cursor . close ( ) <EOL> conn . close ( ) <EOL> def user_defined_tables_exist ( self , connection_hash ) : <EOL> sql = """<STR_LIT>""" <EOL> conn = self . __get_open_connection ( connection_hash ) <EOL> cursor = conn . cursor ( ) <EOL> cursor . execute ( sql ) <EOL> r = cursor . fetchall ( ) <EOL> cursor . close ( ) <EOL> conn . close ( ) <EOL> return ( len ( r ) != <NUM_LIT:0> ) <EOL> def create_tracking_table ( self , connection_hash ) : <EOL> """<STR_LIT>""" <EOL> sql = """<STR_LIT>""" <EOL> self . __execute ( connection_hash , sql ) <EOL> def is_in_tracking_table ( self , connection_hash , script_name ) : <EOL> """<STR_LIT>""" <EOL> sql = "<STR_LIT>" . format ( script_name ) <EOL> conn = self . __get_open_connection ( connection_hash ) <EOL> cursor = conn . cursor ( ) <EOL> cursor . execute ( sql ) <EOL> r = cursor . fetchall ( ) <EOL> cursor . close ( ) <EOL> conn . close ( ) <EOL> return ( len ( r ) > <NUM_LIT:0> ) <EOL> def record_script_in_tracking_table ( self , conn_hash , script_name ) : <EOL> sql = "<STR_LIT>" <EOL> self . __execute ( conn_hash , sql . format ( script_name ) ) <EOL> def execute ( self , conn_hash , sql ) : <EOL> self . __execute ( conn_hash , sql . format ( sql ) ) </s>
