from totalimpactwebapp import db

if __name__ == "__main__":
    # update_partition function is defined in an sql file in this repo
    # make new tables for next month, if not already made
    # is safe to call daily
    sql = "SELECT update_partitions('2014-11-01','public','snap','tableownerdummy','last_collected_date','month')"
    result = db.engine.execute(sql)
    db.session.commit()
    print "new tables created:", result.fetchall()

    # see how many partition tables there are
    # wildcards need to be escaped as %%
    sql = "SELECT tablename FROM pg_catalog.pg_tables where tablename like 'snap%%' order by tablename"
    result = db.engine.execute(sql)
    db.session.commit()
    print "all snap tables:"
    for (tablename, ) in result.fetchall():
        print " *", tablename

@@ -27,16 +27,386 @@
 ort db%0A%0A
+# from http://stackoverflow.com/questions/21469884/commiting-a-transaction-from-a-postgresql-function-in-flask%0Adef run_sql(sql):%0A    connection = db.engine.connect()%0A    trans = connection.begin()%0A    retval = None%0A    try:%0A        retval = connection.execute(sql).fetchall()%0A%0A        trans.commit()%0A    except:%0A        trans.rollback()%0A        raise%0A    return retval%0A%0A
 if __nam
@@ -713,53 +713,19 @@
 t = 
-db.engine.execute(sql)%0A    db.session.commit(
+run_sql(sql
 )%0A  
@@ -761,27 +761,16 @@
 , result
-.fetchall()
 %0A%0A    # 
@@ -971,53 +971,19 @@
 t = 
-db.engine.execute(sql)%0A    db.session.commit(
+run_sql(sql
 )%0A  
@@ -1040,27 +1040,16 @@
 n result
-.fetchall()
 :%0A      

fix transaction issue