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

Fix MigrateController::$migrationPathMap #6693

Merged
merged 2 commits into from
Dec 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HumHub Changelog

1.16.0 (Unreleased)
-------------------
- Fix #6693: `MigrateController::$migrationPathMap` stored rolling sum of migrations
- Enh #6697: Make state badge customizable
- Fix #6636: Module Manager test
- Enh #6530: Small performance improvements
Expand Down
1 change: 1 addition & 0 deletions MIGRATE-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Version 1.15
- Permission to configure modules is now restricted to users allowed to manage settings (was previously restricted to users allowed to manage modules). [More info here](https://github.com/humhub/humhub/issues/6174).

### Type restrictions
- `\humhub\commands\MigrateController` enforces types on fields, method parameters, & return types
- `\humhub\libs\BaseSettingsManager` and its child classes on fields, method parameters, & return types
- `\humhub\libs\Helpers::checkClassType()` (see [#6548](https://github.com/humhub/humhub/pull/6548))
- rather than throwing a `\yii\base\Exception`, it now throws some variations of `yii\base\InvalidArgumentException`
Expand Down
65 changes: 38 additions & 27 deletions protected/humhub/commands/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use humhub\helpers\DatabaseHelper;
use Yii;
use yii\console\Exception;
use yii\db\MigrationInterface;
use yii\web\Application;

/**
Expand Down Expand Up @@ -50,10 +51,12 @@
* yii migrate/down
* ~~~
*
*
* @property-read string[] $newMigrations
* @property-read string[] $migrationPaths
*/
class MigrateController extends \yii\console\controllers\MigrateController
{

/**
* @var string the directory storing the migration classes. This can be either
* a path alias or a directory.
Expand All @@ -63,21 +66,21 @@ class MigrateController extends \yii\console\controllers\MigrateController
/**
* @var boolean also include migration paths of all enabled modules
*/
public $includeModuleMigrations = false;
public bool $includeModuleMigrations = false;

/**
* When includeModuleMigrations is enabled, this maps migrations to the
* corresponding module.
*
* @var array
*/
protected $migrationPathMap = [];
protected array $migrationPathMap = [];


/**
* @inerhitdoc
*/
public function beforeAction($action)
public function beforeAction($action): bool
{
// Make sure to define a default table storage engine
$db = Yii::$app->db;
Expand All @@ -98,9 +101,9 @@ public function beforeAction($action)
/**
* @inheritdoc
*/
public function options($actionID)
public function options($actionID): array
{
if ($actionID == 'up') {
if ($actionID === 'up') {
return array_merge(parent::options($actionID), ['includeModuleMigrations']);
}

Expand All @@ -109,9 +112,10 @@ public function options($actionID)

/**
* Returns the migrations that are not applied.
* @return array list of new migrations
*
* @return string[] list of new migrations
*/
protected function getNewMigrations()
protected function getNewMigrations(): array
{
if (!$this->includeModuleMigrations) {
return parent::getNewMigrations();
Expand All @@ -121,8 +125,9 @@ protected function getNewMigrations()
$migrations = [];
foreach ($this->getMigrationPaths() as $migrationPath) {
$this->migrationPath = $migrationPath;
$migrations = array_merge($migrations, parent::getNewMigrations());
$this->migrationPathMap[$migrationPath] = $migrations;
$newMigrations = parent::getNewMigrations();
$migrations = array_unique(array_merge($migrations, $newMigrations));
$this->migrationPathMap[$migrationPath] = $newMigrations;
}

sort($migrations);
Expand All @@ -132,10 +137,12 @@ protected function getNewMigrations()

/**
* Creates a new migration instance.
*
* @param string $class the migration class name
* @return \yii\db\MigrationInterface the migration instance
*
* @return MigrationInterface the migration instance
*/
protected function createMigration($class)
protected function createMigration($class): MigrationInterface
{
if ($this->includeModuleMigrations) {
$this->migrationPath = $this->getMigrationPath($class);
Expand All @@ -148,14 +155,15 @@ protected function createMigration($class)
* Returns the migration path of a given migration.
* A map containing the path=>migration will be created by getNewMigrations method.
*
* @param type $migration
* @return type
* @throws \yii\console\Exception
* @param string $migration
*
* @return string
* @throws Exception
*/
public function getMigrationPath($migration)
public function getMigrationPath(string $migration): string
{
foreach ($this->migrationPathMap as $path => $migrations) {
if (in_array($migration, $migrations)) {
if (in_array($migration, $migrations, true)) {
return $path;
}
}
Expand All @@ -166,9 +174,9 @@ public function getMigrationPath($migration)
/**
* Returns the migration paths of all enabled modules
*
* @return array
* @return string[]
*/
protected function getMigrationPaths()
protected function getMigrationPaths(): array
{
$migrationPaths = ['base' => $this->migrationPath];
foreach (Yii::$app->getModules() as $id => $config) {
Expand Down Expand Up @@ -196,7 +204,7 @@ protected function getMigrationPaths()
*
* @return string output
*/
public static function webMigrateAll()
public static function webMigrateAll(): string
{
ob_start();
$controller = new self('migrate', Yii::$app);
Expand All @@ -206,16 +214,17 @@ public static function webMigrateAll()
$controller->color = false;
$controller->runAction('up');

return ob_get_clean();
return ob_get_clean() ?: '';
}

/**
* Executes migrations in a specific folder
*
* @param string $migrationPath
*
* @return string output
*/
public static function webMigrateUp($migrationPath)
public static function webMigrateUp(string $migrationPath): string
{
ob_start();
$controller = new self('migrate', Yii::$app);
Expand All @@ -225,7 +234,7 @@ public static function webMigrateUp($migrationPath)
$controller->color = false;
$controller->runAction('up');

return ob_get_clean();
return ob_get_clean() ?: '';
}

/**
Expand All @@ -235,9 +244,10 @@ public function stdout($string)
{
if (Yii::$app instanceof Application) {
print $string;
} else {
return parent::stdout($string);
return strlen($string);
}

return parent::stdout($string);
}

/**
Expand All @@ -247,8 +257,9 @@ public function stderr($string)
{
if (Yii::$app instanceof Application) {
print $string;
} else {
return parent::stderr($string);
return strlen($string);
}

return parent::stderr($string);
}
}