NFC - Short array syntax - auto-convert Civi dir
[civicrm-core.git] / Civi / Test / Schema.php
CommitLineData
728bbd5b
TO
1<?php
2namespace Civi\Test;
3
4use RuntimeException;
5
6/**
7 * Class Schema
8 *
9 * Manage the entire database. This is useful for destroying or loading the schema.
10 */
11class 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);
c64f69d9 28 $result = [];
728bbd5b
TO
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) {
c64f69d9 38 $queries = [
728bbd5b
TO
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;",
c64f69d9 43 ];
728bbd5b
TO
44 }
45 else {
c64f69d9 46 $queries = [
728bbd5b
TO
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;",
c64f69d9 51 ];
728bbd5b
TO
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() {
c64f69d9 62 $queries = [];
728bbd5b
TO
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 /**
38f6295f 87 * @return Schema
728bbd5b
TO
88 */
89 public function truncateAll() {
90 $tables = \Civi\Test::schema()->getTables('BASE TABLE');
91
c64f69d9
CW
92 $truncates = [];
93 $drops = [];
728bbd5b
TO
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}