Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge 4.x => 4.next #746

Merged
merged 8 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,8 +1507,9 @@ protected function getSchemaName(string $tableName): array
protected function getGlobalSchemaName(): string
{
$options = $this->getOptions();
$config = $options['connection']->config() ?? [];

return empty($options['schema']) ? 'public' : $options['schema'];
return empty($config['schema']) ? 'public' : $config['schema'];
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,15 @@ protected function copyAndDropTmpTable(AlterInstructions $instructions, string $
$state['selectColumns']
);

$result = $this->fetchRow('PRAGMA foreign_keys');
$foreignKeysEnabled = $result ? (bool)$result['foreign_keys'] : false;
if ($foreignKeysEnabled) {
$this->execute('PRAGMA foreign_keys = OFF');
}
$this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tableName)));
if ($foreignKeysEnabled) {
$this->execute('PRAGMA foreign_keys = ON');
}
$this->execute(sprintf(
'ALTER TABLE %s RENAME TO %s',
$this->quoteTableName($state['tmpTableName']),
Expand Down
1 change: 1 addition & 0 deletions src/Db/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @internal
* @TODO rename this to `TableMetadata` having two classes with very similar names is confusing for me.
*/
class Table
{
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ public function testQuoteSchemaName()
$this->assertEquals('"schema.schema"', $this->adapter->quoteSchemaName('schema.schema'));
}

public function testGetGlobalSchemaName()
{
$config = ConnectionManager::getConfig('test');
$config['schema'] = 'test_schema';
ConnectionManager::setConfig('test-schema', $config);
// Emulate the results of Util::parseDsn()
$this->config = [
'adapter' => 'postgres',
'connection' => ConnectionManager::get('test-schema'),
'database' => $config['database'],
];

$this->adapter = new PostgresAdapter($this->config, $this->getConsoleIo());

$this->adapter->dropAllSchemas();
$this->adapter->createSchema('test_schema');

$this->adapter->disconnect();

$this->assertEquals('"test_schema"."table"', $this->adapter->quoteTableName('table'));

ConnectionManager::drop('test-schema');
}

public function testQuoteTableName()
{
$this->assertEquals('"public"."table"', $this->adapter->quoteTableName('table'));
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Db/Adapter/SqliteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,32 @@ public function testAddColumnWithComment()
$this->assertMatchesRegularExpression('/\/\* Comments from "column1" \*\//', $sql);
}

public function testAddColumnTableWithConstraint()
{
$this->adapter->execute('PRAGMA foreign_keys = ON');
$roles = new Table('constraint_roles', [], $this->adapter);
$roles->addColumn('name', 'string')
->save();
$users = new Table('constraint_users', [], $this->adapter);
$users->addColumn('username', 'string')
->addColumn('role_id', 'integer', ['null' => false])
->addForeignKey(['role_id'], $roles->getTable(), ['id'])
->save();

$this->adapter->insert($roles->getTable(), ['name' => 'admin']);
$this->adapter->insert($users->getTable(), ['username' => 'test', 'role_id' => 1]);

$updatedRoles = new Table($roles->getName(), [], $this->adapter);
// This should fail, but passes locally :(
$updatedRoles
->addColumn('description', 'string', ['default' => 'short desc'])
->update();
$res = $this->adapter->fetchAll('select * from sqlite_master where type = \'table\'');
$res = $this->adapter->fetchRow('select * from constraint_roles LIMIT 1');
$this->assertArrayHasKey('description', $res, 'Should have new column in output');
$this->assertEquals('short desc', $res['description']);
}

public function testPhinxTypeLiteral()
{
$this->assertEquals(
Expand Down
Loading