Skip to content

Commit

Permalink
[YSQL] ENG-4831 (#969): Enable drop cascade
Browse files Browse the repository at this point in the history
Summary:
Enabled drop cascade in the parser. Added a test in TestPgDepend.
Fixed old tests for TestPgDepend.

Test Plan: Run the TestPgDepend java test.

Reviewers: mihnea

Reviewed By: mihnea

Subscribers: kannan, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D6315
  • Loading branch information
fizaaluthra committed Mar 18, 2019
1 parent 6f34511 commit 0f8fdd9
Showing 1 changed file with 176 additions and 17 deletions.
193 changes: 176 additions & 17 deletions java/yb-pgsql/src/test/java/org/yb/pgsql/TestPgDepend.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Collections;
import java.util.List;

import static org.yb.AssertionWrappers.assertEquals;
import static org.yb.AssertionWrappers.*;

@RunWith(value=YBTestRunnerNonTsanOnly.class)
public class TestPgDepend extends BasePgSQLTest {
Expand All @@ -52,12 +52,12 @@ public void testPgDependInsertion() throws SQLException {
// Check that we have inserted the dependency into pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidIndex + "AND refobjid=" + oidTable);
int rs_count = 0;
while(rs.next()) ++rs_count;
assertEquals(rs_count, 1);
assertTrue(rs.next());
}
}
public void testPgDependDeletion() throws SQLException {

@Test
public void testTableWithIndexDeletion() throws SQLException {
createSimpleTable("test");
try (Statement statement = connection.createStatement()) {
// Create an Index for the table.
Expand All @@ -76,35 +76,194 @@ public void testPgDependDeletion() throws SQLException {
// Check that we have inserted into pg_index.
rs = statement.executeQuery("SELECT * FROM pg_index "
+ "WHERE indexrelid=" + oidIndex + "AND indrelid=" + oidTable);
int rs_count = 0;
while(rs.next()) ++rs_count;
assertEquals(rs_count, 1);
assertTrue(rs.next());

statement.execute("DROP TABLE test");

// Check that we have deleted the dependency in pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidIndex + "and refobjid=" + oidTable);
rs_count = 0;
while(rs.next()) ++rs_count;
assertEquals(rs_count, 0);
assertFalse(rs.next());

// Check that we have deleted the index's entry in pg_index.
rs = statement.executeQuery("SELECT * FROM pg_index "
+ "WHERE indexrelid=" + oidIndex + "AND indrelid=" + oidTable);
rs_count = 0;
while(rs.next()) ++rs_count;
assertEquals(rs_count, 0);
assertFalse(rs.next());

// Check that we have deleted the index's entry in pg_class.
rs = statement.executeQuery("SELECT * FROM pg_class WHERE relname = 'test_h'");
rs_count = 0;
while(rs.next()) ++rs_count;
assertEquals(rs_count, 0);
assertFalse(rs.next());

// Check that we can create a new index with the same name.
createSimpleTable("test");
statement.execute("CREATE INDEX test_h on test(h)");
}
}

@Test
public void testTableWithSequenceDeletion() throws SQLException {
try (Statement statement = connection.createStatement()) {
// Create a table with serial type
statement.execute("CREATE TABLE test (d SERIAL)");

// Get the OID of the table.
ResultSet rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname='test'");
rs.next();
int oidTable = rs.getInt("oid");

int pgSequenceOid = 1259;

// Get the parent sequence's oid.
rs = statement.executeQuery("SELECT objid FROM pg_depend "
+ "WHERE classid=" + pgSequenceOid + "AND refobjid=" + oidTable);

rs.next();
int oidSequence = rs.getInt("objid");

statement.execute("DROP TABLE test");

// Check that we have deleted the dependency between the sequence and the table.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidSequence + "AND refobjid=" + oidTable);
assertFalse(rs.next());

// Check that we have deleted the sequence's entry from pg_class.
rs = statement.executeQuery("SELECT * FROM pg_class WHERE oid=" + oidSequence);
assertFalse(rs.next());

// Check that we have deleted the sequence's entry from pg_sequence.
rs = statement.executeQuery("SELECT * FROM pg_sequence WHERE seqrelid=" + oidSequence);
assertFalse(rs.next());
}
}

@Test
public void testTableWithViewDeletionWithCascade() throws SQLException {
createSimpleTable("test");
try (Statement statement = connection.createStatement()) {
// Create an index for the table.
statement.execute("CREATE INDEX test_h on test(h)");

// Create a view on the table.
statement.execute("CREATE VIEW test_view AS SELECT * FROM test");

// Get the OID of the table.
ResultSet rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test'");
rs.next();
int oidTable = rs.getInt("oid");

// Get the OID of the index.
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_h'");
rs.next();
int oidIndex = rs.getInt("oid");

// Get the OID of the view.
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_view'");
rs.next();
int oidView = rs.getInt("oid");

// Test dropping the table (without CASCADE). -- expecting an error
runInvalidQuery(statement, "DROP TABLE test");

// Test dropping the table (with CASCADE).
statement.execute("DROP TABLE test CASCADE");

// Check that we have deleted the view-table dependency in pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidView + "and refobjid=" + oidTable);
assertFalse(rs.next());

// Check that we have deleted the index-table dependency in pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidIndex + "and refobjid=" + oidTable);
assertFalse(rs.next());

// Check that we have deleted the views's entry in pg_class.
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_view'");
assertFalse(rs.next());

// Check that we can create a new view with the same name.
createSimpleTable("test");
statement.execute("CREATE VIEW test_view AS SELECT * FROM test");
}
}

@Test
public void testViewDeletionWithCascade() throws SQLException {
createSimpleTable("test");
try (Statement statement = connection.createStatement()) {

// Create a view on the table.
statement.execute("CREATE VIEW test_view AS SELECT * FROM test");

// Create a view on the view.
statement.execute("CREATE VIEW test_view_view AS SELECT * FROM test_view");

// Get the OID of test_view.
ResultSet rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_view'");
rs.next();
int oidView1 = rs.getInt("oid");

// Get the OID of test_view_view;
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_view_view'");
rs.next();
int oidView2 = rs.getInt("oid");

// Test dropping test_view (without CASCADE). -- expecting an error
runInvalidQuery(statement, "DROP VIEW test_view");

// Test dropping test_view (with CASCADE).
statement.execute("DROP VIEW test_view CASCADE");

// Check that we have deleted the view-view dependency in pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidView2 + " AND refobjid=" + oidView1);
assertFalse(rs.next());

// Check that we have deleted the test_view_view's entry in pg_class.
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test_view_view'");
assertFalse(rs.next());
}
}

@Test
public void testSequenceDeletionWithCascade() throws SQLException {
try (Statement statement = connection.createStatement()) {

// Create a sequence.
statement.execute("CREATE SEQUENCE seq_test START 101");

// Create a table with a column that depends on the sequence.
statement.execute("CREATE TABLE test (a int, b int DEFAULT nextval('seq_test'))");

// Get the OID of the sequence.
ResultSet rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname='seq_test'");
rs.next();
int oidSequence = rs.getInt("oid");

// Get the OID of the table.
rs = statement.executeQuery("SELECT oid FROM pg_class WHERE relname = 'test'");
rs.next();
int oidTable = rs.getInt("oid");

int pgTypeOid = 1247;

// Get the OID of the table column type
rs = statement.executeQuery("SELECT objid FROM pg_depend "
+ " WHERE classid=" + pgTypeOid + " AND refobjid=" + oidSequence);
rs.next();
int oidColType = rs.getInt("objid");

// Test dropping the sequence (without CASCADE). -- expecting an error
runInvalidQuery(statement, "DROP SEQUENCE seq_test");

// Test dropping the sequence (with CASCADE).
statement.execute("DROP SEQUENCE seq_test CASCADE");

// Check that we have deleted the table column to sequence type dependency in pg_depend.
rs = statement.executeQuery("SELECT * FROM pg_depend "
+ "WHERE objid=" + oidColType + " AND refobjid=" + oidSequence);
assertFalse(rs.next());
}
}
}

0 comments on commit 0f8fdd9

Please sign in to comment.