Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Support PostgreSQL DB #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
74 changes: 31 additions & 43 deletions app/ProophessorDo/Projection/Todo/TodoReadModel.php
Original file line number Diff line number Diff line change
@@ -23,70 +23,58 @@ final class TodoReadModel extends AbstractReadModel
*/
private $connection;

/** @var \Doctrine\DBAL\Platforms\AbstractPlatform */
private $platform;


public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->platform = $this->connection->getDatabasePlatform();
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function init(): void
{
$tableName = Table::TODO;

$sql = <<<EOT
CREATE TABLE `$tableName` (
`id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`assignee_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`text` longtext COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(7) COLLATE utf8_unicode_ci NOT NULL,
`deadline` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`reminder` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_a_status` (`assignee_id`,`status`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
EOT;

$statement = $this->connection->prepare($sql);
$statement->execute();
$schema = new \Doctrine\DBAL\Schema\Schema();

$table = $schema->createTable($tableName);
$table->addColumn('id', 'string', ['unsigned' => true, 'length' => 36, 'notnull' => true]);
$table->addColumn('assignee_id', 'string', ['unsigned' => true, 'length' => 36, 'notnull' => true]);
$table->addColumn('text', 'text', ['notnull' => true]);
$table->addColumn('status', 'string', ['length' => 7, 'notnull' => true]);
$table->addColumn('deadline', 'string', ['length' => 30, 'default' => null]);
$table->addColumn('reminder', 'string', ['length' => 30, 'default' => null]);
$table->setPrimaryKey(['id']);
$table->addIndex(['assignee_id', 'status'], 'idx_a_status');
$table->addIndex(['status'], 'idx_status');

foreach ($schema->toSql($this->platform) as $query) {
$statement = $this->connection->prepare($query);
$statement->execute();
}
}

public function isInitialized(): bool
{
$tableName = Table::TODO;

$sql = "SHOW TABLES LIKE '$tableName';";

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

$result = $statement->fetch();

if (false === $result) {
return false;
}

return true;
return $this->connection->getSchemaManager()->tablesExist([Table::TODO]);
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function reset(): void
{
$tableName = Table::TODO;

$sql = "TRUNCATE TABLE '$tableName';";
EOT;

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->executeUpdate($this->platform->getTruncateTableSQL(Table::TODO, true));
}

public function delete(): void
{
$tableName = Table::TODO;

$sql = "DROP TABLE $tableName;";

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->getSchemaManager()->dropTable(Table::TODO);
}

protected function insert(array $data): void
68 changes: 30 additions & 38 deletions app/ProophessorDo/Projection/Todo/TodoReminderReadModel.php
Original file line number Diff line number Diff line change
@@ -23,65 +23,53 @@ final class TodoReminderReadModel extends AbstractReadModel
*/
private $connection;

/** @var \Doctrine\DBAL\Platforms\AbstractPlatform */
private $platform;


public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->platform = $this->connection->getDatabasePlatform();
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function init(): void
{
$tableName = Table::TODO_REMINDER;

$sql = <<<EOT
CREATE TABLE `$tableName` (
`todo_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`reminder` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`todo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
EOT;

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

public function isInitialized(): bool
{
$tableName = Table::TODO_REMINDER;

$sql = "SHOW TABLES LIKE '$tableName';";

$statement = $this->connection->prepare($sql);
$statement->execute();
$schema = new \Doctrine\DBAL\Schema\Schema();

$result = $statement->fetch();
$table = $schema->createTable($tableName);
$table->addColumn('todo_id', 'string', ['unsigned' => true, 'length' => 36, 'notnull' => true]);
$table->addColumn('reminder', 'string', ['length' => 30, 'notnull' => true]);
$table->addColumn('status', 'string', ['length' => 10, 'notnull' => true]);
$table->setPrimaryKey(['todo_id']);

if (false === $result) {
return false;
foreach ($schema->toSql($this->platform) as $query) {
$statement = $this->connection->prepare($query);
$statement->execute();
}
}

return true;
public function isInitialized(): bool
{
return $this->connection->getSchemaManager()->tablesExist([Table::TODO_REMINDER]);
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function reset(): void
{
$tableName = Table::TODO_REMINDER;

$sql = "TRUNCATE TABLE '$tableName';";
EOT;

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->executeUpdate($this->platform->getTruncateTableSQL(Table::TODO_REMINDER, true));
}

public function delete(): void
{
$tableName = Table::TODO_REMINDER;

$sql = "DROP TABLE $tableName;";

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->getSchemaManager()->dropTable(Table::TODO_REMINDER);
}

protected function insert(array $data): void
@@ -98,6 +86,10 @@ protected function update(array $data, array $identifier): void
);
}

/**
* @param array $query
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException
*/
protected function remove(array $query): void
{
$this->connection->delete(
15 changes: 15 additions & 0 deletions app/ProophessorDo/Projection/User/UserFinder.php
Original file line number Diff line number Diff line change
@@ -33,6 +33,11 @@ public function findAll(): array
return $this->connection->fetchAll(sprintf('SELECT * FROM %s', Table::USER));
}

/**
* @param string $userId
* @return null|\stdClass
* @throws \Doctrine\DBAL\DBALException
*/
public function findById(string $userId): ?\stdClass
{
$stmt = $this->connection->prepare(sprintf('SELECT * FROM %s WHERE id = :user_id', Table::USER));
@@ -48,6 +53,11 @@ public function findById(string $userId): ?\stdClass
return $result;
}

/**
* @param string $emailAddress
* @return null|\stdClass
* @throws \Doctrine\DBAL\DBALException
*/
public function findOneByEmailAddress(string $emailAddress): ?\stdClass
{
$stmt = $this->connection->prepare(sprintf('SELECT * FROM %s WHERE email = :email LIMIT 1', Table::USER));
@@ -63,6 +73,11 @@ public function findOneByEmailAddress(string $emailAddress): ?\stdClass
return $result;
}

/**
* @param string $todoId
* @return null|\stdClass
* @throws \Doctrine\DBAL\DBALException
*/
public function findUserOfTodo(string $todoId): ?\stdClass
{
$stmt = $this->connection->prepare(sprintf(
69 changes: 29 additions & 40 deletions app/ProophessorDo/Projection/User/UserReadModel.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
namespace Prooph\ProophessorDo\Projection\User;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Prooph\EventStore\Projection\AbstractReadModel;
use Prooph\ProophessorDo\Projection\Table;

@@ -23,67 +24,55 @@ final class UserReadModel extends AbstractReadModel
*/
private $connection;

/** @var \Doctrine\DBAL\Platforms\AbstractPlatform */
private $platform;

public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->platform = $this->connection->getDatabasePlatform();
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function init(): void
{
$tableName = Table::USER;

$sql = <<<EOT
CREATE TABLE `$tableName` (
`id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`open_todos` int(11) NOT NULL DEFAULT '0',
`done_todos` int(11) NOT NULL DEFAULT '0',
`expired_todos` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
EOT;

$statement = $this->connection->prepare($sql);
$statement->execute();
}
$schema = new \Doctrine\DBAL\Schema\Schema();

public function isInitialized(): bool
{
$tableName = Table::USER;

$sql = "SHOW TABLES LIKE '$tableName';";

$statement = $this->connection->prepare($sql);
$statement->execute();
$table = $schema->createTable($tableName);
$table->addColumn('id', 'string', ['unsigned' => true, 'length' => 36, 'notnull' => true]);
$table->addColumn('name', 'string', ['length' => 50, 'notnull' => true]);
$table->addColumn('email', 'string', ['length' => 100, 'notnull' => true]);
$table->addColumn('open_todos', 'integer', ['length' => 11, 'notnull' => true, 'default' => 0]);
$table->addColumn('done_todos', 'integer', ['length' => 11, 'notnull' => true, 'default' => 0]);
$table->addColumn('expired_todos', 'integer', ['length' => 32, 'notnull' => true, 'default' => 0]);
$table->setPrimaryKey(['id']);

$result = $statement->fetch();

if (false === $result) {
return false;
foreach ($schema->toSql($this->platform) as $query) {
$statement = $this->connection->prepare($query);
$statement->execute();
}
}

return true;
public function isInitialized(): bool
{
return $this->connection->getSchemaManager()->tablesExist([Table::USER]);
}

/**
* @throws \Doctrine\DBAL\DBALException
*/
public function reset(): void
{
$tableName = Table::USER;

$sql = "TRUNCATE TABLE $tableName;";

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->executeUpdate($this->platform->getTruncateTableSQL(Table::USER, true));
}

public function delete(): void
{
$tableName = Table::USER;

$sql = "DROP TABLE $tableName;";

$statement = $this->connection->prepare($sql);
$statement->execute();
$this->connection->getSchemaManager()->dropTable(Table::USER);
}

protected function insert(array $data): void
2 changes: 1 addition & 1 deletion app/Providers/ProophessorDoServiceProvider.php
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ public function register(): void
case 'mysql':
$driver = 'pdo_mysql';
break;
case 'postgres':
case 'pgsql':
$driver = 'pdo_pgsql';
}