'Add the container_count column to the \'policy_stat\' table and
update it
:param conn: DB connection object'

def _migrate_add_container_count(self, conn):

    curs = conn.cursor()
    curs.executescript(('\n                                                DROP    TRIGGER    container_delete_ps;\n                                                DROP    TRIGGER    container_insert_ps;\n                                                ALTER    TABLE    policy_stat\n                                                ADD    COLUMN    container_count    INTEGER    DEFAULT    0;\n                                ' + POLICY_STAT_TRIGGER_SCRIPT))
    curs.executescript('\n                                UPDATE    policy_stat\n                                SET    container_count    =    (\n                                                SELECT    container_count\n                                                FROM    account_stat)\n                                WHERE    (\n                                                SELECT    COUNT(storage_policy_index)\n                                                FROM    policy_stat\n                                )    <=    1;\n\n                                INSERT    OR    REPLACE    INTO    policy_stat    (\n                                                storage_policy_index,\n                                                container_count,\n                                                object_count,\n                                                bytes_used\n                                )\n                                SELECT    p.storage_policy_index,\n                                                            c.count,\n                                                            p.object_count,\n                                                            p.bytes_used\n                                FROM    (\n                                                SELECT    storage_policy_index,\n                                                                            COUNT(*)    as    count\n                                                FROM    container\n                                                WHERE    deleted    =    0\n                                                GROUP    BY    storage_policy_index\n                                )    c\n                                JOIN    policy_stat    p\n                                ON    p.storage_policy_index    =    c.storage_policy_index\n                                WHERE    NOT    EXISTS(\n                                                SELECT    changes()    as    change\n                                                FROM    policy_stat\n                                                WHERE    change    <>    0\n                                );\n                                ')
    conn.commit()
