Created
September 10, 2014 00:18
-
-
Save icook/d6532b2de1020e08a32e to your computer and use it in GitHub Desktop.
Fix wrong SQLA polymorphic
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
+@manager.command | |
+def fix_credits(): | |
+ from simplecoin.models import CreditExchange | |
+ credit_table = Credit.__table__ | |
+ credit_exc_table = CreditExchange.__table__ | |
+ for credit in Credit.query.options(db.joinedload('block')).order_by(Credit.id): | |
+ correct = 1 | |
+ if credit.block and credit.block.currency == credit.currency: | |
+ correct = 0 | |
+ | |
+ print "{} {} {} {}".format(str(credit.id).zfill(4), credit.currency, | |
+ credit.block.currency, credit.type) | |
+ | |
+ id = credit.id | |
+ if credit.type != correct and correct == 0: | |
+ print "\tCreditExchange => Credit".format(credit.id) | |
+ a = credit_exc_table.delete().where(credit_exc_table.c.id == id) | |
+ b = credit_table.update().where(credit_table.c.id == id).values(type=0) | |
+ db.session.execute(a) | |
+ db.session.execute(b) | |
+ elif credit.type != correct and correct == 1: | |
+ print "\tCredit => CreditExchange".format(id) | |
+ a = credit_exc_table.insert().values(id=id) | |
+ b = credit_table.update().where(credit_table.c.id == id).values(type=1) | |
+ db.session.execute(a) | |
+ db.session.execute(b) | |
+ else: | |
+ print "\tCorrect!".format(credit.id) | |
+ pass | |
+ | |
+ db.session.commit() | |
+ | |
+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment