コード例 #1
0
def test_cleanup_table():
    dbops = DBOps()
    sql = "TRUNCATE cbc_schedule.test"
    with dbops.connection.cursor() as cursor:
        cursor.execute(sql)
    dbops.connection.commit()

    now = datetime.datetime.utcnow()
    past = datetime.datetime.now() - datetime.timedelta(days=31)
    sql1 = """
        INSERT INTO cbc_schedule.test
        (name, insert_timestamp)
        VALUES
        (%s, %s)
    """
    sql2 = """
        INSERT INTO cbc_schedule.test
        (name, insert_timestamp)
        VALUES
        (%s, %s)
    """
    with dbops.connection.cursor() as cursor:
        cursor.execute(sql1, ('test1', now))
        cursor.execute(sql2, ('test2', past))
    dbops.connection.commit()

    sql = "SELECT * FROM cbc_schedule.test"
    df = pd.read_sql(sql, dbops.connection)
    assert len(df) == 2

    dbops.cleanup_table('test', 30)
    sql = "SELECT * FROM cbc_schedule.test"
    df = pd.read_sql(sql, dbops.connection)
    assert len(df) == 1

    sql = "TRUNCATE cbc_schedule.test"
    with dbops.connection.cursor() as cursor:
        cursor.execute(sql)
    dbops.connection.commit()
コード例 #2
0
def cleanup_table(table, days):
    """ Cleans up old data in the specified table """
    dbops = DBOps()
    dbops.cleanup_table(table=table, days=days)