Skip to content
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
111 changes: 20 additions & 91 deletions src/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Cake\Database\Schema\SchemaDialect;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Exception;
use InvalidArgumentException;
use Migrations\Config\Config;
use Migrations\Db\Action\AddColumn;
Expand All @@ -37,7 +36,6 @@
use Migrations\Db\AlterInstructions;
use Migrations\Db\InsertMode;
use Migrations\Db\Literal;
use Migrations\Db\Table;
use Migrations\Db\Table\CheckConstraint;
use Migrations\Db\Table\Column;
use Migrations\Db\Table\ForeignKey;
Expand Down Expand Up @@ -129,21 +127,7 @@ public function setConnection(Connection $connection): AdapterInterface
if (!$this->hasTable($this->getSchemaTableName())) {
$this->createSchemaTable();
} else {
$table = new Table($this->getSchemaTableName(), [], $this);
if (!$table->hasColumn('migration_name')) {
$table
->addColumn(
'migration_name',
'string',
['limit' => 100, 'after' => 'version', 'default' => null, 'null' => true],
)
->save();
}
if (!$table->hasColumn('breakpoint')) {
$table
->addColumn('breakpoint', 'boolean', ['default' => false, 'null' => false])
->save();
}
$this->migrationsTable()->upgradeTable();
}

return $this;
Expand Down Expand Up @@ -357,26 +341,7 @@ public function hasColumn(string $tableName, string $columnName): bool
*/
public function createSchemaTable(): void
{
try {
$options = [
'id' => false,
'primary_key' => 'version',
];

$table = new Table($this->getSchemaTableName(), $options, $this);
$table->addColumn('version', 'biginteger', ['null' => false])
->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
->addColumn('breakpoint', 'boolean', ['default' => false, 'null' => false])
->save();
} catch (Exception $exception) {
throw new InvalidArgumentException(
'There was a problem creating the schema table: ' . $exception->getMessage(),
(int)$exception->getCode(),
$exception,
);
}
$this->migrationsTable()->createTable();
}

/**
Expand Down Expand Up @@ -815,6 +780,18 @@ public function getVersions(): array
return array_keys($rows);
}

/**
* Get the migrations table storage implementation.
*
* @return \Migrations\Db\Adapter\MigrationsTableStorage
* @internal
*/
protected function migrationsTable(): MigrationsTableStorage
{
// TODO Use configure/auto-detect which implmentation to use.
return new MigrationsTableStorage($this, $this->getSchemaTableName());
}

/**
* {@inheritDoc}
*
Expand All @@ -832,10 +809,7 @@ public function getVersionLog(): array
default:
throw new RuntimeException('Invalid version_order configuration option');
}
$query = $this->getSelectBuilder();
$query->select('*')
->from($this->getSchemaTableName())
->orderBy($orderBy);
$query = $this->migrationsTable()->getVersions($orderBy);

// This will throw an exception if doing a --dry-run without any migrations as phinxlog
// does not exist, so in that case, we can just expect to trivially return empty set
Expand All @@ -862,24 +836,10 @@ public function getVersionLog(): array
public function migrated(MigrationInterface $migration, string $direction, string $startTime, string $endTime): AdapterInterface
{
if (strcasecmp($direction, MigrationInterface::UP) === 0) {
$query = $this->getInsertBuilder();
$query->insert(['version', 'migration_name', 'start_time', 'end_time', 'breakpoint'])
->into($this->getSchemaTableName())
->values([
'version' => (string)$migration->getVersion(),
'migration_name' => substr($migration->getName(), 0, 100),
'start_time' => $startTime,
'end_time' => $endTime,
'breakpoint' => 0,
]);
$this->executeQuery($query);
$this->migrationsTable()->recordUp($migration, $startTime, $endTime);
} else {
// down
$query = $this->getDeleteBuilder();
$query->delete()
->from($this->getSchemaTableName())
->where(['version' => $migration->getVersion()]);
$this->executeQuery($query);
$this->migrationsTable()->recordDown($migration);
}

return $this;
Expand All @@ -890,19 +850,7 @@ public function migrated(MigrationInterface $migration, string $direction, strin
*/
public function toggleBreakpoint(MigrationInterface $migration): AdapterInterface
{
$params = [
$migration->getVersion(),
];
$this->query(
sprintf(
'UPDATE %1$s SET %2$s = CASE %2$s WHEN true THEN false ELSE true END, %4$s = %4$s WHERE %3$s = ?;',
$this->quoteTableName($this->getSchemaTableName()),
$this->quoteColumnName('breakpoint'),
$this->quoteColumnName('version'),
$this->quoteColumnName('start_time'),
),
$params,
);
$this->migrationsTable()->toggleBreakpoint($migration);

return $this;
}
Expand All @@ -912,17 +860,7 @@ public function toggleBreakpoint(MigrationInterface $migration): AdapterInterfac
*/
public function resetAllBreakpoints(): int
{
$query = $this->getUpdateBuilder();
$query->update($this->getSchemaTableName())
->set([
'breakpoint' => 0,
'start_time' => $query->identifier('start_time'),
])
->where([
'breakpoint !=' => 0,
]);

return $this->executeQuery($query);
return $this->migrationsTable()->resetAllBreakpoints();
}

/**
Expand Down Expand Up @@ -954,16 +892,7 @@ public function unsetBreakpoint(MigrationInterface $migration): AdapterInterface
*/
protected function markBreakpoint(MigrationInterface $migration, bool $state): AdapterInterface
{
$query = $this->getUpdateBuilder();
$query->update($this->getSchemaTableName())
->set([
'breakpoint' => (int)$state,
'start_time' => $query->identifier('start_time'),
])
->where([
'version' => $migration->getVersion(),
]);
$this->executeQuery($query);
$this->migrationsTable()->markBreakpoint($migration, $state);

return $this;
}
Expand Down
Loading
Loading