bennett-treptow/laravel-migration-generator

从现有数据库结构生成迁移

4.4.1 2024-04-30 22:54 UTC

README

Latest Version on Packagist

从现有数据库结构生成迁移,是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