itsmill3rtime / laravel-migration-generator
从现有数据库结构生成迁移
Requires
- php: ^7.4|^8.0
- illuminate/config: ^6.0|^7.0|^8.0
- illuminate/console: ^6.0|^7.0|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- marcj/topsort: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^6.17
This package is auto-updated.
Last update: 2024-09-11 23:20:52 UTC
README
从现有数据库结构生成迁移,是Laravel提供的schema dump的替代方案。本包的主要用例是处理包含许多使用 doctrine/dbal 的 ->change() 方法来修改表的项目,而 SQLite 不支持这种方法,并需要一种方法来更新表结构以便在测试中使用 SQLite。另一个用例是将一个具有数据库但没有迁移的项目转换为基本迁移。
安装
composer require --dev bennett-treptow/laravel-migration-generator
php artisan vendor:publish --provider="LaravelMigrationGenerator\LaravelMigrationGeneratorProvider"
Lumen 安装
composer require --dev bennett-treptow/laravel-migration-generator
将配置文件从 vendor/bennett-treptow/laravel-migration-generator/config 复制到您的 Lumen 配置文件夹
在 bootstrap/app.php 中注册服务提供者
$app->register(\LaravelMigrationGenerator\LaravelMigrationGeneratorProvider::class);
用法
每次您有数据库更改或准备好将数据库结构压缩成迁移时,运行
php artisan generate:migrations
默认情况下,迁移将创建在 tests/database/migrations。您可以使用 --path 选项指定不同的路径
php artisan generate:migrations --path=database/migrations
您可以使用 --connection 选项指定要使用的连接
php artisan generate:migrations --connection=mysql2
您还可以使用 --empty-path 选项清空目录
php artisan generate:migrations --empty-path
您可以通过设置环境变量 LMG_RUN_AFTER_MIGRATIONS 为 true 并以正常方式运行迁移来运行此命令。这将捕获 MigrationsEnded 事件并使用通过环境变量指定的默认选项运行此命令。注意:它仅在您的应用程序环境设置为 local 时运行。
配置
想要自定义迁移占位符吗?请确保您已使用 artisan 命令发布了供应商资产以发布上述供应商文件。
环境变量
占位符
默认的表和视图占位符位于 resources/stubs/vendor/laravel-migration-generator/。可以创建一个新占位符文件来为每个数据库驱动程序分配特定的迁移占位符,例如,对于特定的 MySQL 表占位符,可以创建 mysql-table.stub。
占位符命名
表和视图占位符可以使用 LMG_(TABLE|VIEW)_NAMING_SCHEME 环境变量进行命名。还可以使用驱动程序特定的命名方案,通过指定使用相同标记的 LMG_{driver}_TABLE_NAMING_SCHEME 环境变量。以下是可以替换的可用标记。
表名称占位符标记
表占位符有以下可用的命名方案标记
表架构占位符标记
表架构占位符有以下可用的标记
视图名称占位符标记
视图占位符有以下可用的命名方案标记
视图架构占位符标记
视图架构占位符有以下可用的标记
示例用法
给定一个 users 表的数据库结构
CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `first_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `last_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `timezone` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'America/New_York', `location_id` int(10) unsigned NOT NULL, `deleted_at` timestamp NULL DEFAULT NULL, `remember_token` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `users_username_index` (`username`), KEY `users_first_name_index` (`first_name`), KEY `users_last_name_index` (`last_name`), KEY `users_email_index` (`email`), KEY `fk_users_location_id_index` (`location_id`) CONSTRAINT `users_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
将创建一个 tests/database/migrations/[TIMESTAMP]_create_users_table.php,其中包含以下 Blueprint
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('username', 128)->nullable()->index(); $table->string('email', 255)->index(); $table->string('password', 255); $table->string('first_name', 45)->nullable()->index(); $table->string('last_name', 45)->index(); $table->string('timezone', 45)->default('America/New_York'); $table->unsignedInteger('location_id'); $table->softDeletes(); $table->string('remember_token', 255)->nullable(); $table->timestamps(); $table->foreign('location_id', 'users_location_id_foreign')->references('id')->on('locations')->onUpdate('cascade')->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
当前支持的 DBMS
以下 DBMS 是当前支持的用于创建迁移的数据库管理系统。创建的迁移将遵循 Laravel 迁移允许的数据库驱动程序。
- MySQL
- Postgres
- SQLite
- SQL Server