Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ResetCommand behavior for PostgreSQL #81

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 28 additions & 24 deletions src/Console/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle(ConfigurationProvider $provider)
if (!$this->confirmToProceed()) {
return;
}

$configuration = $provider->getForConnection(
$this->option('connection')
);
Expand Down Expand Up @@ -68,7 +68,7 @@ private function throwExceptionIfPlatformIsNotSupported()
{
$platformName = $this->connection->getDatabasePlatform()->getName();

if (!array_key_exists($platformName, $this->getCardinalityCheckInstructions())) {
if (!array_key_exists($platformName, $this->getPlatformInstructions())) {
throw new Exception(sprintf('The platform %s is not supported', $platformName));
}
}
Expand All @@ -79,46 +79,50 @@ private function throwExceptionIfPlatformIsNotSupported()
private function safelyDropTable($table)
{
$platformName = $this->connection->getDatabasePlatform()->getName();
$instructions = $this->getCardinalityCheckInstructions()[$platformName];
$instructions = $this->getPlatformInstructions()[$platformName];

$queryDisablingCardinalityChecks = $instructions['needsTableIsolation'] ?
sprintf($instructions['disable'], $table) :
$instructions['disable'];
$this->connection->query($queryDisablingCardinalityChecks);
if (isset($instructions['isolation']['enable'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot find any key named isolation, did you mean tableIsolation?

$statement = sprintf($instructions['isolation']['enable'], $table);
$this->connection->exec($statement);
}

$schema = $this->connection->getSchemaManager();
$schema->dropTable($table);
$dropStatement = sprintf($instructions['dropStatement'], $table);
$this->connection->exec($dropStatement);

// When table is already dropped we cannot enable any cardinality checks on it
// See https://github.com/laravel-doctrine/migrations/issues/50
if (!$instructions['needsTableIsolation']) {
$this->connection->query($instructions['enable']);
if (isset($instructions['isolation']['disable'])) {
$statement = sprintf($instructions['isolation']['disable'], $table);
$this->connection->exec($statement);
}
}

/**
* @return array
*/
private function getCardinalityCheckInstructions()
private function getPlatformInstructions()
{
return [
'mssql' => [
'needsTableIsolation' => true,
'disable' => 'ALTER TABLE %s CHECK CONSTRAINT ALL',
'tableIsolation' => [
'disable' => 'ALTER TABLE %s CHECK CONSTRAINT ALL',
],
'dropStatement' => 'DROP TABLE %s',
],
'mysql' => [
'needsTableIsolation' => false,
'enable' => 'SET FOREIGN_KEY_CHECKS = 1',
'disable' => 'SET FOREIGN_KEY_CHECKS = 0',
'tableIsolation' => [
'enable' => 'SET FOREIGN_KEY_CHECKS = 1',
'disable' => 'SET FOREIGN_KEY_CHECKS = 0',
],
'dropStatement' => 'DROP TABLE %s',
],
'postgresql' => [
'needsTableIsolation' => true,
'disable' => 'ALTER TABLE %s DISABLE TRIGGER ALL',
'dropStatement' => 'DROP TABLE IF EXISTS %s CASCADE',
],
'sqlite' => [
'needsTableIsolation' => false,
'enable' => 'PRAGMA foreign_keys = ON',
'disable' => 'PRAGMA foreign_keys = OFF',
'tableIsolation' => [
'enable' => 'PRAGMA foreign_keys = ON',
'disable' => 'PRAGMA foreign_keys = OFF',
],
'dropStatement' => 'DROP TABLE %s',
],
];
}
Expand Down