3 namespace Drupal\KernelTests\Core\Database;
6 * Tests delete and truncate queries.
8 * The DELETE tests are not as extensive, as all of the interesting code for
9 * DELETE queries is in the conditional which is identical to the UPDATE and
10 * SELECT conditional handling.
12 * The TRUNCATE tests are not extensive either, because the behavior of
13 * TRUNCATE queries is not consistent across database engines. We only test
14 * that a TRUNCATE query actually deletes all rows from the target table.
18 class DeleteTruncateTest extends DatabaseTestBase {
21 * Confirms that we can use a subselect in a delete successfully.
23 public function testSubselectDelete() {
24 $num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
25 $pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")->fetchField();
27 $subquery = db_select('test', 't')
29 ->condition('t.id', [$pid_to_delete], 'IN');
30 $delete = db_delete('test_task')
31 ->condition('task', 'sleep')
32 ->condition('pid', $subquery, 'IN');
34 $num_deleted = $delete->execute();
35 $this->assertEqual($num_deleted, 1, 'Deleted 1 record.');
37 $num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
38 $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
42 * Confirms that we can delete a single record successfully.
44 public function testSimpleDelete() {
45 $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
47 $num_deleted = db_delete('test')
50 $this->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
52 $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
53 $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
57 * Confirms that we can truncate a whole table successfully.
59 public function testTruncate() {
60 $num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
61 $this->assertTrue($num_records_before > 0, 'The table is not empty.');
63 db_truncate('test')->execute();
65 $num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
66 $this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
70 * Confirms that we can truncate a whole table while in transaction.
72 public function testTruncateInTransaction() {
73 // This test won't work right if transactions are not supported.
74 if (!$this->connection->supportsTransactions()) {
75 $this->markTestSkipped('The database driver does not support transactions.');
78 $num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
79 $this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
81 $transaction = $this->connection->startTransaction('test_truncate_in_transaction');
82 $this->connection->insert('test')
86 'job' => 'Great singer',
89 $num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
90 $this->assertEquals($num_records_before + 1, $num_records_after_insert);
92 $this->connection->truncate('test')->execute();
94 // Checks that there are no records left in the table, and transaction is
96 $this->assertTrue($this->connection->inTransaction());
97 $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
98 $this->assertEquals(0, $num_records_after);
100 // Close the transaction, and check that there are still no records in the
103 $this->assertFalse($this->connection->inTransaction());
104 $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
105 $this->assertEquals(0, $num_records_after);
109 * Confirms that transaction rollback voids a truncate operation.
111 public function testTruncateTransactionRollback() {
112 // This test won't work right if transactions are not supported.
113 if (!$this->connection->supportsTransactions()) {
114 $this->markTestSkipped('The database driver does not support transactions.');
117 $num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
118 $this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
120 $transaction = $this->connection->startTransaction('test_truncate_in_transaction');
121 $this->connection->insert('test')
125 'job' => 'Great singer',
128 $num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
129 $this->assertEquals($num_records_before + 1, $num_records_after_insert);
131 $this->connection->truncate('test')->execute();
133 // Checks that there are no records left in the table, and transaction is
135 $this->assertTrue($this->connection->inTransaction());
136 $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
137 $this->assertEquals(0, $num_records_after);
139 // Roll back the transaction, and check that we are back to status before
140 // insert and truncate.
141 $this->connection->rollBack();
142 $this->assertFalse($this->connection->inTransaction());
143 $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
144 $this->assertEquals($num_records_before, $num_records_after);
148 * Confirms that we can delete a single special column name record successfully.
150 public function testSpecialColumnDelete() {
151 $num_records_before = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
153 $num_deleted = db_delete('test_special_columns')
156 $this->assertIdentical($num_deleted, 1, 'Deleted 1 special column record.');
158 $num_records_after = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
159 $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');