Skip to content

Commit

Permalink
Merge pull request #1636 from tripal/tv4g9-issue1523-Drupal10.1-compa…
Browse files Browse the repository at this point in the history
…tibility

Striving towards Drupal 10.1 Compatibility
  • Loading branch information
spficklin committed Sep 16, 2023
2 parents c95c57c + 2ce7b70 commit 0cc0479
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
21 changes: 17 additions & 4 deletions tripal/src/TripalDBX/TripalDbxConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
* and offers, beside others, the follwing methods: addIndex(),
* addPrimaryKey(), addUniqueKey(), createTable(), dropField(), dropIndex(),
* dropPrimaryKey(), dropTable(), dropUniqueKey(), fieldExists(),
* findPrimaryKeyColumns(), findTables(), indexExists(), renameTable(),
* tableExists() and more from the documentation.
* findPrimaryKeyColumns(), findTables(), indexExists(), renameTable()
* and more from the documentation.
*
* A couple of methods have been added to this class to complete the above list.
*
Expand Down Expand Up @@ -697,9 +697,9 @@ public function setExtraSchema(string $schema_name, int $index = 2) :void {
*/
public function getVersion() :string {

if ((NULL === $this->version) && !empty($this->usedSchemas[1])) {
if (!is_numeric($this->version) && !empty($this->usedSchemas[1])) {
// Get the version of the schema.
$this->version = $this->findVersion();
$this->version = (string) $this->findVersion();
}

return $this->version ?? '';
Expand Down Expand Up @@ -978,6 +978,8 @@ function ($matches) {
* Find the prefix for a table.
*
* OVERRIDES \Drupal\Core\Database\Connection:tablePrefix().
* REMOVED IN Drupal 10.1.x
* SEE https://www.drupal.org/node/3260849
*
* This function is for when you want to know the prefix of a table. This
* is not used in prefixTables due to performance reasons.
Expand Down Expand Up @@ -1034,6 +1036,17 @@ public function tablePrefix(
}
}

/**
* Returns the prefix of the tables.
*
* OVERRIDES \Drupal\Core\Database\Connection:getPrefix().
*
* @return string $prefix
*/
public function getPrefix(): string {
return $this->usedSchemas[1] . '.';
}

/**
* Executes all the given SQL statements into the current schema.
*
Expand Down
22 changes: 20 additions & 2 deletions tripal/src/TripalDBX/TripalDbxSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* - createTable(), dropTable()
* - dropField(), dropIndex(), dropPrimaryKey(), dropUniqueKey(),
* - fieldExists(), findPrimaryKeyColumns(),
* - renameTable(), tableExists()
* - renameTable()
* and more from the documentation.
*
* @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Schema.php/class/Schema/9.0.x
Expand Down Expand Up @@ -144,7 +144,7 @@ public function __construct(
}

/**
*
*
*/
public function initialize() {
if (!$this->initialized) {
Expand Down Expand Up @@ -1137,4 +1137,22 @@ public function dropSchema() :void {
$this->tripalDbxApi->dropSchema($this->defaultSchema, $this->connection);
}

/**
* Overrides Drupal\Core\Database\Schema->tableExists().
*
* We needed to override it because core Drupal makes some assumptions
* when building the where condition that do not match our multi-schema setup.
*/
public function tableExists($table) {

// We can't use \Drupal::database()->select() here
// because it would prefix information_schema.tables
// and the query would fail.
// Don't use {} around information_schema.tables table.
return (bool) $this->connection
->query('SELECT TRUE AS table_exists FROM pg_tables
WHERE schemaname=:schema AND tablename = :table',
[':table' => $table, ':schema' => $this->getSchemaName()])
->fetchField();
}
}
6 changes: 5 additions & 1 deletion tripal/tests/src/Kernel/TripalDBX/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testConnectionConstructorAllDefault() {
// Create a mock for the abstract class.
$dbmock = $this->getMockBuilder(\Drupal\tripal\TripalDBX\TripalDbxConnection::class)
->disableOriginalConstructor()
->onlyMethods(['setTarget', 'setKey', 'setSchemaName'])
->onlyMethods(['setTarget', 'setKey', 'setSchemaName', 'findVersion'])
->getMockForAbstractClass()
;

Expand All @@ -160,6 +160,10 @@ public function testConnectionConstructorAllDefault() {
->method('setSchemaName')
->with($this->equalTo(''))
;
$dbmock
->expects($this->any())
->method('findVersion')
->willReturn('');

// Call the constructor.
$reflected_class = new \ReflectionClass(\Drupal\tripal\TripalDBX\TripalDbxConnection::class);
Expand Down
11 changes: 7 additions & 4 deletions tripal_chado/src/Database/ChadoSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public function getSchemaDef(array $parameters) :array {

$source = $parameters['source'] ?? 'file';
$format = strtolower($parameters['format'] ?? '');
$version = $parameters['version']
?? $this->connection->getVersion()
?? $DEFAULT_VERSION
;

$version = $parameters['version'];
if (!array_key_exists('version', $parameters) OR empty($parameters['version'])) {
$version = $this->connection->getVersion();
}

if (!empty($parameters['clear'])) {
$schema_structure = [];
}
Expand All @@ -49,6 +51,7 @@ public function getSchemaDef(array $parameters) :array {
. $version
. '.yml'
;

// Make sure we got a valid version format.
if (!preg_match('/^\\d\\.\\d$/', $version)
|| !file_exists($filename)
Expand Down

0 comments on commit 0cc0479

Please sign in to comment.