def setup(): # get connection connection = apsw.Connection(etc.database_path) # use foreign keys cursor = connection.cursor() cursor.execute("PRAGMA foreign_keys = ON;") cursor.execute("PRAGMA defer_foreign_keys = ON;") # check foreign keys violations = list(cursor.execute("PRAGMA foreign_key_check;")) if violations: msg = "Foreign key check failed! {0}" raise Exception(msg.format(repr(violations))) # check integrity rows = cursor.execute("PRAGMA integrity_check;").fetchall() if not (len(rows) == 1 and rows[0][0] == "ok"): raise Exception("Integrity check failed!") # now ready for global use etc.database_connection = connection # migrate script = "PRAGMA user_version;" db_version = sql.fetchone(script, cursor=cursor)["user_version"] while db_version in _MIGRATIONS: sql.execute(_MIGRATIONS[db_version], cursor=cursor) db_version += 1 script = "PRAGMA user_version = {0};".format(db_version) sql.execute(script, cursor=cursor)
def add_revoke_secret(channel_id, secret_hash, secret_value, cursor=None): args = { "channel_id": channel_id, "secret_hash": secret_hash, "secret_value": secret_value } sql.execute(_ADD_REVOKE_SECRET, args=args, cursor=cursor)
def complete_hub_connection(data, cursor=None): cursor = cursor or sql.get_cursor() cursor.execute("BEGIN TRANSACTION") set_next_revoke_secret_hash( handle=data["handle"], next_revoke_secret_hash=data["next_revoke_secret_hash"], cursor=cursor) sql.execute(_COMPLETE_CONNECTION, data, cursor=cursor) add_revoke_secret_args = { "secret_hash": data["secret_hash"], "secret_value": data["secret_value"], "channel_id": data["c2h_channel_id"], } sql.execute(_ADD_REVOKE_SECRET, args=add_revoke_secret_args, cursor=cursor) cursor.execute("COMMIT")
def complete_hub_connection(data, cursor=None): cursor = cursor or sql.get_cursor() cursor.execute("BEGIN TRANSACTION") set_next_revoke_secret_hash( handle=data["handle"], next_revoke_secret_hash=data["next_revoke_secret_hash"], cursor=cursor ) sql.execute(_COMPLETE_CONNECTION, data, cursor=cursor) add_revoke_secret_args = { "secret_hash": data["secret_hash"], "secret_value": data["secret_value"], "channel_id": data["c2h_channel_id"], } sql.execute(_ADD_REVOKE_SECRET, args=add_revoke_secret_args, cursor=cursor) cursor.execute("COMMIT")
def add_hub_connection(data, cursor=None): cursor = cursor or sql.get_cursor() cursor.execute("BEGIN TRANSACTION") sql.execute(_ADD_HUB_CONNECTION, data, cursor=cursor) cursor.execute("COMMIT")