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 foreach ($tables as $table) {
30 $result[] = $table['table_name'];
35 public function setStrict($checks) {
36 $dbName = \Civi\Test
::dsn('database');
40 "SET global innodb_flush_log_at_trx_commit = 1;",
41 "SET SQL_MODE='STRICT_ALL_TABLES';",
42 "SET foreign_key_checks = 1;",
48 "SET foreign_key_checks = 0",
49 "SET SQL_MODE='STRICT_ALL_TABLES';",
50 "SET global innodb_flush_log_at_trx_commit = 2;",
53 foreach ($queries as $query) {
54 if (\Civi\Test
::execute($query) === FALSE) {
55 throw new RuntimeException("Query failed: $query");
61 public function dropAll() {
63 foreach ($this->getTables('VIEW') as $table) {
64 if (preg_match('/^(civicrm_|log_)/', $table)) {
65 $queries[] = "DROP VIEW $table";
69 foreach ($this->getTables('BASE TABLE') as $table) {
70 if (preg_match('/^(civicrm_|log_)/', $table)) {
71 $queries[] = "DROP TABLE $table";
75 $this->setStrict(FALSE);
76 foreach ($queries as $query) {
77 if (\Civi\Test
::execute($query) === FALSE) {
78 throw new RuntimeException("dropSchema: Query failed: $query");
81 $this->setStrict(TRUE);
89 public function truncateAll() {
90 $tables = \Civi\Test
::schema()->getTables('BASE TABLE');
94 foreach ($tables as $table) {
96 if (substr($table, 0, 4) == 'log_') {
100 // don't change list of installed extensions
101 if ($table == 'civicrm_extension') {
105 if (substr($table, 0, 14) == 'civicrm_value_') {
106 $drops[] = 'DROP TABLE ' . $table . ';';
108 elseif (substr($table, 0, 9) == 'civitest_') {
112 $truncates[] = 'TRUNCATE ' . $table . ';';
116 \Civi\Test
::schema()->setStrict(FALSE);
117 $queries = array_merge($truncates, $drops);
118 foreach ($queries as $query) {
119 if (\Civi\Test
::execute($query) === FALSE) {
120 throw new RuntimeException("Query failed: $query");
123 \Civi\Test
::schema()->setStrict(TRUE);