Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
Db | n/a |
0 / 0 |
n/a |
0 / 0 |
22 | n/a |
0 / 0 |
|||
startExecuteSqlFile | n/a |
0 / 0 |
n/a |
0 / 0 |
8 | |||||
executeSql | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
startUsingDatabase | n/a |
0 / 0 |
n/a |
0 / 0 |
4 | |||||
startTestDataImport | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
startConfigDataImport | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
startMigrations | n/a |
0 / 0 |
n/a |
0 / 0 |
5 | |||||
executeTestData | n/a |
0 / 0 |
n/a |
0 / 0 |
2 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Cli; |
4 | |
5 | /** |
6 | * @codeCoverageIgnore |
7 | * @SuppressWarnings(Short) |
8 | */ |
9 | class Db |
10 | { |
11 | public static $baseDSN = ''; |
12 | |
13 | public static function startExecuteSqlFile($file, $dbname = null, $verbose = true) |
14 | { |
15 | $pdo = self::startUsingDatabase($dbname, $verbose); |
16 | $startTime = microtime(true); |
17 | $sqlFile = gzopen($file, 'r'); |
18 | if ($verbose) { |
19 | echo "Importing " . basename($file) . "\n"; |
20 | } |
21 | $query = ''; |
22 | while ($line = gzgets($sqlFile)) { |
23 | $query .= $line; |
24 | if (preg_match('/;\s*$/', $line)) { |
25 | try { |
26 | $pdo->exec($query); |
27 | if ($verbose) { |
28 | echo "."; |
29 | } |
30 | //echo "Successful:\n$query\n"; |
31 | $query = ''; |
32 | } catch (\Exception $exception) { |
33 | if ($verbose) { |
34 | echo "Offending query: \n$query\n"; |
35 | } |
36 | throw $exception; |
37 | } |
38 | } |
39 | } |
40 | gzclose($sqlFile); |
41 | $time = round(microtime(true) - $startTime, 3); |
42 | if ($verbose) { |
43 | echo "\nTook $time seconds\n"; |
44 | } |
45 | } |
46 | |
47 | public static function executeSql($query, $dbname = null) |
48 | { |
49 | $pdo = self::startUsingDatabase($dbname, false); |
50 | $pdo->exec($query); |
51 | } |
52 | |
53 | public static function startUsingDatabase($dbname = null, $verbose = true): \BO\Zmsdb\Connection\Pdo |
54 | { |
55 | if (!self::$baseDSN) { |
56 | self::$baseDSN = \BO\Zmsdb\Connection\Select::$writeSourceName; |
57 | } |
58 | \BO\Zmsdb\Connection\Select::closeWriteConnection(); |
59 | if ($dbname === null) { |
60 | $dbname =& \BO\Zmsdb\Connection\Select::$dbname_zms; |
61 | \BO\Zmsdb\Connection\Select::$writeSourceName = self::$baseDSN; |
62 | } else { |
63 | $dbname_zms =& \BO\Zmsdb\Connection\Select::$dbname_zms; |
64 | \BO\Zmsdb\Connection\Select::$writeSourceName = |
65 | preg_replace("#dbname=$dbname_zms.*?;#", "dbname=$dbname;", self::$baseDSN); |
66 | } |
67 | |
68 | if ($verbose) { |
69 | error_log("Use Connection " . \BO\Zmsdb\Connection\Select::$writeSourceName); |
70 | } |
71 | |
72 | $pdo = \BO\Zmsdb\Connection\Select::getWriteConnection(); |
73 | return $pdo; |
74 | } |
75 | |
76 | public static function startTestDataImport($fixtures, $filename = 'mysql_zmsbo.sql.gz') |
77 | { |
78 | $dbname_zms =& \BO\Zmsdb\Connection\Select::$dbname_zms; |
79 | |
80 | $pdo = self::startUsingDatabase('information_schema'); |
81 | $pdo->exec("DROP DATABASE IF EXISTS `$dbname_zms`;"); |
82 | $pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname_zms`;"); |
83 | |
84 | self::startExecuteSqlFile($fixtures . '/' . $filename); |
85 | } |
86 | |
87 | public static function startConfigDataImport() |
88 | { |
89 | $defaults = new \BO\Zmsentities\Config(); |
90 | (new \BO\Zmsdb\Config())->updateEntity($defaults); |
91 | } |
92 | |
93 | public static function startMigrations($migrationList, $commit = true) |
94 | { |
95 | if (!is_array($migrationList)) { |
96 | $migrationList = glob($migrationList . '/*.sql'); |
97 | } |
98 | sort($migrationList); |
99 | $pdo = self::startUsingDatabase(); |
100 | $migrationsDoneList = $pdo->fetchPairs('SELECT filename, changeTimestamp FROM migrations'); |
101 | $addedMigrations = 0; |
102 | foreach ($migrationList as $migrationFile) { |
103 | $migrationName = basename($migrationFile); |
104 | if (!array_key_exists($migrationName, $migrationsDoneList)) { |
105 | $addedMigrations++; |
106 | if (!$commit) { |
107 | echo "$addedMigrations. Add migration $migrationName\n"; |
108 | } else { |
109 | self::startExecuteSqlFile($migrationFile); |
110 | $pdo->prepare('INSERT INTO `migrations` SET `filename` = :filename') |
111 | ->execute(['filename' => $migrationName]); |
112 | } |
113 | } |
114 | } |
115 | echo "\nFound " . count($migrationsDoneList) . " completed migrations and added $addedMigrations migrations.\n"; |
116 | return $addedMigrations; |
117 | } |
118 | |
119 | public static function executeTestData(string $testName, string $step) |
120 | { |
121 | $fixtures = realpath(__DIR__ . '/../../../tests/Zmsdb/fixtures/'); |
122 | $sqlFile = $fixtures . '/' . $testName . '/' . $step . '.sql'; |
123 | |
124 | if (! file_exists($sqlFile)) { |
125 | return; |
126 | } |
127 | |
128 | self::startExecuteSqlFile( |
129 | $sqlFile, |
130 | null, |
131 | false |
132 | ); |
133 | } |
134 | } |