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

Bug: Migration in module (different namespace) do not find migrations #4348

Closed
nicojmb opened this issue Feb 25, 2021 · 1 comment
Closed

Comments

@nicojmb
Copy link

nicojmb commented Feb 25, 2021

CI: 4.1.1 (composer)

Hi, i've create a module and add it to PSR4 autoload, the module works fine!

The problem is that i've a migration inside a module and run $migrate->latests(); not create de tables, i call the migration with this code from the web UI:

<?php

namespace App\Controllers;

class Migrate extends BaseController
{
	public function index()
	{
		echo 'Migrate';

		$migrate = \Config\Services::migrations();

		try
		{
			$migrate->latest();
		}
		catch (\Throwable $e)
		{
			print_r($e);
		}
	}
}

I see in MigrationRunner.php than the function findMigrations() search for the migration files, but exists a condition that makes me dubt:

// If a namespace is set then use it, otherwise load all namespaces from the autoloader
$namespaces = $this->namespace ? [$this->namespace] : array_keys(Services::autoloader()->getNamespace());

If i see MigrationRunner.php constructor, always set the $this->namespace:

/**
	 * Constructor.
	 *
	 * When passing in $db, you may pass any of the following to connect:
	 * - group name
	 * - existing connection instance
	 * - array of database configuration values
	 *
	 * @param MigrationsConfig                      $config
	 * @param ConnectionInterface|array|string|null $db
	 *
	 * @throws ConfigException
	 */
	public function __construct(MigrationsConfig $config, $db = null)
	{
		$this->enabled = $config->enabled ?? false;
		$this->table   = $config->table ?? 'migrations';

		// Default name space is the app namespace
		$this->namespace = APP_NAMESPACE;

		// get default database group
		$config      = config('Database');
		$this->group = $config->defaultGroup;
		unset($config);

		// If no db connection passed in, use
		// default database group.
		$this->db = db_connect($db);
	}

and of course, the namespaces founded by FindMigrations es this:

Array ( [0] => App )

if i comment this condition get the correct array:

Array ( [0] => CodeIgniter [1] => App [2] => Config [3] => Modules\Auth [4] => Translations [5] => Psr\Log [6] => Laminas\ZendFrameworkBridge [7] => Laminas\Escaper [8] => Kint [9] => Fluent\Auth )

what i'm doing wrong? how i get the correct migrations?

Regards!

@nicojmb nicojmb added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 25, 2021
@paulbalandan
Copy link
Member

Try this:

<?php

namespace App\Controllers;

class Migrate extends BaseController
{
	public function index()
	{
		echo 'Migrate';

		$migrate = \Config\Services::migrations();

		try
		{
			$migrate->setNamespace(null)->latest();
		}
		catch (\Throwable $e)
		{
			print_r($e);
		}
	}
}

Setting the namespace to null will force migrations to search the Autoloader for namespaces.

@paulbalandan paulbalandan removed the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants