diff --git a/src/Import/Import.php b/src/Import/Import.php index 4c4a4a9ce3..65044844e1 100644 --- a/src/Import/Import.php +++ b/src/Import/Import.php @@ -775,7 +775,7 @@ final class Import $size = 10; } - $tempSQLStr .= Util::backquote($column) . ' ' . match ($analyses[$i][$j]->type) { + $colType = match ($analyses[$i][$j]->type) { ColumnType::None => 'NULL', ColumnType::Varchar => 'varchar', ColumnType::Int => 'int', @@ -783,7 +783,17 @@ final class Import ColumnType::BigInt => 'bigint', ColumnType::Geometry => 'geometry', }; - if ($analyses[$i][$j]->type !== ColumnType::Geometry) { + + // Use TEXT instead of VARCHAR when the length exceeds + // the maximum allowed for VARCHAR. The limit depends on + // the charset (e.g. 16383 for utf8mb4, 65535 for latin1). + // We use the most restrictive limit (utf8mb4) to be safe. + if ($colType === 'varchar' && $size > 16383) { + $colType = 'text'; + } + + $tempSQLStr .= Util::backquote($column) . ' ' . $colType; + if ($colType !== 'text' && $analyses[$i][$j]->type !== ColumnType::Geometry) { $tempSQLStr .= '(' . $size . ')'; } diff --git a/src/Plugins/Export/ExportSql.php b/src/Plugins/Export/ExportSql.php index 894da88b84..5f7d68b5b4 100644 --- a/src/Plugins/Export/ExportSql.php +++ b/src/Plugins/Export/ExportSql.php @@ -853,6 +853,13 @@ class ExportSql extends ExportPlugin return; } + // Skip CREATE DATABASE when only exporting data + if (! $this->includeStructure()) { + $this->exportUseStatement($dbAlias, $this->compatibility); + + return; + } + $createQuery = 'CREATE DATABASE IF NOT EXISTS ' . Util::backquoteCompat($dbAlias, $this->compatibility, $this->useSqlBackquotes); $collation = $this->dbi->getDbCollation($db); diff --git a/tests/unit/Export/ExportTest.php b/tests/unit/Export/ExportTest.php index 7dc8dde66a..91cbe43378 100644 --- a/tests/unit/Export/ExportTest.php +++ b/tests/unit/Export/ExportTest.php @@ -230,7 +230,6 @@ class ExportTest extends AbstractTestCase ); $expected = <<<'SQL' - CREATE DATABASE IF NOT EXISTS test_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; USE test_db; INSERT INTO test_table (id, name, datetimefield) VALUES diff --git a/tests/unit/Plugins/Export/ExportSqlTest.php b/tests/unit/Plugins/Export/ExportSqlTest.php index 2273d40591..490b36c2b5 100644 --- a/tests/unit/Plugins/Export/ExportSqlTest.php +++ b/tests/unit/Plugins/Export/ExportSqlTest.php @@ -512,6 +512,33 @@ final class ExportSqlTest extends AbstractTestCase self::assertStringContainsString('USE db;', $result); } + public function testExportDBCreateDataOnly(): void + { + $request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/') + ->withParsedBody([ + 'sql_backquotes' => 'true', + 'sql_compatibility' => 'NONE', + 'sql_structure_or_data' => 'data', + ]); + + $dbi = $this->getMockBuilder(DatabaseInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $dbi->expects(self::any())->method('quoteString') + ->willReturnCallback(static fn (string $string): string => "'" . $string . "'"); + + $exportSql = $this->getExportSql($dbi); + $exportSql->setExportOptions($request, new Export()); + + ob_start(); + $exportSql->exportDBCreate('db'); + $result = ob_get_clean(); + + self::assertIsString($result); + self::assertStringNotContainsString('CREATE DATABASE', $result); + self::assertStringContainsString('USE `db`;', $result); + } + public function testExportDBHeader(): void { $request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')