Last active
June 19, 2018 12:00
-
-
Save samuelbalogh/0dd3147ad52dde8f426a97b638be3acd to your computer and use it in GitHub Desktop.
pseudocode for how not to perform datamigrations with SQLAclhemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy as sa | |
invoices_table = sa.Table('invoices', sa.Metadata()) | |
invoices = select([invoices_table]).where(invoice_type='oldie') | |
for invoice in old_invoices: | |
invoices_table.update( | |
).where(id=invoice.id).values(invoice_type='cutting_edge') | |
project_to_update = select([projects]).where(invoice.project_id == projects.id) | |
project_values = { | |
'total_spend': project_to_update.total_spend, | |
'total_projected_spend': project_to_update.total_projected_spend, | |
} | |
if invoice.status == 'paid': | |
project_values['total_spend'] += invoice.amount | |
elif invoice.status == 'pending': | |
project_values['total_projected_spend'] += invoice.amount | |
query = projects.update().where( | |
projects.c.id == rows_to_update.id | |
).values(project_values) | |
connection.execute(query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment