| 1 | <?php |
| 2 | namespace Civi\Test; |
| 3 | |
| 4 | use RuntimeException; |
| 5 | |
| 6 | /** |
| 7 | * Class Schema |
| 8 | * |
| 9 | * Manage the entire database. This is useful for destroying or loading the schema. |
| 10 | */ |
| 11 | class Schema { |
| 12 | |
| 13 | /** |
| 14 | * @param string $type |
| 15 | * 'BASE TABLE' or 'VIEW'. |
| 16 | * @return array |
| 17 | */ |
| 18 | public function getTables($type) { |
| 19 | $pdo = \Civi\Test::pdo(); |
| 20 | // only consider real tables and not views |
| 21 | $query = sprintf( |
| 22 | "SELECT table_name FROM INFORMATION_SCHEMA.TABLES |
| 23 | WHERE TABLE_SCHEMA = %s AND TABLE_TYPE = %s", |
| 24 | $pdo->quote(\Civi\Test::dsn('database')), |
| 25 | $pdo->quote($type) |
| 26 | ); |
| 27 | $tables = $pdo->query($query); |
| 28 | $result = []; |
| 29 | if (!empty($tables)) { |
| 30 | foreach ($tables as $table) { |
| 31 | $result[] = $table['TABLE_NAME'] ?? $table['table_name']; |
| 32 | } |
| 33 | } |
| 34 | return $result; |
| 35 | } |
| 36 | |
| 37 | public function setStrict($checks) { |
| 38 | $dbName = \Civi\Test::dsn('database'); |
| 39 | if ($checks) { |
| 40 | $queries = [ |
| 41 | "USE {$dbName};", |
| 42 | "SET global innodb_flush_log_at_trx_commit = 1;", |
| 43 | "SET SQL_MODE='STRICT_ALL_TABLES';", |
| 44 | "SET foreign_key_checks = 1;", |
| 45 | ]; |
| 46 | } |
| 47 | else { |
| 48 | $queries = [ |
| 49 | "USE {$dbName};", |
| 50 | "SET foreign_key_checks = 0", |
| 51 | "SET SQL_MODE='STRICT_ALL_TABLES';", |
| 52 | "SET global innodb_flush_log_at_trx_commit = 2;", |
| 53 | ]; |
| 54 | } |
| 55 | foreach ($queries as $query) { |
| 56 | if (\Civi\Test::execute($query) === FALSE) { |
| 57 | throw new RuntimeException("Query failed: $query"); |
| 58 | } |
| 59 | } |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | public function dropAll() { |
| 64 | $queries = []; |
| 65 | foreach ($this->getTables('VIEW') as $table) { |
| 66 | if (preg_match('/^(civicrm_|log_)/', $table)) { |
| 67 | $queries[] = "DROP VIEW $table"; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | foreach ($this->getTables('BASE TABLE') as $table) { |
| 72 | if (preg_match('/^(civicrm_|log_)/', $table)) { |
| 73 | $queries[] = "DROP TABLE $table"; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $this->setStrict(FALSE); |
| 78 | foreach ($queries as $query) { |
| 79 | if (\Civi\Test::execute($query) === FALSE) { |
| 80 | throw new RuntimeException("dropSchema: Query failed: $query"); |
| 81 | } |
| 82 | } |
| 83 | $this->setStrict(TRUE); |
| 84 | |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return Schema |
| 90 | */ |
| 91 | public function truncateAll() { |
| 92 | $tables = \Civi\Test::schema()->getTables('BASE TABLE'); |
| 93 | |
| 94 | $truncates = []; |
| 95 | $drops = []; |
| 96 | foreach ($tables as $table) { |
| 97 | // skip log tables |
| 98 | if (substr($table, 0, 4) == 'log_') { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // don't change list of installed extensions |
| 103 | if ($table == 'civicrm_extension') { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if (substr($table, 0, 14) == 'civicrm_value_') { |
| 108 | $drops[] = 'DROP TABLE ' . $table . ';'; |
| 109 | } |
| 110 | elseif (substr($table, 0, 9) == 'civitest_') { |
| 111 | // ignore |
| 112 | } |
| 113 | else { |
| 114 | $truncates[] = 'TRUNCATE ' . $table . ';'; |
| 115 | } |
| 116 | } |
| 117 | |
| 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"); |
| 123 | } |
| 124 | } |
| 125 | \Civi\Test::schema()->setStrict(TRUE); |
| 126 | |
| 127 | return $this; |
| 128 | } |
| 129 | |
| 130 | } |