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

[5.3] Allow wrapping schema changes in a transaction if the database supports it #15780

Merged
merged 2 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
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
29 changes: 27 additions & 2 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Migrations;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -159,7 +160,9 @@ protected function runUp($file, $batch, $pretend)
return $this->pretendToRun($migration, 'up');
}

$migration->up();
$this->runMigration(function () use ($migration) {
$migration->up();
});

// Once we have run a migrations class, we will log that it was run in this
// repository so that we don't try to run it next time we do a migration
Expand Down Expand Up @@ -279,7 +282,9 @@ protected function runDown($file, $migration, $pretend)
return $this->pretendToRun($instance, 'down');
}

$instance->down();
$this->runMigration(function () use ($instance) {
$instance->down();
});

// Once we have successfully run the migration "down" we will remove it from
// the migration repository so it will be considered to have not been run
Expand Down Expand Up @@ -356,6 +361,26 @@ protected function getQueries($migration, $method)
});
}

/**
* Run a migration, inside a transaction if the database supports it.
*
* @param \Closure $callback
* @return void
*/
protected function runMigration(Closure $callback)
{
// To deal with potential errors during migrations that may leave a database
// in an invalid state, we wrap everything in a transaction so all changes
// are rolled back, and the developer need not manually repair errors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Taylor approves of this cascade 😂

$connection = $this->resolveConnection($this->connection);

$grammar = $connection->getSchemaGrammar();

$grammar->supportsSchemaTransactions()
? $connection->transaction($callback)
: $callback();
}

/**
* Resolve a migration instance from a file.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

abstract class Grammar extends BaseGrammar
{
/**
* If this Grammar supports schema changes wrapped in a transaction.
*
* @var bool
*/
protected $transactions = false;

/**
* Compile a rename column command.
*
Expand Down Expand Up @@ -456,4 +463,14 @@ protected function mapFluentValueToDoctrine($option, $value)
{
return $option == 'notnull' ? ! $value : $value;
}

/**
* Check if this Grammar supports schema changes wrapped in a transaction.
*
* @return bool
*/
public function supportsSchemaTransactions()
{
return $this->transactions;
}
}
7 changes: 7 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

class PostgresGrammar extends Grammar
{
/**
* If this Grammar supports schema changes wrapped in a transaction.
*
* @var bool
*/
protected $transactions = true;

/**
* The possible column modifiers.
*
Expand Down