From a864f285e42dc1648f771cccf88058a4505059bc Mon Sep 17 00:00:00 2001 From: padreug Date: Sun, 6 Jul 2025 00:24:49 +0200 Subject: [PATCH] Refactor m004_convert_to_gtq_storage migration: Streamlined the conversion of centavo amounts to GTQ by detecting the database type (PostgreSQL or SQLite) and applying appropriate data type changes and updates. This enhances clarity and ensures proper handling of data conversions across relevant tables. --- migrations.py | 105 ++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 77 deletions(-) diff --git a/migrations.py b/migrations.py index 6654c71..c8147f5 100644 --- a/migrations.py +++ b/migrations.py @@ -140,82 +140,33 @@ async def m003_add_max_daily_limit_config(db): async def m004_convert_to_gtq_storage(db): """ Convert centavo storage to GTQ storage by changing data types and converting existing data. + Handles both SQLite (data conversion only) and PostgreSQL (data + schema changes). """ - # Convert dca_deposits amounts from centavos to GTQ - await db.execute( - """ - UPDATE satoshimachine.dca_deposits - SET amount = CAST(amount AS DECIMAL(10,2)) / 100.0 - WHERE currency = 'GTQ' - """ - ) + # Detect database type + db_type = str(type(db)).lower() + is_postgres = 'postgres' in db_type or 'asyncpg' in db_type - # Convert dca_payments amounts from centavos to GTQ - await db.execute( - """ - UPDATE satoshimachine.dca_payments - SET amount_fiat = CAST(amount_fiat AS DECIMAL(10,2)) / 100.0 - """ - ) - - # Convert lamassu_transactions amounts from centavos to GTQ - await db.execute( - """ - UPDATE satoshimachine.lamassu_transactions - SET fiat_amount = CAST(fiat_amount AS DECIMAL(10,2)) / 100.0 - """ - ) - - # Convert fixed_mode_daily_limit from centavos to GTQ - await db.execute( - """ - UPDATE satoshimachine.dca_clients - SET fixed_mode_daily_limit = CAST(fixed_mode_daily_limit AS DECIMAL(10,2)) / 100.0 - WHERE fixed_mode_daily_limit IS NOT NULL - """ - ) - - # Convert max_daily_limit_gtq in config (if already in centavos) - await db.execute( - """ - UPDATE satoshimachine.lamassu_config - SET max_daily_limit_gtq = CAST(max_daily_limit_gtq AS DECIMAL(10,2)) / 100.0 - WHERE max_daily_limit_gtq > 1000 - """ - ) - - # Change column types to DECIMAL - await db.execute( - """ - ALTER TABLE satoshimachine.dca_deposits - ALTER COLUMN amount TYPE DECIMAL(10,2) - """ - ) - - await db.execute( - """ - ALTER TABLE satoshimachine.dca_payments - ALTER COLUMN amount_fiat TYPE DECIMAL(10,2) - """ - ) - - await db.execute( - """ - ALTER TABLE satoshimachine.lamassu_transactions - ALTER COLUMN fiat_amount TYPE DECIMAL(10,2) - """ - ) - - await db.execute( - """ - ALTER TABLE satoshimachine.dca_clients - ALTER COLUMN fixed_mode_daily_limit TYPE DECIMAL(10,2) - """ - ) - - await db.execute( - """ - ALTER TABLE satoshimachine.lamassu_config - ALTER COLUMN max_daily_limit_gtq TYPE DECIMAL(10,2) - """ - ) \ No newline at end of file + if is_postgres: + # PostgreSQL: Need to change column types first, then convert data + + # Change column types to DECIMAL(10,2) + await db.execute("ALTER TABLE satoshimachine.dca_deposits ALTER COLUMN amount TYPE DECIMAL(10,2)") + await db.execute("ALTER TABLE satoshimachine.dca_payments ALTER COLUMN amount_fiat TYPE DECIMAL(10,2)") + await db.execute("ALTER TABLE satoshimachine.lamassu_transactions ALTER COLUMN fiat_amount TYPE DECIMAL(10,2)") + await db.execute("ALTER TABLE satoshimachine.dca_clients ALTER COLUMN fixed_mode_daily_limit TYPE DECIMAL(10,2)") + await db.execute("ALTER TABLE satoshimachine.lamassu_config ALTER COLUMN max_daily_limit_gtq TYPE DECIMAL(10,2)") + + # Convert data from centavos to GTQ + await db.execute("UPDATE satoshimachine.dca_deposits SET amount = amount / 100.0 WHERE currency = 'GTQ'") + await db.execute("UPDATE satoshimachine.dca_payments SET amount_fiat = amount_fiat / 100.0") + await db.execute("UPDATE satoshimachine.lamassu_transactions SET fiat_amount = fiat_amount / 100.0") + await db.execute("UPDATE satoshimachine.dca_clients SET fixed_mode_daily_limit = fixed_mode_daily_limit / 100.0 WHERE fixed_mode_daily_limit IS NOT NULL") + await db.execute("UPDATE satoshimachine.lamassu_config SET max_daily_limit_gtq = max_daily_limit_gtq / 100.0 WHERE max_daily_limit_gtq > 1000") + + else: + # SQLite: Data conversion only (dynamic typing handles the rest) + await db.execute("UPDATE satoshimachine.dca_deposits SET amount = CAST(amount AS REAL) / 100.0 WHERE currency = 'GTQ'") + await db.execute("UPDATE satoshimachine.dca_payments SET amount_fiat = CAST(amount_fiat AS REAL) / 100.0") + await db.execute("UPDATE satoshimachine.lamassu_transactions SET fiat_amount = CAST(fiat_amount AS REAL) / 100.0") + await db.execute("UPDATE satoshimachine.dca_clients SET fixed_mode_daily_limit = CAST(fixed_mode_daily_limit AS REAL) / 100.0 WHERE fixed_mode_daily_limit IS NOT NULL") + await db.execute("UPDATE satoshimachine.lamassu_config SET max_daily_limit_gtq = CAST(max_daily_limit_gtq AS REAL) / 100.0 WHERE max_daily_limit_gtq > 1000") \ No newline at end of file