importer 3
This commit is contained in:
@@ -436,8 +436,9 @@ final class SchemaManager
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$currentStatement = $trimmed;
|
$normalizedStatement = $this->normalizeImportStatement($trimmed);
|
||||||
$this->pdo->exec($trimmed);
|
$currentStatement = $normalizedStatement;
|
||||||
|
$this->pdo->exec($normalizedStatement);
|
||||||
$executed++;
|
$executed++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,6 +464,74 @@ final class SchemaManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function normalizeImportStatement(string $statement): string
|
||||||
|
{
|
||||||
|
if ($this->driver !== 'pgsql') {
|
||||||
|
return $statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resolvedSetval = $this->normalizePgsqlSetvalStatement($statement);
|
||||||
|
return $resolvedSetval ?? $statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePgsqlSetvalStatement(string $statement): ?string
|
||||||
|
{
|
||||||
|
$pattern = "/^SELECT\\s+setval\\(\\s*'([^']+)'\\s*,\\s*([0-9]+)\\s*,\\s*(true|false)\\s*\\)$/i";
|
||||||
|
if (!preg_match($pattern, trim($statement), $matches)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sequenceReference = $matches[1];
|
||||||
|
$nextValue = $matches[2];
|
||||||
|
$isCalled = strtolower($matches[3]);
|
||||||
|
|
||||||
|
if ($this->pgsqlRelationExists($sequenceReference)) {
|
||||||
|
return $statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sequenceName = $sequenceReference;
|
||||||
|
$schemaName = 'public';
|
||||||
|
if (str_contains($sequenceReference, '.')) {
|
||||||
|
[$schemaName, $sequenceName] = explode('.', $sequenceReference, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$schemaName = trim($schemaName, "\"'");
|
||||||
|
$sequenceName = trim($sequenceName, "\"'");
|
||||||
|
|
||||||
|
if (!preg_match('/^(.*)_([A-Za-z0-9]+)_seq\d*$/', $sequenceName, $parts)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableName = $parts[1];
|
||||||
|
$columnName = $parts[2];
|
||||||
|
$actualSequence = $this->resolvePgsqlSerialSequence($schemaName, $tableName, $columnName);
|
||||||
|
if ($actualSequence === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sprintf("SELECT setval('%s', %s, %s)", $actualSequence, $nextValue, $isCalled);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function pgsqlRelationExists(string $qualifiedName): bool
|
||||||
|
{
|
||||||
|
$statement = $this->pdo->prepare('SELECT to_regclass(:name) IS NOT NULL');
|
||||||
|
$statement->execute(['name' => $qualifiedName]);
|
||||||
|
return (bool) $statement->fetchColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolvePgsqlSerialSequence(string $schemaName, string $tableName, string $columnName): ?string
|
||||||
|
{
|
||||||
|
$qualifiedTable = sprintf('"%s"."%s"', str_replace('"', '""', $schemaName), str_replace('"', '""', $tableName));
|
||||||
|
$statement = $this->pdo->prepare('SELECT pg_get_serial_sequence(:table_name, :column_name)');
|
||||||
|
$statement->execute([
|
||||||
|
'table_name' => $qualifiedTable,
|
||||||
|
'column_name' => $columnName,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $statement->fetchColumn();
|
||||||
|
return is_string($result) && $result !== '' ? $result : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return list<string>
|
* @return list<string>
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user