Skip to content

Commit c00be46

Browse files
committed
Move schema to class property for PostgresAdapter
1 parent a093a96 commit c00be46

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

docs/en/configuration.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,26 @@ Phinx currently supports the following database adapters natively:
364364
* `SQLite <https://www.sqlite.org/>`_: specify the ``sqlite`` adapter.
365365
* `SQL Server <https://www.microsoft.com/sqlserver>`_: specify the ``sqlsrv`` adapter.
366366

367+
The following settings are available for the adapters:
368+
369+
adapter
370+
The name of the adapter to use, e.g. ``pgsql``
371+
host
372+
The database server's hostname (or IP address).
373+
port
374+
The database server's TCP port number.
375+
user
376+
The username for the database.
377+
pass
378+
The password for the database.
379+
name
380+
The name of the database for this environment. For SQLite, it's recommended to use an absolute path,
381+
without the file extension.
382+
suffix
383+
The suffix to use for the SQLite database file. Defaults to ``.sqlite3``.
384+
schema
385+
For PostgreSQL, allows specifying the schema to use for the database. Defaults to ``public``.
386+
367387
For each adapter, you may configure the behavior of the underlying PDO object by setting in your
368388
config object the lowercase version of the constant name. This works for both PDO options
369389
(e.g. ``\PDO::ATTR_CASE`` would be ``attr_case``) and adapter specific options (e.g. for MySQL

src/Phinx/Db/Adapter/PostgresAdapter.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ class PostgresAdapter extends PdoAdapter
6060
*/
6161
protected bool $useIdentity;
6262

63+
/**
64+
* @var string
65+
*/
66+
protected string $schema = 'public';
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function setOptions(array $options): AdapterInterface
72+
{
73+
parent::setOptions($options);
74+
75+
if (!empty($options['schema'])) {
76+
$this->schema = $options['schema'];
77+
}
78+
79+
return $this;
80+
}
81+
6382
/**
6483
* {@inheritDoc}
6584
*/
@@ -1378,8 +1397,8 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
13781397
public function createSchemaTable(): void
13791398
{
13801399
// Create the public/custom schema if it doesn't already exist
1381-
if ($this->hasSchema($this->getGlobalSchemaName()) === false) {
1382-
$this->createSchema($this->getGlobalSchemaName());
1400+
if ($this->hasSchema($this->schema) === false) {
1401+
$this->createSchema($this->schema);
13831402
}
13841403

13851404
$this->setSearchPath();
@@ -1528,7 +1547,7 @@ protected function isArrayType(string|Literal $columnType): bool
15281547
*/
15291548
protected function getSchemaName(string $tableName): array
15301549
{
1531-
$schema = $this->getGlobalSchemaName();
1550+
$schema = $this->schema;
15321551
$table = $tableName;
15331552
if (strpos($tableName, '.') !== false) {
15341553
[$schema, $table] = explode('.', $tableName);
@@ -1540,18 +1559,6 @@ protected function getSchemaName(string $tableName): array
15401559
];
15411560
}
15421561

1543-
/**
1544-
* Gets the schema name.
1545-
*
1546-
* @return string
1547-
*/
1548-
protected function getGlobalSchemaName(): string
1549-
{
1550-
$options = $this->getOptions();
1551-
1552-
return empty($options['schema']) ? 'public' : $options['schema'];
1553-
}
1554-
15551562
/**
15561563
* @inheritDoc
15571564
*/
@@ -1574,6 +1581,7 @@ public function getDecoratedConnection(): Connection
15741581
'username' => $options['user'] ?? null,
15751582
'password' => $options['pass'] ?? null,
15761583
'database' => $options['name'],
1584+
'schema' => $this->schema,
15771585
'quoteIdentifiers' => true,
15781586
] + $options;
15791587

@@ -1590,7 +1598,7 @@ public function setSearchPath(): void
15901598
$this->execute(
15911599
sprintf(
15921600
'SET search_path TO %s,"$user",public',
1593-
$this->quoteSchemaName($this->getGlobalSchemaName())
1601+
$this->quoteSchemaName($this->schema)
15941602
)
15951603
);
15961604
}

tests/Phinx/Db/Adapter/PostgresAdapterTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ public function testConnectionWithSocketConnection()
136136
$this->assertInstanceOf('\PDO', $this->adapter->getConnection());
137137
}
138138

139+
public function testConnectionWithSchema()
140+
{
141+
$this->adapter->connect();
142+
$this->adapter->createSchema('foo');
143+
144+
$options = PGSQL_DB_CONFIG;
145+
$options['schema'] = 'foo';
146+
$adapter = new PostgresAdapter($options, new ArrayInput([]), new NullOutput());
147+
$adapter->connect();
148+
$this->assertTrue($adapter->hasTable('foo.'. $adapter->getSchemaTableName()));
149+
}
150+
139151
public function testCreatingTheSchemaTableOnConnect()
140152
{
141153
$this->adapter->connect();
@@ -150,7 +162,6 @@ public function testCreatingTheSchemaTableOnConnect()
150162
public function testSchemaTableIsCreatedWithPrimaryKey()
151163
{
152164
$this->adapter->connect();
153-
new Table($this->adapter->getSchemaTableName(), [], $this->adapter);
154165
$this->assertTrue($this->adapter->hasIndex($this->adapter->getSchemaTableName(), ['version']));
155166
}
156167

0 commit comments

Comments
 (0)