Skip to content
Merged
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
51 changes: 23 additions & 28 deletions lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public function getApp(): string {
}

/**
* @codeCoverageIgnore - this will implicitly tested on installation
* Ensures the `migrations` table exists with the expected schema.
*
* Creates the table if missing, or recreates it if the existing one is incompatible.
*
* @return bool True if the table was created or recreated; false otherwise.
* @codeCoverageIgnore This is exercised implicitly during installation.
*/
private function createMigrationTable(): bool {
if ($this->migrationTableCreated) {
Expand All @@ -109,8 +114,9 @@ private function createMigrationTable(): bool {
$schema = new SchemaWrapper($this->connection);

/**
* We drop the table when it has different columns or the definition does not
* match. E.g. ownCloud uses a length of 177 for app and 14 for version.
* Recreate the `migrations` table when the existing schema is incompatible.
* For example, older ownCloud installations used shorter column lengths
* for `app` (177) and `version` (14).
*/
try {
$table = $schema->getTable('migrations');
Expand All @@ -131,19 +137,19 @@ private function createMigrationTable(): bool {
}

if (!$schemaMismatch) {
// Table exists and schema matches: return back!
// The existing table matches the expected schema; nothing to do.
$this->migrationTableCreated = true;
return false;
}
}

// Drop the table, when it didn't match our expectations.
// Drop the table because it does not match the expected schema.
$this->connection->dropTable('migrations');

// Recreate the schema after the table was dropped.
// Recreate the schema wrapper after dropping the table.
$schema = new SchemaWrapper($this->connection);
} catch (SchemaException $e) {
// Table not found, no need to panic, we will create it.
// The table does not exist; it will be created below.
}

$table = $schema->createTable('migrations');
Expand All @@ -164,14 +170,13 @@ private function createMigrationTable(): bool {
* @return list<string>
* @codeCoverageIgnore - no need to test this
*/
public function getMigratedVersions() {
public function getMigratedVersions(): array {
$this->createMigrationTable();
$qb = $this->connection->getQueryBuilder();

$qb->select('version')
->from('migrations')
->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp())))
->orderBy('version');
->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp())));

$result = $qb->executeQuery();
$rows = $result->fetchAll(\PDO::FETCH_COLUMN);
Expand Down Expand Up @@ -247,7 +252,7 @@ protected function findMigrations(): array {
* @param string $to
* @return string[]
*/
private function getMigrationsToExecute($to) {
private function getMigrationsToExecute($to): array {
$knownMigrations = $this->getMigratedVersions();
$availableMigrations = $this->getAvailableVersions();

Expand All @@ -269,7 +274,7 @@ private function getMigrationsToExecute($to) {
* @param string[] $knownMigrations
* @return bool
*/
private function shallBeExecuted($m, $knownMigrations) {
private function shallBeExecuted($m, $knownMigrations): bool {
if (in_array($m, $knownMigrations)) {
return false;
}
Expand All @@ -289,28 +294,22 @@ private function markAsExecuted($version): void {

/**
* Returns the name of the table which holds the already applied versions
*
* @return string
*/
public function getMigrationsTableName() {
public function getMigrationsTableName(): string {
return $this->connection->getPrefix() . 'migrations';
}

/**
* Returns the namespace of the version classes
*
* @return string
*/
public function getMigrationsNamespace() {
public function getMigrationsNamespace(): string {
return $this->migrationsNamespace;
}

/**
* Returns the directory which holds the versions
*
* @return string
*/
public function getMigrationsDirectory() {
public function getMigrationsDirectory(): string {
return $this->migrationsPath;
}

Expand Down Expand Up @@ -452,7 +451,7 @@ public function migrateSchemaOnly(string $to = 'latest'): void {
* @param string $to
* @return string[] [$name => $description]
*/
public function describeMigrationStep($to = 'latest') {
public function describeMigrationStep(string $to = 'latest'): array {
$toBeExecuted = $this->getMigrationsToExecute($to);
$description = [];
foreach ($toBeExecuted as $version) {
Expand All @@ -465,11 +464,9 @@ public function describeMigrationStep($to = 'latest') {
}

/**
* @param string $version
* @return IMigrationStep
* @throws \InvalidArgumentException
*/
public function createInstance($version) {
public function createInstance(string $version): IMigrationStep {
/** @psalm-var class-string<IMigrationStep> $class */
$class = $this->getClass($version);
try {
Expand All @@ -491,11 +488,9 @@ public function createInstance($version) {
/**
* Executes one explicit version
*
* @param string $version
* @param bool $schemaOnly
* @throws \InvalidArgumentException
*/
public function executeStep($version, $schemaOnly = false): void {
public function executeStep(string $version, bool $schemaOnly = false): void {
$instance = $this->createInstance($version);

if (!$schemaOnly) {
Expand Down
Loading