fmoreno-airzone / laravel-migration-generator
从现有数据库结构生成迁移
Requires
- php: ^7.4|^8.0
- illuminate/config: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/console: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/database: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
- marcj/topsort: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16|^3.14
- orchestra/testbench: ^6.17|^8.0
This package is auto-updated.
Last update: 2024-09-26 18:34:29 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/
中找到。可以为每个数据库驱动程序分配特定的迁移模板,通过在 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 服务器