-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from kitloong/feature/vendor
Ignore vendor migrations
- Loading branch information
Showing
15 changed files
with
493 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
stderr="true" | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
stderr="true" | ||
> | ||
<coverage includeUncoveredFiles="true"> | ||
<coverage includeUncoveredFiles="true"/> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
<exclude> | ||
<directory>src/KitLoong/MigrationsGenerator/Types</directory> | ||
</exclude> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace KitLoong\MigrationsGenerator\Migration\Migrator; | ||
|
||
use Illuminate\Database\Migrations\Migrator as DefaultMigrator; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\File; | ||
use KitLoong\MigrationsGenerator\Support\Regex; | ||
|
||
class Migrator extends DefaultMigrator | ||
{ | ||
/** | ||
* Scan and get vendors' loaded migration table names. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getVendorTableNames(): array | ||
{ | ||
$tables = []; | ||
|
||
// Backup the current DB connection. | ||
$previousConnection = DB::getDefaultConnection(); | ||
|
||
try { | ||
// Create an in-memory SQLite database for the migrations generator. | ||
// Note that no real migrations will be executed, this is simply a precautionary switch to in-memory SQLite. | ||
Config::set('database.connections.lgm_sqlite', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
|
||
DB::setDefaultConnection('lgm_sqlite'); | ||
|
||
$vendorPaths = app('migrator')->paths(); | ||
|
||
foreach ($vendorPaths as $path) { | ||
$files = File::files($path); | ||
|
||
foreach ($files as $file) { | ||
$queries = $this->getMigrationQueries($file->getPathname()); | ||
|
||
foreach ($queries as $q) { | ||
$matched = Regex::match('/^create table ["|`](.*?)["|`]/', $q['query']); | ||
|
||
if ($matched === '') { | ||
continue; | ||
} | ||
|
||
$tables[] = $matched; | ||
} | ||
} | ||
} | ||
} finally { | ||
// Restore backup DB connection. | ||
DB::setDefaultConnection($previousConnection); | ||
} | ||
|
||
return $tables; | ||
} | ||
|
||
/** | ||
* Resolve migration instance from `$path` and get all of the queries that would be run for a migration. | ||
* | ||
* @return array<int, array{'query': string, 'bindings': array<string, array<mixed>>, 'time': float|null}> | ||
*/ | ||
protected function getMigrationQueries(string $path): array | ||
{ | ||
$migration = $this->resolveMigration($path); | ||
|
||
return $this->getQueries($migration, 'up'); | ||
} | ||
|
||
/** | ||
* Resolve migration instance with backward compatibility. | ||
* | ||
* @return object | ||
*/ | ||
protected function resolveMigration(string $path) | ||
{ | ||
if (method_exists(DefaultMigrator::class, 'resolvePath')) { | ||
return $this->resolvePath($path); | ||
} | ||
|
||
// @codeCoverageIgnoreStart | ||
return $this->resolve( | ||
$this->getMigrationName($path) | ||
); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.