Update sqlalchemy from v1.4 to 2.0.

This commit is contained in:
tecnovert
2024-10-11 21:34:45 +02:00
parent bdc173187d
commit 445aa116ad
5 changed files with 216 additions and 186 deletions

View File

@@ -7,6 +7,7 @@
import json
import time
from sqlalchemy.sql import text
from sqlalchemy.orm import scoped_session
from .db import (
BidState,
@@ -79,7 +80,7 @@ def upgradeDatabaseData(self, data_version):
in_error = isErrorBidState(state)
swap_failed = isFailingBidState(state)
swap_ended = isFinalBidState(state)
session.execute('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)})
session.execute(text('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)}))
if data_version > 0 and data_version < 4:
for state in (BidStates.BID_REQUEST_SENT, BidStates.BID_REQUEST_ACCEPTED):
session.add(BidState(
@@ -112,16 +113,16 @@ def upgradeDatabase(self, db_version):
current_version = db_version
if current_version == 6:
session.execute('ALTER TABLE bids ADD COLUMN security_token BLOB')
session.execute('ALTER TABLE offers ADD COLUMN security_token BLOB')
session.execute(text('ALTER TABLE bids ADD COLUMN security_token BLOB'))
session.execute(text('ALTER TABLE offers ADD COLUMN security_token BLOB'))
db_version += 1
elif current_version == 7:
session.execute('ALTER TABLE transactions ADD COLUMN block_hash BLOB')
session.execute('ALTER TABLE transactions ADD COLUMN block_height INTEGER')
session.execute('ALTER TABLE transactions ADD COLUMN block_time INTEGER')
session.execute(text('ALTER TABLE transactions ADD COLUMN block_hash BLOB'))
session.execute(text('ALTER TABLE transactions ADD COLUMN block_height INTEGER'))
session.execute(text('ALTER TABLE transactions ADD COLUMN block_time INTEGER'))
db_version += 1
elif current_version == 8:
session.execute('''
session.execute(text('''
CREATE TABLE wallets (
record_id INTEGER NOT NULL,
coin_id INTEGER,
@@ -129,30 +130,30 @@ def upgradeDatabase(self, db_version):
wallet_data VARCHAR,
balance_type INTEGER,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
db_version += 1
elif current_version == 9:
session.execute('ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR')
session.execute(text('ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR'))
db_version += 1
elif current_version == 10:
session.execute('ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER')
session.execute('ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER')
session.execute('ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR')
session.execute('ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR')
session.execute('UPDATE smsgaddresses SET active_ind = 1, created_at = 1')
session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER'))
session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER'))
session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR'))
session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR'))
session.execute(text('UPDATE smsgaddresses SET active_ind = 1, created_at = 1'))
session.execute('ALTER TABLE offers ADD COLUMN addr_to VARCHAR')
session.execute(f'UPDATE offers SET addr_to = "{self.network_addr}"')
session.execute(text('ALTER TABLE offers ADD COLUMN addr_to VARCHAR'))
session.execute(text(f'UPDATE offers SET addr_to = "{self.network_addr}"'))
db_version += 1
elif current_version == 11:
session.execute('ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER')
session.execute('ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER')
session.execute('ALTER TABLE bids ADD COLUMN protocol_version INTEGER')
session.execute('ALTER TABLE offers ADD COLUMN protocol_version INTEGER')
session.execute('ALTER TABLE transactions ADD COLUMN tx_data BLOB')
session.execute(text('ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER'))
session.execute(text('ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER'))
session.execute(text('ALTER TABLE bids ADD COLUMN protocol_version INTEGER'))
session.execute(text('ALTER TABLE offers ADD COLUMN protocol_version INTEGER'))
session.execute(text('ALTER TABLE transactions ADD COLUMN tx_data BLOB'))
db_version += 1
elif current_version == 12:
session.execute('''
session.execute(text('''
CREATE TABLE knownidentities (
record_id INTEGER NOT NULL,
address VARCHAR,
@@ -167,15 +168,15 @@ def upgradeDatabase(self, db_version):
note VARCHAR,
updated_at BIGINT,
created_at BIGINT,
PRIMARY KEY (record_id))''')
session.execute('ALTER TABLE bids ADD COLUMN reject_code INTEGER')
session.execute('ALTER TABLE bids ADD COLUMN rate INTEGER')
session.execute('ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER')
session.execute('ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER')
PRIMARY KEY (record_id))'''))
session.execute(text('ALTER TABLE bids ADD COLUMN reject_code INTEGER'))
session.execute(text('ALTER TABLE bids ADD COLUMN rate INTEGER'))
session.execute(text('ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER'))
session.execute(text('ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER'))
db_version += 1
elif current_version == 13:
db_version += 1
session.execute('''
session.execute(text('''
CREATE TABLE automationstrategies (
record_id INTEGER NOT NULL,
active_ind INTEGER,
@@ -187,9 +188,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
session.execute('''
session.execute(text('''
CREATE TABLE automationlinks (
record_id INTEGER NOT NULL,
active_ind INTEGER,
@@ -204,9 +205,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
session.execute('''
session.execute(text('''
CREATE TABLE history (
record_id INTEGER NOT NULL,
concept_type INTEGER,
@@ -215,9 +216,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
session.execute('''
session.execute(text('''
CREATE TABLE bidstates (
record_id INTEGER NOT NULL,
active_ind INTEGER,
@@ -227,31 +228,31 @@ def upgradeDatabase(self, db_version):
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
session.execute('ALTER TABLE wallets ADD COLUMN active_ind INTEGER')
session.execute('ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER')
session.execute('ALTER TABLE eventqueue RENAME TO actions')
session.execute('ALTER TABLE actions RENAME COLUMN event_id TO action_id')
session.execute('ALTER TABLE actions RENAME COLUMN event_type TO action_type')
session.execute('ALTER TABLE actions RENAME COLUMN event_data TO action_data')
session.execute(text('ALTER TABLE wallets ADD COLUMN active_ind INTEGER'))
session.execute(text('ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER'))
session.execute(text('ALTER TABLE eventqueue RENAME TO actions'))
session.execute(text('ALTER TABLE actions RENAME COLUMN event_id TO action_id'))
session.execute(text('ALTER TABLE actions RENAME COLUMN event_type TO action_type'))
session.execute(text('ALTER TABLE actions RENAME COLUMN event_data TO action_data'))
elif current_version == 14:
db_version += 1
session.execute('ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB')
session.execute('ALTER TABLE xmr_swaps RENAME COLUMN coin_a_lock_refund_spend_tx_msg_id TO coin_a_lock_spend_tx_msg_id')
session.execute(text('ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB'))
session.execute(text('ALTER TABLE xmr_swaps RENAME COLUMN coin_a_lock_refund_spend_tx_msg_id TO coin_a_lock_spend_tx_msg_id'))
elif current_version == 15:
db_version += 1
session.execute('''
session.execute(text('''
CREATE TABLE notifications (
record_id INTEGER NOT NULL,
active_ind INTEGER,
event_type INTEGER,
event_data BLOB,
created_at BIGINT,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
elif current_version == 16:
db_version += 1
session.execute('''
session.execute(text('''
CREATE TABLE prefunded_transactions (
record_id INTEGER NOT NULL,
active_ind INTEGER,
@@ -261,25 +262,25 @@ def upgradeDatabase(self, db_version):
tx_type INTEGER,
tx_data BLOB,
used_by BLOB,
PRIMARY KEY (record_id))''')
PRIMARY KEY (record_id))'''))
elif current_version == 17:
db_version += 1
session.execute('ALTER TABLE knownidentities ADD COLUMN automation_override INTEGER')
session.execute('ALTER TABLE knownidentities ADD COLUMN visibility_override INTEGER')
session.execute('ALTER TABLE knownidentities ADD COLUMN data BLOB')
session.execute('UPDATE knownidentities SET active_ind = 1')
session.execute(text('ALTER TABLE knownidentities ADD COLUMN automation_override INTEGER'))
session.execute(text('ALTER TABLE knownidentities ADD COLUMN visibility_override INTEGER'))
session.execute(text('ALTER TABLE knownidentities ADD COLUMN data BLOB'))
session.execute(text('UPDATE knownidentities SET active_ind = 1'))
elif current_version == 18:
db_version += 1
session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING')
session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING')
session.execute(text('ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING'))
session.execute(text('ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING'))
elif current_version == 19:
db_version += 1
session.execute('ALTER TABLE bidstates ADD COLUMN in_error INTEGER')
session.execute('ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER')
session.execute('ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER')
session.execute(text('ALTER TABLE bidstates ADD COLUMN in_error INTEGER'))
session.execute(text('ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER'))
session.execute(text('ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER'))
elif current_version == 20:
db_version += 1
session.execute('''
session.execute(text('''
CREATE TABLE message_links (
record_id INTEGER NOT NULL,
active_ind INTEGER,
@@ -291,18 +292,18 @@ def upgradeDatabase(self, db_version):
msg_type INTEGER,
msg_sequence INTEGER,
msg_id BLOB,
PRIMARY KEY (record_id))''')
session.execute('ALTER TABLE offers ADD COLUMN bid_reversed INTEGER')
PRIMARY KEY (record_id))'''))
session.execute(text('ALTER TABLE offers ADD COLUMN bid_reversed INTEGER'))
elif current_version == 21:
db_version += 1
session.execute('ALTER TABLE offers ADD COLUMN proof_utxos BLOB')
session.execute('ALTER TABLE bids ADD COLUMN proof_utxos BLOB')
session.execute(text('ALTER TABLE offers ADD COLUMN proof_utxos BLOB'))
session.execute(text('ALTER TABLE bids ADD COLUMN proof_utxos BLOB'))
elif current_version == 22:
db_version += 1
session.execute('ALTER TABLE offers ADD COLUMN amount_to INTEGER')
session.execute(text('ALTER TABLE offers ADD COLUMN amount_to INTEGER'))
elif current_version == 23:
db_version += 1
session.execute('''
session.execute(text('''
CREATE TABLE checkedblocks (
record_id INTEGER NOT NULL,
created_at BIGINT,
@@ -310,8 +311,8 @@ def upgradeDatabase(self, db_version):
block_height INTEGER,
block_hash BLOB,
block_time INTEGER,
PRIMARY KEY (record_id))''')
session.execute('ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB')
PRIMARY KEY (record_id))'''))
session.execute(text('ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB'))
if current_version != db_version:
self.db_version = db_version
self.setIntKV('db_version', db_version, session)