Skip to content

Commit

Permalink
Move schema to class property for PostgresAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Sep 7, 2024
1 parent a093a96 commit f5b2736
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
20 changes: 20 additions & 0 deletions docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ Phinx currently supports the following database adapters natively:
* `SQLite <https://www.sqlite.org/>`_: specify the ``sqlite`` adapter.
* `SQL Server <https://www.microsoft.com/sqlserver>`_: specify the ``sqlsrv`` adapter.

The following settings are available for the adapters:

adapter
The name of the adapter to use, e.g. ``pgsql``.
host
The database server's hostname (or IP address).
port
The database server's TCP port number.
user
The username for the database.
pass
The password for the database.
name
The name of the database for this environment. For SQLite, it's recommended to use an absolute path,
without the file extension.
suffix
The suffix to use for the SQLite database file. Defaults to ``.sqlite3``.
schema
For PostgreSQL, allows specifying the schema to use for the database. Defaults to ``public``.

For each adapter, you may configure the behavior of the underlying PDO object by setting in your
config object the lowercase version of the constant name. This works for both PDO options
(e.g. ``\PDO::ATTR_CASE`` would be ``attr_case``) and adapter specific options (e.g. for MySQL
Expand Down
40 changes: 24 additions & 16 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class PostgresAdapter extends PdoAdapter
*/
protected bool $useIdentity;

/**
* @var string
*/
protected string $schema = 'public';

/**
* {@inheritDoc}
*/
public function setOptions(array $options): AdapterInterface
{
parent::setOptions($options);

if (!empty($options['schema'])) {
$this->schema = $options['schema'];
}

return $this;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -1378,8 +1397,8 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
public function createSchemaTable(): void
{
// Create the public/custom schema if it doesn't already exist
if ($this->hasSchema($this->getGlobalSchemaName()) === false) {
$this->createSchema($this->getGlobalSchemaName());
if ($this->hasSchema($this->schema) === false) {
$this->createSchema($this->schema);
}

$this->setSearchPath();
Expand Down Expand Up @@ -1528,7 +1547,7 @@ protected function isArrayType(string|Literal $columnType): bool
*/
protected function getSchemaName(string $tableName): array
{
$schema = $this->getGlobalSchemaName();
$schema = $this->schema;
$table = $tableName;
if (strpos($tableName, '.') !== false) {
[$schema, $table] = explode('.', $tableName);
Expand All @@ -1540,18 +1559,6 @@ protected function getSchemaName(string $tableName): array
];
}

/**
* Gets the schema name.
*
* @return string
*/
protected function getGlobalSchemaName(): string
{
$options = $this->getOptions();

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

/**
* @inheritDoc
*/
Expand All @@ -1574,6 +1581,7 @@ public function getDecoratedConnection(): Connection
'username' => $options['user'] ?? null,
'password' => $options['pass'] ?? null,
'database' => $options['name'],
'schema' => $this->schema,
'quoteIdentifiers' => true,
] + $options;

Expand All @@ -1590,7 +1598,7 @@ public function setSearchPath(): void
$this->execute(
sprintf(
'SET search_path TO %s,"$user",public',
$this->quoteSchemaName($this->getGlobalSchemaName())
$this->quoteSchemaName($this->schema)
)
);
}
Expand Down
13 changes: 12 additions & 1 deletion tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ public function testConnectionWithSocketConnection()
$this->assertInstanceOf('\PDO', $this->adapter->getConnection());
}

public function testConnectionWithSchema()
{
$this->adapter->connect();
$this->adapter->createSchema('foo');

$options = PGSQL_DB_CONFIG;
$options['schema'] = 'foo';
$adapter = new PostgresAdapter($options, new ArrayInput([]), new NullOutput());
$adapter->connect();
$this->assertTrue($adapter->hasTable('foo.'. $adapter->getSchemaTableName()));
}

public function testCreatingTheSchemaTableOnConnect()
{
$this->adapter->connect();
Expand All @@ -150,7 +162,6 @@ public function testCreatingTheSchemaTableOnConnect()
public function testSchemaTableIsCreatedWithPrimaryKey()
{
$this->adapter->connect();
new Table($this->adapter->getSchemaTableName(), [], $this->adapter);
$this->assertTrue($this->adapter->hasIndex($this->adapter->getSchemaTableName(), ['version']));
}

Expand Down

0 comments on commit f5b2736

Please sign in to comment.