Exemplo n.º 1
    def internal_test_reindex(self, name, sql):
        c = self.db.cursor()
        # Create index
        oid1 = get_rel_oid(c, name)
        stmt1 = fetch_single_val(c, "SELECT pg_get_indexdef(%s, 0, false)", [oid1])

        # Recreate index
        pgtool.pg_reindex(self.db, name)
        oid2 = get_rel_oid(c, name)
        stmt2 = fetch_single_val(c, "SELECT pg_get_indexdef(%s, 0, false)", [oid2])
        self.assertTrue(oid2 > 0)

        # New oid must be allocated for the new index
        self.assertNotEqual(oid1, oid2)
        # But index expressions must remain equal
        self.assertEqual(stmt1, stmt2)
Exemplo n.º 2
    def test_reindex_recovery(self):
        """Test error recovery when reindex fails"""
        c = self.db.cursor()
        # Create invalid index. DataError: invalid input syntax for integer: "a"
        with self.assertRaises(psycopg2.DataError):
            c.execute("CREATE INDEX CONCURRENTLY reindex_idx2 ON reindex_tbl((txt::int))")
        oid1 = get_rel_oid(c, "reindex_idx2")

        # Reindex fails too
        with self.assertRaises(psycopg2.DataError):
            pgtool.pg_reindex(self.db, "reindex_idx2")
        oid2 = get_rel_oid(c, "reindex_idx2")

        # Make sure the index wasn't replaced or dropped
        self.assertEqual(oid1, oid2)

        # Make sure we didn't leave behind an invalid index
        SELECT array_agg(indexrelid::regclass::text) FROM pg_catalog.pg_index
            WHERE indrelid='pgtool_test.reindex_tbl'::regclass AND NOT indisvalid
        self.assertEqual(c.fetchone()[0], ["reindex_idx2"])