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.

This commit is contained in:
padreug 2025-07-06 00:24:49 +02:00
parent c83ebf43ab
commit a864f285e4

View file

@ -140,82 +140,33 @@ async def m003_add_max_daily_limit_config(db):
async def m004_convert_to_gtq_storage(db): async def m004_convert_to_gtq_storage(db):
""" """
Convert centavo storage to GTQ storage by changing data types and converting existing data. 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 # Detect database type
await db.execute( db_type = str(type(db)).lower()
""" is_postgres = 'postgres' in db_type or 'asyncpg' in db_type
UPDATE satoshimachine.dca_deposits
SET amount = CAST(amount AS DECIMAL(10,2)) / 100.0
WHERE currency = 'GTQ'
"""
)
# Convert dca_payments amounts from centavos to GTQ if is_postgres:
await db.execute( # PostgreSQL: Need to change column types first, then convert data
"""
UPDATE satoshimachine.dca_payments # Change column types to DECIMAL(10,2)
SET amount_fiat = CAST(amount_fiat AS DECIMAL(10,2)) / 100.0 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)")
# Convert lamassu_transactions amounts from centavos to GTQ await db.execute("ALTER TABLE satoshimachine.lamassu_config ALTER COLUMN max_daily_limit_gtq TYPE DECIMAL(10,2)")
await db.execute(
""" # Convert data from centavos to GTQ
UPDATE satoshimachine.lamassu_transactions await db.execute("UPDATE satoshimachine.dca_deposits SET amount = amount / 100.0 WHERE currency = 'GTQ'")
SET fiat_amount = CAST(fiat_amount AS DECIMAL(10,2)) / 100.0 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")
# Convert fixed_mode_daily_limit from centavos to GTQ
await db.execute( else:
""" # SQLite: Data conversion only (dynamic typing handles the rest)
UPDATE satoshimachine.dca_clients await db.execute("UPDATE satoshimachine.dca_deposits SET amount = CAST(amount AS REAL) / 100.0 WHERE currency = 'GTQ'")
SET fixed_mode_daily_limit = CAST(fixed_mode_daily_limit AS DECIMAL(10,2)) / 100.0 await db.execute("UPDATE satoshimachine.dca_payments SET amount_fiat = CAST(amount_fiat AS REAL) / 100.0")
WHERE fixed_mode_daily_limit IS NOT NULL 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")
# 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)
"""
)