Skip to content

CO-2252_Refactor_MatchgridBuilder_Fix_dbal_Deprecations #25

Merged
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
95 changes: 53 additions & 42 deletions app/src/Lib/Match/MatchgridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,54 @@

namespace App\Lib\Match;

use Cake\Datasource\ConnectionInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Utility\Xml;
use Cake\Datasource\EntityInterface;

use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\SchemaException;

class MatchgridBuilder {
/**
* Build the requested Matchgrid.
*
* @since COmanage Match v1.0.0
* @param \Cake\Datasource\EntityInterface $Matchgrid Matchgrid Object
* @param array $attributes Array of Attributes
* @param bool $indexes Whether to build indexes (disable for bulk loading only)
* @param EntityInterface $Matchgrid Matchgrid Object
* @param array $attributes Array of Attributes
* @param bool $indexes Whether to build indexes (disable for bulk loading only)
* @throws SchemaException|DBALException|Exception
*/

public function build(\Cake\Datasource\EntityInterface $Matchgrid, array $attributes, bool $indexes=true) {
public function build(EntityInterface $Matchgrid, array $attributes, bool $indexes=true): void
{
// Connect to the database
$dbc = $this->connect();

// Build and execute the schema
$this->configToSchema($dbc, $Matchgrid, $attributes, $indexes);

// Disconnect
// No DBAL disconnect?
// $dbc->disconnect();
}

/**
* Convert a Matchgrid Attribute configuration into a DBAL schema.
*
*
* @since COmanage Match v1.0.0
* @param DBALConnection $dbc DBAL Connection Object
* @param \Cake\Datasource\EntityInterface $Matchgrid Matchgrid Object
* @param array $attributes Array of Attributes
* @param bool $indexes Whether to build indexes (disable for bulk loading only)
* @throws Exceptions
* @param DBALConnection $dbc DBAL Connection Object
* @param EntityInterface $Matchgrid Matchgrid Object
* @param array $attributes Array of Attributes
* @param bool $indexes Whether to build indexes (disable for bulk loading only)
* @throws SchemaException
*/

protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchgrid, array $attributes, bool $indexes=true) {

protected function configToSchema(
DBALConnection $dbc,
EntityInterface $Matchgrid,
array $attributes,
bool $indexes=true): void
{
// Unlike ADOdb, there is no native DBAL format. We could create a JSON
// document similar to what DatabaseCommand uses, but the use case is just
// different enough that it's not really worth the effort at the moment.
Expand All @@ -82,9 +87,9 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg

// Create the table
$table = $schema->createTable($Matchgrid->prefixed_table_name);
// For type definitions see https://www.doctrine-project.org/api/dbal/2.9/Doctrine/DBAL/Types/Type.html

// For type definitions see https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#types

// There are various mandatory columns that we hardcode here.
$table->addColumn("id", "integer", ['autoincrement' => true, 'notnull' => true]);
// Maybe SOR Label and ID should be UI configured so length can be set?
Expand Down Expand Up @@ -113,11 +118,12 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg
// $flags and $options as passed to Index(), but otherwise undocumented
$flags = [];
$options = [];

$indexPrefix = "matchgrid_" . $Matchgrid->id;

$i = 1;

// Start with the standard indexes
$indexLabel = "matchgrid_" . $Matchgrid->id . "_i";
$indexLabel = $indexPrefix . "_i";
$table->addIndex(['sor'], $indexLabel.$i++, $flags, $options);
$table->addIndex(['sorid'], $indexLabel.$i++, $flags, $options);
$table->addUniqueIndex(['sor', 'sorid'], $indexLabel.$i++, $options);
Expand All @@ -127,7 +133,7 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg
// Add in indexes for configured fields
foreach($attributes as $attr) {
// We use the Entity ID to provide some level of reproducibility
$indexName = 'matchgrid_' . $Matchgrid->id . '_attr_id' . $attr->id;
$indexName = $indexPrefix . '_attr_id' . $attr->id;

$table->addIndex([$attr->name], $indexName, $flags, $options);

Expand All @@ -143,11 +149,11 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg
$toSql = $schema->toSql($dbc->getDatabasePlatform());

// SchemaManager provides info about the database
$sm = $dbc->getSchemaManager();
$sm = $dbc->createSchemaManager();

// The is the current database representation
// This is the current database representation
$curSchema = $sm->createSchema();

$fromSql = $curSchema->toSql($dbc->getDatabasePlatform());

$comparator = new Comparator();
Expand Down Expand Up @@ -180,21 +186,22 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg
$sql = "CREATE INDEX IF NOT EXISTS " . $matches[1] . " ON " . $Matchgrid->prefixed_table_name . " (LOWER(" . $attr . "))";
}
}

$stmt = $dbc->query($sql);

// $stmt just returns the query string so we don't bother examining it
$stmt = $dbc->query($sql);
}
}

/**
* Connect to the Database.
*
*
* @since COmanage Match v1.0.0
* @return \Doctrine\DBAL\Connection
* @throws DBALException
* @return DBALConnection
* @throws DBALException|Exception
*/

protected function connect() {
protected function connect(): DBALConnection
{
// There's some overlap between here and DatabaseCommand.

// Use the ConnectionManager to get the database config to pass to adodb.
Expand All @@ -204,20 +211,24 @@ protected function connect() {
$cfg = $db->config();

// We only support Postgres (at least for now)
if($cfg['driver'] != "Cake\Database\Driver\Postgres") {
if($cfg['driver'] !== "Cake\Database\Driver\Postgres") {
throw new \RuntimeException(__('match.er.db.driver' , [ $cfg['driver'] ]));
}

$config = new \Doctrine\DBAL\Configuration();

// Map CAKE DB drivers to PHP DB drivers
$driver = [
'Cake\Database\Driver\Postgres' => 'pdo_pgsql',
'Cake\Database\Driver\Mysql' => 'pdo_mysql',
];

$cfargs = [
'dbname' => $cfg['database'],
'user' => $cfg['username'],
'password' => $cfg['password'],
'host' => $cfg['host'],
'driver' => ($cfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "pdo_mysql")
'driver' => $driver[ $cfg['driver'] ]
];

return DriverManager::getConnection($cfargs, $config);
return DriverManager::getConnection($cfargs);
}
}