bennett-treptow / laravel-migration-generator
从现有数据库结构生成迁移
Requires
- php: ^7.4|~8.0
- illuminate/config: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/console: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- marcj/topsort: ^2.0
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^6.17|^8.0|^9.0
- dev-main
- 4.4.1
- 4.4.0
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1
- 2.0
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0
- 1.0-beta
This package is auto-updated.
Last update: 2024-08-30 23:39:31 UTC
README
从现有数据库结构生成迁移,是Laravel提供的模式转储的替代方案。此包的主要用途之一是为具有许多使用 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 命令发布了 vendor 资产以发布上面的 vendor 文件。
环境变量
占位符
在 resources/stubs/vendor/laravel-migration-generator/
中有一个默认的表和视图占位符,可以针对每个数据库驱动程序分配特定的迁移占位符,通过在 resources/stubs/vendor/laravel-migration-generator/
中创建一个以 driver
-前缀的新占位符文件,例如为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