Merge pull request #5903 from scardinius/master
[civicrm-core.git] / Civi / Test / Schema.php
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 = array();
29 foreach ($tables as $table) {
30 $result[] = $table['table_name'];
31 }
32 return $result;
33 }
34
35 public function setStrict($checks) {
36 $dbName = \Civi\Test::dsn('database');
37 if ($checks) {
38 $queries = array(
39 "USE {$dbName};",
40 "SET global innodb_flush_log_at_trx_commit = 1;",
41 "SET SQL_MODE='STRICT_ALL_TABLES';",
42 "SET foreign_key_checks = 1;",
43 );
44 }
45 else {
46 $queries = array(
47 "USE {$dbName};",
48 "SET foreign_key_checks = 0",
49 "SET SQL_MODE='STRICT_ALL_TABLES';",
50 "SET global innodb_flush_log_at_trx_commit = 2;",
51 );
52 }
53 foreach ($queries as $query) {
54 if (\Civi\Test::execute($query) === FALSE) {
55 throw new RuntimeException("Query failed: $query");
56 }
57 }
58 return $this;
59 }
60
61 public function dropAll() {
62 $queries = array();
63 foreach ($this->getTables('VIEW') as $table) {
64 if (preg_match('/^(civicrm_|log_)/', $table)) {
65 $queries[] = "DROP VIEW $table";
66 }
67 }
68
69 foreach ($this->getTables('BASE TABLE') as $table) {
70 if (preg_match('/^(civicrm_|log_)/', $table)) {
71 $queries[] = "DROP TABLE $table";
72 }
73 }
74
75 $this->setStrict(FALSE);
76 foreach ($queries as $query) {
77 if (\Civi\Test::execute($query) === FALSE) {
78 throw new RuntimeException("dropSchema: Query failed: $query");
79 }
80 }
81 $this->setStrict(TRUE);
82
83 return $this;
84 }
85
86 /**
87 * @return array
88 */
89 public function truncateAll() {
90 $tables = \Civi\Test::schema()->getTables('BASE TABLE');
91
92 $truncates = array();
93 $drops = array();
94 foreach ($tables as $table) {
95 // skip log tables
96 if (substr($table, 0, 4) == 'log_') {
97 continue;
98 }
99
100 // don't change list of installed extensions
101 if ($table == 'civicrm_extension') {
102 continue;
103 }
104
105 if (substr($table, 0, 14) == 'civicrm_value_') {
106 $drops[] = 'DROP TABLE ' . $table . ';';
107 }
108 elseif (substr($table, 0, 9) == 'civitest_') {
109 // ignore
110 }
111 else {
112 $truncates[] = 'TRUNCATE ' . $table . ';';
113 }
114 }
115
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");
121 }
122 }
123 \Civi\Test::schema()->setStrict(TRUE);
124
125 return $this;
126 }
127
128 }