Skip to content

Commit

Permalink
Export views after indexes
Browse files Browse the repository at this point in the history
When a view uses USE INDEX syntax import failed because of
the missing index.

Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
  • Loading branch information
MoonE committed Apr 27, 2024
1 parent 0c1bd41 commit fb91de3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
15 changes: 14 additions & 1 deletion libraries/classes/Plugins/Export/ExportSql.php
Expand Up @@ -982,6 +982,12 @@ public function exportDBFooter($db): bool
unset($GLOBALS['sql_auto_increments']);
}

//add views to the sql dump file
if (isset($GLOBALS['sql_views'])) {
$result = $this->export->outputHandler($GLOBALS['sql_views']);
unset($GLOBALS['sql_views']);
}

//add constraints to the sql dump file
if (isset($GLOBALS['sql_constraints'])) {
$result = $this->export->outputHandler($GLOBALS['sql_constraints']);
Expand Down Expand Up @@ -2070,7 +2076,7 @@ public function exportStructure(
$dates = false,
array $aliases = []
): bool {
global $dbi;
global $dbi, $sql_views;

$dbAlias = $db;
$tableAlias = $table;
Expand Down Expand Up @@ -2177,6 +2183,13 @@ public function exportStructure(
$dump .= $this->getTableDefForView($db, $table, $crlf, true, $aliases);
}

if (empty($GLOBALS['sql_views_as_tables'])) {
// Save views, to be inserted after indexes
// in case the view uses USE INDEX syntax
$sql_views = ($sql_views ?? '') . $dump;
$dump = '';
}

break;
case 'stand_in':
$dump .= $this->exportComment(
Expand Down
9 changes: 6 additions & 3 deletions psalm-baseline.xml
Expand Up @@ -9932,10 +9932,11 @@
<code>$GLOBALS['asfile']</code>
<code>$GLOBALS['sql_if_not_exists']</code>
</InvalidArgument>
<MixedArgument occurrences="51">
<MixedArgument occurrences="52">
<code>$GLOBALS['sql_auto_increments']</code>
<code>$GLOBALS['sql_header_comment']</code>
<code>$GLOBALS['sql_indexes']</code>
<code>$GLOBALS['sql_views']</code>
<code>$GLOBALS['table_data']</code>
<code>$colAlias</code>
<code>$colAlias</code>
Expand Down Expand Up @@ -10046,7 +10047,7 @@
<code>$val</code>
<code>$values[$val]</code>
</MixedAssignment>
<MixedOperand occurrences="40">
<MixedOperand occurrences="41">
<code>$column['Collation']</code>
<code>$column['Type']</code>
<code>$crlf</code>
Expand Down Expand Up @@ -10084,6 +10085,7 @@
<code>$crlf</code>
<code>$crlf</code>
<code>$definition['Type']</code>
<code>$sql_views ?? ''</code>
<code>$statement-&gt;entityOptions-&gt;has('AUTO_INCREMENT')</code>
<code>$tmpUniqueCondition</code>
<code>$trigger['drop']</code>
Expand Down Expand Up @@ -16060,8 +16062,9 @@
</MixedAssignment>
</file>
<file src="test/classes/Plugins/Export/ExportSqlTest.php">
<InvalidArrayOffset occurrences="1">
<InvalidArrayOffset occurrences="2">
<code>$GLOBALS['sql_constraints']</code>
<code>$GLOBALS['sql_views']</code>
</InvalidArrayOffset>
<MixedArgument occurrences="6">
<code>$GLOBALS['sql_constraints']</code>
Expand Down
9 changes: 5 additions & 4 deletions test/classes/Plugins/Export/ExportSqlTest.php
Expand Up @@ -1084,10 +1084,11 @@ public function testExportStructure(): void
);
$result = ob_get_clean();

$this->assertIsString($result);
$this->assertStringContainsString('-- Structure for view test_table', $result);
$this->assertStringContainsString('DROP TABLE IF EXISTS `test_table`;', $result);
$this->assertStringContainsString('CREATE TABLE `test_table`', $result);
$this->assertEquals('', $result);
$this->assertIsString($GLOBALS['sql_views']);
$this->assertStringContainsString('-- Structure for view test_table', $GLOBALS['sql_views']);
$this->assertStringContainsString('DROP TABLE IF EXISTS `test_table`;', $GLOBALS['sql_views']);
$this->assertStringContainsString('CREATE TABLE `test_table`', $GLOBALS['sql_views']);

// case 4
$GLOBALS['sql_views_as_tables'] = true;
Expand Down

0 comments on commit fb91de3

Please sign in to comment.