9 * Manage the entire database. This is useful for destroying or loading the schema.
15 * 'BASE TABLE' or 'VIEW'.
18 public function getTables($type) {
19 $pdo = \Civi\Test
::pdo();
20 // only consider real tables and not views
22 "SELECT table_name FROM INFORMATION_SCHEMA.TABLES
23 WHERE TABLE_SCHEMA = %s AND TABLE_TYPE = %s",
24 $pdo->quote(\Civi\Test
::dsn('database')),
27 $tables = $pdo->query($query);
29 if (!empty($tables)) {
30 foreach ($tables as $table) {
31 $result[] = $table['TABLE_NAME'] ??
$table['table_name'];
37 public function setStrict($checks) {
38 $dbName = \Civi\Test
::dsn('database');
42 "SET global innodb_flush_log_at_trx_commit = 1;",
43 "SET SQL_MODE='STRICT_ALL_TABLES';",
44 "SET foreign_key_checks = 1;",
50 "SET foreign_key_checks = 0",
51 "SET SQL_MODE='STRICT_ALL_TABLES';",
52 "SET global innodb_flush_log_at_trx_commit = 2;",
55 foreach ($queries as $query) {
56 if (\Civi\Test
::execute($query) === FALSE) {
57 throw new RuntimeException("Query failed: $query");
63 public function dropAll() {
65 foreach ($this->getTables('VIEW') as $table) {
66 if (preg_match('/^(civicrm_|log_)/', $table)) {
67 $queries[] = "DROP VIEW $table";
71 foreach ($this->getTables('BASE TABLE') as $table) {
72 if (preg_match('/^(civicrm_|log_)/', $table)) {
73 $queries[] = "DROP TABLE $table";
77 $this->setStrict(FALSE);
78 foreach ($queries as $query) {
79 if (\Civi\Test
::execute($query) === FALSE) {
80 throw new RuntimeException("dropSchema: Query failed: $query");
83 $this->setStrict(TRUE);
91 public function truncateAll() {
92 $tables = \Civi\Test
::schema()->getTables('BASE TABLE');
96 foreach ($tables as $table) {
98 if (substr($table, 0, 4) == 'log_') {
102 // don't change list of installed extensions
103 if ($table == 'civicrm_extension') {
107 if (substr($table, 0, 14) == 'civicrm_value_') {
108 $drops[] = 'DROP TABLE ' . $table . ';';
110 elseif (substr($table, 0, 9) == 'civitest_') {
114 $truncates[] = 'TRUNCATE ' . $table . ';';
118 \Civi\Test
::schema()->setStrict(FALSE);
119 $queries = array_merge($truncates, $drops);
120 foreach ($queries as $query) {
121 if (\Civi\Test
::execute($query) === FALSE) {
122 throw new RuntimeException("Query failed: $query");
125 \Civi\Test
::schema()->setStrict(TRUE);