Skip to content

Commit 9ae0b13

Browse files
committed
Migrations will now be found in all defined namespace folders.
1 parent 610e71c commit 9ae0b13

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

system/Database/MigrationRunner.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
use CodeIgniter\Config\BaseConfig;
4040
use CodeIgniter\ConfigException;
41+
use Config\Autoload;
4142

4243
/**
4344
* Class MigrationRunner
@@ -115,7 +116,7 @@ public function __construct(BaseConfig $config, ConnectionInterface $db = null)
115116
$this->type = $config->type ?? 'timestamp';
116117
$this->table = $config->table ?? 'migrations';
117118
$this->currentVersion = $config->currentVersion ?? 0;
118-
$this->path = $config->path ?? APPPATH.'Database/Migrations/';
119+
$this->path = $config->path ?? 'Database/Migrations/';
119120

120121
$this->path = rtrim($this->path, '/').'/';
121122

@@ -166,15 +167,6 @@ public function version(string $targetVersion, $group='default')
166167
// Note: We use strings, so that timestamp versions work on 32-bit systems
167168
$currentVersion = $this->getVersion($group);
168169

169-
if ($this->type === 'sequential')
170-
{
171-
$targetVersion = sprintf('%03d', $targetVersion);
172-
}
173-
else
174-
{
175-
$targetVersion = (string)$targetVersion;
176-
}
177-
178170
$migrations = $this->findMigrations();
179171

180172
if ($targetVersion > 0 && ! isset($migrations[$targetVersion]))
@@ -263,11 +255,11 @@ public function latest()
263255
throw new \RuntimeException(lang('Migrations.migNotFound'));
264256
}
265257

266-
$lastMigration = basename(end($migrations));
258+
$lastMigration = basename(end($migrations), '.php');
267259

268260
// Calculate the last migration step from existing migration
269261
// filenames and proceed to the standard version migration
270-
return $this->version($this->getMigrationNumber($lastMigration));
262+
return $this->version($lastMigration);
271263
}
272264

273265
//--------------------------------------------------------------------
@@ -293,25 +285,31 @@ public function findMigrations()
293285
{
294286
$migrations = [];
295287

296-
// Load all *_*.php files in the migrations path
297-
foreach (glob($this->path.'*_*.php') as $file)
298-
{
299-
$name = basename($file, '.php');
300-
301-
// Filter out non-migration files
302-
if (preg_match($this->regex, $name))
303-
{
304-
$number = $this->getMigrationNumber($name);
305-
306-
// There cannot be duplicate migration numbers
307-
if (isset($migrations[$number]))
308-
{
309-
throw new \RuntimeException(lang('Migrations.migMultiple').$number);
310-
}
311-
312-
$migrations[$number] = $file;
313-
}
314-
}
288+
$config = new Autoload();
289+
290+
// Loop through all of our namespaced folders
291+
// searching for migration directories.
292+
foreach ($config->psr4 as $namespace => $dir)
293+
{
294+
$dir = rtrim($dir, '/').'/'.$this->path;
295+
296+
if (! is_dir($dir))
297+
{
298+
continue;
299+
}
300+
301+
// Load all *_*.php files in the migrations path
302+
foreach (glob($dir.'*_*.php') as $file)
303+
{
304+
$name = basename($file, '.php');
305+
306+
// Filter out non-migration files
307+
if (preg_match($this->regex, $name))
308+
{
309+
$migrations[$name] = $file;
310+
}
311+
}
312+
}
315313

316314
ksort($migrations);
317315

user_guide_src/source/database/migration.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ match the name of the database group exactly::
124124
public function down() { . . . }
125125
}
126126

127+
Namespaces
128+
==========
129+
130+
The migration library will automatically scan all namespaces you have defined within
131+
**application/Config/Autoload.php** and its ``$psr4`` property for matching directory
132+
names. It will include all migrations it finds.
133+
134+
For example, assume that we have the the following namespaces defined in our Autoload
135+
configuration file::
136+
137+
$psr4 = [
138+
'App' => APPPATH,
139+
'MyCompany' => ROOTPATH.'MyCompany'
140+
];
141+
142+
This will look for any migrations located at both **APPPATH/Database/Migrations** and
143+
**ROOTPATH/Database/Migrations**. This makes it simple to include migrations in your
144+
re-usable, modular code suites.
145+
127146
*************
128147
Usage Example
129148
*************
@@ -225,7 +244,7 @@ The following is a table of all the config options for migrations, available in
225244
Preference Default Options Description
226245
========================== ====================== ========================== =============================================================
227246
**enabled** FALSE TRUE / FALSE Enable or disable migrations.
228-
**path** APPPATH.'migrations/' None The path to your migrations folder.
247+
**path** 'Database/Migrations/' None The path to your migrations folder.
229248
**currentVersion** 0 None The current version your database should use.
230249
**table** migrations None The table name for storing the schema version number.
231250
**type** 'timestamp' 'timestamp' / 'sequential' The type of numeric identifier used to name migration files.

0 commit comments

Comments
 (0)