Skip to content

Commit

Permalink
Replace uses of deprecated Connection::query() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Oct 29, 2023
1 parent e19bfbb commit 243e9ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/Services/DatabaseBackup/MysqlDatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ public function restore(AbstractExecutor $executor, array $excludedTables = []):
$em = $executor->getReferenceRepository()->getManager();
$connection = $em->getConnection();

$connection->query('SET FOREIGN_KEY_CHECKS = 0;');
if (method_exists($connection, 'executeQuery')) {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 0;');
} else {
$connection->query('SET FOREIGN_KEY_CHECKS = 0;');
}

$this->updateSchemaIfNeed($em);
$truncateSql = [];
foreach ($this->metadatas as $classMetadata) {
Expand All @@ -95,17 +100,29 @@ public function restore(AbstractExecutor $executor, array $excludedTables = []):
}
}
if (!empty($truncateSql)) {
$connection->query(implode(';', $truncateSql));
if (method_exists($connection, 'executeQuery')) {
$connection->executeQuery(implode(';', $truncateSql));
} else {
$connection->query(implode(';', $truncateSql));
}
}

// Only run query if it exists, to avoid the following exception:
// SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty
$backup = $this->getBackup();
if (!empty($backup)) {
$connection->query($backup);
if (method_exists($connection, 'executeQuery')) {
$connection->executeQuery($backup);
} else {
$connection->query($backup);
}
}

$connection->query('SET FOREIGN_KEY_CHECKS = 1;');
if (method_exists($connection, 'executeQuery')) {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 1;');
} else {
$connection->query('SET FOREIGN_KEY_CHECKS = 1;');
}

if (self::$metadata) {
// it need for better performance
Expand Down
14 changes: 12 additions & 2 deletions src/Services/DatabaseTools/ORMDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ protected function disableForeignKeyChecksIfApplicable(): void
return;
}

$this->connection->query('SET FOREIGN_KEY_CHECKS=0');
if (method_exists($this->connection, 'executeQuery')) {
$this->connection->executeQuery('SET FOREIGN_KEY_CHECKS=0');
} else {
$this->connection->query('SET FOREIGN_KEY_CHECKS=0');
}

$this->shouldEnableForeignKeyChecks = true;
}

Expand All @@ -226,7 +231,12 @@ protected function enableForeignKeyChecksIfApplicable(): void
return;
}

$this->connection->query('SET FOREIGN_KEY_CHECKS=1');
if (method_exists($this->connection, 'executeQuery')) {
$this->connection->executeQuery('SET FOREIGN_KEY_CHECKS=1');
} else {
$this->connection->query('SET FOREIGN_KEY_CHECKS=1');
}

$this->shouldEnableForeignKeyChecks = false;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Services/DatabaseTools/ORMSqliteDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ protected function disableForeignKeyChecksIfApplicable(): void
return;
}

$this->connection->query('PRAGMA foreign_keys = 0');
if (method_exists($this->connection, 'executeQuery')) {
$this->connection->executeQuery('PRAGMA foreign_keys = 0');
} else {
$this->connection->query('PRAGMA foreign_keys = 0');
}

$this->shouldEnableForeignKeyChecks = true;
}

Expand All @@ -150,7 +155,12 @@ protected function enableForeignKeyChecksIfApplicable(): void
return;
}

$this->connection->query('PRAGMA foreign_keys = 1');
if (method_exists($this->connection, 'executeQuery')) {
$this->connection->executeQuery('PRAGMA foreign_keys = 1');
} else {
$this->connection->query('PRAGMA foreign_keys = 1');
}

$this->shouldEnableForeignKeyChecks = false;
}

Expand Down

0 comments on commit 243e9ae

Please sign in to comment.