Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 23, 2024
1 parent 989e432 commit 57b3823
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/Persistence/Sql/Sqlite/debug-attach/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,12 @@ public function getAlterTableSQL(TableDiff $diff)
$sql = [];
$tableSql = [];
if (! $this->onSchemaAlterTable($diff, $tableSql)) {
$dataTable = new Table('__temp__' . $table->getName());
if (str_contains($table->getName(), '.')) {
$nameArr = explode('.', $table->getName(), 2);
$dataTable = new Table(/* $nameArr[0] . '.' . */ '__temp__' . $nameArr[1]);
} else {
$dataTable = new Table('__temp__' . $table->getName());
}

$newTable = new Table(
$table->getQuotedName($this),
Expand Down
77 changes: 55 additions & 22 deletions src/Persistence/Sql/Sqlite/debug-attach/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,31 @@ protected function _getPortableTableDefinition($table)
return $table['table_name'];
}

/**
* @return array{?string, string}
*/
public static function extractSchemaFromTableName(string $tableName, $fallbackDatabaseName = null): array
{
if (!str_contains($tableName, '.')) {
return [$fallbackDatabaseName, $tableName];
}

return explode('.', $tableName, 2);
}

/**
* @return array{?string, string}
*/
public static function extractSchemaAsPrefixFromTableName(string $tableName, $fallbackDatabaseName = null): array
{
[$schemaName, $tableName] = self::extractSchemaFromTableName($tableName);

return [
($schemaName ?? $fallbackDatabaseName) === null ? null : ($schemaName ?? $fallbackDatabaseName) . '.',
$tableName,
];
}

/**
* {@inheritDoc}
*
Expand All @@ -231,8 +256,10 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null
{
$indexBuffer = [];

[$schemaPrefix, $tableName] = self::extractSchemaAsPrefixFromTableName($tableName);

// fetch primary
$indexArray = $this->_conn->fetchAllAssociative('SELECT * FROM PRAGMA_TABLE_INFO (?)', [$tableName]);
$indexArray = $this->_conn->fetchAllAssociative('SELECT * FROM ' . $schemaPrefix . 'PRAGMA_TABLE_INFO (?)', [$tableName]);

usort(
$indexArray,
Expand Down Expand Up @@ -550,19 +577,21 @@ private function parseColumnCommentFromSQL(string $column, string $sql): ?string
/** @throws Exception */
private function getCreateTableSQL(string $table): string
{
[$schema, $table] = self::extractSchemaAsPrefixFromTableName($table);

$sql = $this->_conn->fetchOne(
<<<'SQL'
'
SELECT sql
FROM (
SELECT *
FROM sqlite_master
FROM ' . $schema . 'sqlite_master
UNION ALL
SELECT *
FROM sqlite_temp_master
FROM temp.sqlite_master
)
WHERE type = 'table'
WHERE type = \'table\'
AND name = ?
SQL
'
,
[$table],
);
Expand Down Expand Up @@ -663,31 +692,33 @@ public function getSchemaSearchPaths()

protected function selectTableNames(string $databaseName): Result
{
$sql = <<<'SQL'
$sql = '
SELECT name AS table_name
FROM sqlite_master
WHERE type = 'table'
AND name != 'sqlite_sequence'
AND name != 'geometry_columns'
AND name != 'spatial_ref_sys'
FROM ' . $databaseName . '.sqlite_master
WHERE type = \'table\'
AND name != \'sqlite_sequence\'
AND name != \'geometry_columns\'
AND name != \'spatial_ref_sys\'
UNION ALL
SELECT name
FROM sqlite_temp_master
WHERE type = 'table'
FROM temp.sqlite_master
WHERE type = \'table\'
ORDER BY name
SQL;
';

return $this->_conn->executeQuery($sql);
}

protected function selectTableColumns(string $databaseName, ?string $tableName = null): Result
{
$sql = <<<'SQL'
[$databaseName, $tableName] = self::extractSchemaAsPrefixFromTableName($tableName);

$sql = '
SELECT t.name AS table_name,
c.*
FROM sqlite_master t
FROM ' . $databaseName . 'sqlite_master t
JOIN pragma_table_info(t.name) c
SQL;
';

$conditions = [
"t.type = 'table'",
Expand All @@ -707,12 +738,14 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =

protected function selectIndexColumns(string $databaseName, ?string $tableName = null): Result
{
$sql = <<<'SQL'
[$databaseName, $tableName] = self::extractSchemaAsPrefixFromTableName($tableName, $databaseName);

$sql = '
SELECT t.name AS table_name,
i.*
FROM sqlite_master t
JOIN pragma_index_list(t.name) i
SQL;
FROM ' . $databaseName . 'sqlite_master t
JOIN ' . $databaseName . 'pragma_index_list(t.name) i
';

$conditions = [
"t.type = 'table'",
Expand Down
8 changes: 7 additions & 1 deletion tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,13 @@ public function testTableWithSchema(): void
$this->createMigrator($user)->create();
$this->createMigrator($doc)->create();
$this->debug = true;
$this->createMigrator()->createForeignKey($doc->getReference('user_id'));
if (!$this->getDatabasePlatform() instanceof SQLitePlatform) {
$this->createMigrator()->createForeignKey($doc->getReference('user_id'));
}
// TODO once https://sqlite.org/forum/info/f8f0c1a1069bb40a is resolved
if (!$this->getDatabasePlatform() instanceof SQLitePlatform) {
$this->createMigrator()->createIndex([$doc->getField('user_id')], true);
}

$user->createEntity()
->set('name', 'Sarah')
Expand Down

0 comments on commit 57b3823

Please sign in to comment.