Skip to content

Commit

Permalink
Merge 293aba9 into 788210f
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Jul 28, 2017
2 parents 788210f + 293aba9 commit 06760ab
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .travis.install-mariadb-10.2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo apt-get -y remove --purge \
mysql-server-5.6 \
mysql-server-core-5.6 \
mysql-client-5.6 \
mysql-client-core-5.6
sudo rm -rf /var/lib/mysql
sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://download.nus.edu.sg/mirror/mariadb/repo/10.2/ubuntu trusty main'
sudo apt-get update
sudo apt-get install mariadb-server
32 changes: 31 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ language: php
matrix:
fast_finish: true
include:
- php: 7.1
dist: trusty
sudo: true
env:
- DEPENDENCIES=""
- DRIVER="pdo_mysql"
- DB=mariadb_10.2
addons:
mariadb: '10.2'
- php: 7.1
dist: trusty
sudo: true
env:
- DEPENDENCIES="--prefer-lowest --prefer-stable"
- DRIVER="pdo_mysql"
- DB=mariadb_10.2
addons:
mariadb: '10.2'
- php: 7.1
dist: trusty
sudo: true
env:
- DEPENDENCIES=""
- DRIVER="pdo_mysql"
- DB=mariadb_10.2
- DB_ATTR_ERRMODE=2 # \PDO::ERRMODE_EXCEPTION
addons:
mariadb: '10.2'
- php: 7.1
env:
- DEPENDENCIES=""
Expand Down Expand Up @@ -99,14 +127,16 @@ cache:
before_script:
- mkdir -p "$HOME/.php-cs-fixer"
- phpenv config-rm xdebug.ini
- VENDOR=$(echo $DB | cut -d'_' -f 1)
- if [[ $DB == 'mysql_5.7' ]]; then bash .travis.install-mysql-5.7.sh; fi
- if [[ $DB == 'mariadb_10.2' ]]; then bash .travis.install-mariadb-10.2.sh; fi
- if [[ $DRIVER == 'pdo_mysql' ]]; then mysql -e 'create database snapshot_tests;'; fi
- if [[ $DRIVER == 'pdo_pgsql' ]]; then psql -c 'create database snapshot_tests;' -U postgres; fi
- composer self-update
- composer update --prefer-dist $DEPENDENCIES

script:
- cp phpunit.xml.$DRIVER phpunit.xml
- cp phpunit.xml.$VENDOR phpunit.xml
- if [[ $TEST_COVERAGE == 'true' ]]; then php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml; else ./vendor/bin/phpunit; fi
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run; fi
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/docheader check src/ tests/; fi
Expand Down
15 changes: 13 additions & 2 deletions docs/setup.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Setup

The PDO SnapshotStore is currently tested with 2 backends, MySQL and Postgres.
The PDO SnapshotStore is currently tested with 3 backends, MariaDB, MySQL and Postgres.

In order to use it, you need have a database and create one (or multitple) snapshot tables.

For MySQL see: `scripts/mysql_snapshot_table.sql`
For MySQL and MariaDB see: `scripts/mysql_snapshot_table.sql`
For Postgres see: `scripts/postgres_snapshot_table.sql`

## Disable transaction handling

You can configure the snapshot store to disable transaction handling completely. In order to do this, set the last parameter
in the constructor to true (or configure your interop config factory accordingly, key is `disable_transaction_handling`).

Enabling this feature will disable all transaction handling and you have to take care yourself to start, commit and rollback
transactions.

Note: This could lead to problems using the snapshot store, if you did not manage to handle the transaction handling accordingly.
This is your problem and we will not provide any support for problems you encounter while doing so.
File renamed without changes.
36 changes: 36 additions & 0 deletions phpunit.xml.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuite name="Prooph PDO Snapshot Store Test Suite">
<directory>./tests</directory>
</testsuite>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>

<php>
<env name="db_type" value="pdo_mysql"/>
<env name="db_host" value="127.0.0.1"/>
<env name="db_username" value="root"/>
<env name="db_password" value=""/>
<env name="db_name" value="snapshot_tests"/>
<env name="db_port" value="3306"/>
<!-- \PDO::ERRMODE_SILENT -->
<env name="DB_ATTR_ERRMODE" value="0"/>
</php>
</phpunit>
File renamed without changes.
4 changes: 3 additions & 1 deletion src/Container/PdoSnapshotStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function __invoke(ContainerInterface $container): PdoSnapshotStore
$connection,
$config['snapshot_table_map'],
$config['default_snapshot_table_name'],
$serializer
$serializer,
$config['disable_transaction_handling']
);
}

