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

Correctly escape schema namespaced table name in the PostgreSQL. #30

Merged
merged 3 commits into from
Apr 26, 2019
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
4 changes: 2 additions & 2 deletions src/Container/PdoSnapshotStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
15 changes: 12 additions & 3 deletions src/PdoSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -216,7 +216,16 @@ private function getTableName(string $aggregateType): string

switch ($this->vendor) {
case 'pgsql':
return '"'.$tableName.'"';
$pos = \strpos($tableName, '.');

if (false === $pos) {
return '"' . $tableName . '"';
}

$schema = \substr($tableName, 0, $pos);
$table = \substr($tableName, $pos + 1);

return '"' . $schema . '"."' . $table . '"';
default:
return "`$tableName`";
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Container/PdoSnapshotStoreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
4 changes: 2 additions & 2 deletions tests/Mock/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
64 changes: 62 additions & 2 deletions tests/PdoSnapshotStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -198,6 +198,54 @@ public function it_ignores_transaction_handling_if_flag_is_enabled(): void
$this->snapshotStore->removeAll('foo');
}

/**
* @test
*/
public function it_works_with_custom_schema()
{
if (TestUtil::getDatabaseVendor() !== 'pdo_pgsql') {
$this->markTestSkipped('Test case only for pdo_pgsql vendor');

return;
}

$this->createSchemaTable('prooph', 'snapshots');
$this->createSchemaTable('prooph', 'bar');

$this->snapshotStore = new PdoSnapshotStore(
$this->connection,
['foo' => 'prooph.bar'],
'prooph.snapshots'
);

$aggregateRoot1 = new \stdClass();
$aggregateRoot1->foo = 'bar';

$aggregateRoot2 = new \stdClass();
$aggregateRoot2->foo = 'baz';

$time = (string) \microtime(true);
if (false === \strpos($time, '.')) {
$time .= '.0000';
}

$now = \DateTimeImmutable::createFromFormat('U.u', $time);

$snapshot1 = new Snapshot('object', 'id_one', $aggregateRoot1, 1, $now);
$snapshot2 = new Snapshot('foo', 'id_two', $aggregateRoot2, 2, $now);

$this->snapshotStore->save($snapshot1, $snapshot2);

$stmt = $this->connection->query('SELECT * FROM prooph.snapshots');
$this->assertNotNull($stmt->fetch(PDO::FETCH_ASSOC));

$stmt = $this->connection->query('SELECT * FROM prooph.bar');
$this->assertNotNull($stmt->fetch(PDO::FETCH_ASSOC));

$this->assertEquals($snapshot1, $this->snapshotStore->get('object', 'id_one'));
$this->assertEquals($snapshot2, $this->snapshotStore->get('foo', 'id_two'));
}

protected function setUp(): void
{
$this->connection = TestUtil::getConnection();
Expand All @@ -222,6 +270,11 @@ protected function tearDown(): void
$statement->execute();
$statement = $this->connection->prepare('DROP TABLE IF EXISTS bar');
$statement->execute();

if (TestUtil::getDatabaseVendor() === 'pdo_pgsql') {
$statement = $this->connection->prepare('DROP SCHEMA IF EXISTS prooph CASCADE');
$statement->execute();
}
}

protected function createTable(string $name)
Expand All @@ -240,4 +293,11 @@ protected function createTable(string $name)
$sql = \str_replace('snapshots', $name, $sql);
$this->connection->exec($sql);
}

protected function createSchemaTable(string $schema, string $table)
{
$this->connection->exec('CREATE SCHEMA IF NOT EXISTS ' . $schema);

$this->createTable($schema . '.' . $table);
}
}
4 changes: 2 additions & 2 deletions tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* This file is part of prooph/pdo-snapshot-store.
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
* (c) 2016-2019 prooph software GmbH <contact@prooph.de>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down