Skip to content

Commit

Permalink
Merge branch '4.x' into 4.next
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 9, 2024
2 parents 2648e31 + 40b5ef9 commit 2b2890a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ class AbstractMigration extends BaseAbstractMigration
*/
public bool $autoId = true;

/**
* Hook method to decide if this migration should use transactions
*
* By default if your driver supports transactions, a transaction will be opened
* before the migration begins, and commit when the migration completes.
*
* @return bool
*/
public function useTransactions(): bool
{
return $this->getAdapter()->hasTransactions();
}

/**
* Returns an instance of the Table class.
*
Expand Down
11 changes: 10 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,16 @@ protected function getRenameColumnInstructions(string $tableName, string $column
if (strcasecmp($row['Field'], $columnName) === 0) {
$null = $row['Null'] === 'NO' ? 'NOT NULL' : 'NULL';
$comment = isset($row['Comment']) ? ' COMMENT ' . '\'' . addslashes($row['Comment']) . '\'' : '';
$extra = ' ' . strtoupper($row['Extra']);

// create the extra string by also filtering out the DEFAULT_GENERATED option (MySQL 8 fix)
$extras = array_filter(
explode(' ', strtoupper($row['Extra'])),
static function ($value) {
return $value !== 'DEFAULT_GENERATED';
},
);
$extra = ' ' . implode(' ', $extras);

if (($row['Default'] !== null)) {
$extra .= $this->getDefaultValueDefinition($row['Default']);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1631,4 +1631,18 @@ public function bulkinsert(Table $table, array $rows): void
$this->getConnection()->execute($sql, $vals);
}
}

/**
* Get the adapter type name
*
* @return string
*/
public function getAdapterType(): string
{
// Hardcoded because the parent implementation
// reads an option that is based off of Database\Driver
// names which is postgres, but pgsql is required for
// compatibility.
return 'pgsql';
}
}
8 changes: 0 additions & 8 deletions src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,15 +1046,7 @@ protected function copyAndDropTmpTable(AlterInstructions $instructions, string $
$state['selectColumns']
);

$result = $this->fetchRow('PRAGMA foreign_keys');
$foreignKeysEnabled = $result ? (bool)$result['foreign_keys'] : false;
if ($foreignKeysEnabled) {
$this->execute('PRAGMA foreign_keys = OFF');
}
$this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tableName)));
if ($foreignKeysEnabled) {
$this->execute('PRAGMA foreign_keys = ON');
}
$this->execute(sprintf(
'ALTER TABLE %s RENAME TO %s',
$this->quoteTableName($state['tmpTableName']),
Expand Down
14 changes: 9 additions & 5 deletions src/Migration/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ public function executeMigration(MigrationInterface $migration, string $directio
$migration->{MigrationInterface::INIT}();
}

$atomic = $adapter->hasTransactions();
if (method_exists($migration, 'useTransactions')) {
$atomic = $migration->useTransactions();
}
// begin the transaction if the adapter supports it
if ($adapter->hasTransactions()) {
if ($atomic) {
$adapter->beginTransaction();
}

Expand Down Expand Up @@ -124,7 +128,7 @@ public function executeMigration(MigrationInterface $migration, string $directio
$adapter->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));

// commit the transaction if the adapter supports it
if ($adapter->hasTransactions()) {
if ($atomic) {
$adapter->commitTransaction();
}

Expand All @@ -144,9 +148,9 @@ public function executeSeed(SeedInterface $seed): void
if (method_exists($seed, SeedInterface::INIT)) {
$seed->{SeedInterface::INIT}();
}

// begin the transaction if the adapter supports it
if ($adapter->hasTransactions()) {
$atomic = $adapter->hasTransactions();
if ($atomic) {
$adapter->beginTransaction();
}

Expand All @@ -156,7 +160,7 @@ public function executeSeed(SeedInterface $seed): void
}

// commit the transaction if the adapter supports it
if ($adapter->hasTransactions()) {
if ($atomic) {
$adapter->commitTransaction();
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,19 @@ public function testRenameColumnPreserveComment()
$this->assertEquals('comment1', $columns[1]['Comment']);
}

public function testRenameColumnWithDefaultGeneratedExtra()
{
$table = new Table('t', [], $this->adapter);
$table->save();
$this->assertFalse($table->hasColumn('last_changed'));
$table->addColumn('last_changed', 'datetime', ['default' => 'CURRENT_TIMESTAMP', 'null' => false])
->save();
$this->assertTrue($table->hasColumn('last_changed'));
$table->renameColumn('last_changed', 'last_changed2')->save();
$this->assertFalse($this->adapter->hasColumn('t', 'last_changed'));
$this->assertTrue($this->adapter->hasColumn('t', 'last_changed2'));
}

public function testRenamingANonExistentColumn()
{
$table = new Table('t', [], $this->adapter);
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ private function usingPostgres10(): bool
return version_compare($version, '10.0.0', '>=');
}

public function testAdapterType()
{
$this->assertEquals('pgsql', $this->adapter->getAdapterType());
}

public function testConnection()
{
$this->assertInstanceOf(Connection::class, $this->adapter->getConnection());
Expand Down
39 changes: 38 additions & 1 deletion tests/TestCase/Migration/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function testExecutingAMigrationWithTransactions()
$adapterStub->expects($this->once())
->method('commitTransaction');

$adapterStub->expects($this->exactly(2))
$adapterStub->expects($this->exactly(1))
->method('hasTransactions')
->willReturn(true);

Expand All @@ -194,6 +194,43 @@ public function up(): void
$this->assertTrue($migration->executed);
}

public function testExecutingAMigrationWithUseTransactions()
{
// stub adapter
$adapterStub = $this->getMockBuilder(PdoAdapter::class)
->setConstructorArgs([[]])
->getMock();
$adapterStub->expects($this->never())
->method('beginTransaction');

$adapterStub->expects($this->never())
->method('commitTransaction');

$adapterStub->expects($this->exactly(1))
->method('hasTransactions')
->willReturn(true);

$this->environment->setAdapter($adapterStub);

// migrate
$migration = new class ('mockenv', 20110301080000) extends AbstractMigration {
public bool $executed = false;

public function useTransactions(): bool
{
return false;
}

public function up(): void
{
$this->executed = true;
}
};

$this->environment->executeMigration($migration, MigrationInterface::UP);
$this->assertTrue($migration->executed);
}

public function testExecutingAChangeMigrationUp()
{
// stub adapter
Expand Down

0 comments on commit 2b2890a

Please sign in to comment.