Expand All @@ -95,6 +96,7 @@ public function defaultOptions(): iterable
'snapshot_table_map' => [],
'default_snapshot_table_name' => 'snapshots',
'serializer' => new CallbackSerializer(null, null),
'disable_transaction_handling' => false,
];
}
}
36 changes: 29 additions & 7 deletions src/PdoSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,23 @@ final class PdoSnapshotStore implements SnapshotStore
*/
private $serializer;

/**
* @var bool
*/
private $disableTransactionHandling;

public function __construct(
PDO $connection,
array $snapshotTableMap = [],
string $defaultSnapshotTableName = 'snapshots',
Serializer $serializer = null
Serializer $serializer = null,
bool $disableTransactionHandling = false
) {
$this->connection = $connection;
$this->snapshotTableMap = $snapshotTableMap;
$this->defaultSnapshotTableName = $defaultSnapshotTableName;
$this->serializer = $serializer ?: new CallbackSerializer(null, null);
$this->disableTransactionHandling = $disableTransactionHandling;
}

public function get(string $aggregateType, string $aggregateId): ?Snapshot
Expand All @@ -65,6 +72,7 @@ public function get(string $aggregateType, string $aggregateId): ?Snapshot
EOT;

$statement = $this->connection->prepare($query);

try {
$statement->execute([$aggregateId]);
} catch (PDOException $exception) {
Expand Down Expand Up @@ -137,18 +145,25 @@ public function save(Snapshot ...$snapshots): void
$statements[] = $statement;
}

$this->connection->beginTransaction();
if (! $this->disableTransactionHandling) {
$this->connection->beginTransaction();
}

try {
foreach ($statements as $statement) {
$statement->execute();
}
} catch (PDOException $exception) {
$this->connection->rollBack();
if (! $this->disableTransactionHandling) {
$this->connection->rollBack();
}

throw RuntimeException::fromStatementErrorInfo($statement->errorInfo());
}

$this->connection->commit();
if (! $this->disableTransactionHandling) {
$this->connection->commit();
}
}

public function removeAll(string $aggregateType): void
Expand All @@ -161,7 +176,9 @@ public function removeAll(string $aggregateType): void

$statement = $this->connection->prepare($sql);

$this->connection->beginTransaction();
if (! $this->disableTransactionHandling) {
$this->connection->beginTransaction();
}

try {
$statement->execute([$aggregateType]);
Expand All @@ -170,11 +187,16 @@ public function removeAll(string $aggregateType): void
}

if ($statement->errorCode() !== '00000') {
$this->connection->rollBack();
if (! $this->disableTransactionHandling) {
$this->connection->rollBack();
}

throw RuntimeException::fromStatementErrorInfo($statement->errorInfo());
}

$this->connection->commit();
if (! $this->disableTransactionHandling) {
$this->connection->commit();
}
}

private function getTableName(string $aggregateType): string
Expand Down
48 changes: 48 additions & 0 deletions tests/PdoSnapshotStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,54 @@ public function it_uses_custom_snapshot_table_map()
$this->assertNotNull($statement->fetch(\PDO::FETCH_ASSOC));
}

public function it_ignores_transaction_handling_if_flag_is_enabled(): void
{
$this->connection = $this->prophesize(PDO::class);
$this->connection->beginTransaction()->shouldNotBeCalled();
$this->connection->commmit()->shouldNotBeCalled();
$this->connection->rollback()->shouldNotBeCalled();
$this->connection = $this->connection->reveal();

$this->connection->exec('DROP TABLE IF EXISTS snapshots');
switch (TestUtil::getDatabaseVendor()) {
case 'pdo_mysql':
$this->connection->exec(file_get_contents(__DIR__ . '/../scripts/mysql_snapshot_table.sql'));
break;
case 'pdo_pgsql':
$this->connection->exec(file_get_contents(__DIR__ . '/../scripts/postgres_snapshot_table.sql'));
break;
default:
throw new \RuntimeException('Invalid database vendor');
}

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

$this->createTable('bar');

$aggregateType = 'foo';
$aggregateRoot = new \stdClass();
$aggregateRoot->foo = 'bar';
$time = (string) microtime(true);

if (false === strpos($time, '.')) {
$time .= '.0000';
}

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

$snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 1, $now);

$this->snapshotStore->save($snapshot);

$this->snapshotStore->removeAll('foo');
}

protected function setUp(): void
{
$this->connection = TestUtil::getConnection();
Expand Down

0 comments on commit 06760ab

Please sign in to comment.