callcocam/tall-migration

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

dev-main 2023-10-20 17:14 UTC

This package is auto-updated.

Last update: 2024-09-20 19:40:21 UTC


README

安装

composer require --dev callcocam/tall-migration
php artisan vendor:publish --provider="Tall\Migration\TallMigratioProvider"

Lumen 安装

composer require --dev callcocam/tall-migration

将配置文件从 vendor/callcocam/tall-migration/config 复制到你的 Lumen 配置文件夹

在 bootstrap/app.php 中注册服务提供者

$app->register(\Tall\Migration\TallMigratioProvider::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

可以通过设置环境变量 TALL_RUN_AFTER_MIGRATIONStrue 并按正常方式运行迁移来执行此命令。这将钩入 MigrationsEnded 事件并使用通过环境变量指定的默认选项运行此命令。注意:它只有在你的应用环境设置为 local 时才会运行。

配置

想要自定义迁移模板?确保你已经使用 artisan 命令发布了供应商资产以在上方发布供应商文件。

环境变量

模板

默认模板在 resources/stubs/vendor/tall-migration/ 中,用于表和视图。每个数据库驱动都可以通过在 resources/stubs/vendor/tall-migration/ 中创建一个以 driver-前缀的新模板文件来分配特定的迁移模板,例如,为 MySQL 特定表模板创建 mysql-table.stub

模板命名

表和视图模板可以使用 TALL_(TABLE|VIEW)_NAMING_SCHEME 环境变量命名。也可以使用特定于驱动程序的命名方案,通过指定 TALL_{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

将创建一个具有以下 Blueprint 的 tests/database/migrations/[TIMESTAMP]_create_users_table.php

<?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 目前支持创建迁移 from。创建的迁移将遵循 Laravel 迁移允许的数据库驱动程序。

  • MySQL
  • Postgres
  • SQLite
  • SQL Server