如何将Laravel迁移转换为原始SQL脚本? [英] How to convert Laravel migrations to raw SQL scripts?

查看:227
本文介绍了如何将Laravel迁移转换为原始SQL脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们团队的开发人员真的已经习惯了Laravel迁移的功能,他们在本地计算机和我们的开发服务器上工作得非常好. 但是客户的数据库管理员将不接受Laravel迁移.他要求为我们的应用程序的每个新版本提供原始SQL脚本.

Developers of my team are really used to the power of Laravel migrations, they are working great on local machines and our dev servers. But customer's database admin will not accept Laravel migrations. He asks for raw SQL scripts for each new version of our application.

是否有任何工具或编程技术来捕获Laravel向上/向下SQL脚本迁移的输出?

如果在创建生产版本时将SQL脚本生成集成到CI系统(TeamCity)中,那将是完美的.

It would be perfect if we could integrate SQL script generation in our CI system (TeamCity) when creating production builds.

顺便说一下,我们将为此项目使用Laravel 5和PostgreSQL.

By the way, we will be using Laravel 5 and PostgreSQL for this project.

推荐答案

使用迁移命令

您可以在运行php artisan migrate时添加--pretend标志,以将查询输出到终端:

Use the migrate command

You can add the --pretend flag when you run php artisan migrate to output the queries to the terminal:

php artisan migrate --pretend

这看起来像这样:

Migration table created successfully.
CreateUsersTable: create table "users" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar null, "created_at" datetime not null, "updated_at" datetime not null)
CreateUsersTable: create unique index users_email_unique on "users" ("email")
CreatePasswordResetsTable: create table "password_resets" ("email" varchar not null, "token" varchar not null, "created_at" datetime not null)
CreatePasswordResetsTable: create index password_resets_email_index on "password_resets" ("email")
CreatePasswordResetsTable: create index password_resets_token_index on "password_resets" ("token")

要将其保存到文件中,只需重定向输出,而无需ansi :

To save this to a file, just redirect the output without ansi:

php artisan migrate --pretend --no-ansi > migrate.sql

此命令仅包括尚未迁移的迁移.

This command only include the migrations that hasn't been migrated yet.


接收迁移命令

要进一步自定义获取查询的方式,请考虑破解源代码并制作自己的自定义命令或类似的命令.为了帮助您入门,这里有一些快速代码可以帮助您完成所有迁移.


Hack the migrate command

To further customize how to get the queries, consider hacking the source and make your own custom command or something like that. To get you started, here is some quick code to get all the migrations.

$migrator = app('migrator');
$db = $migrator->resolveConnection(null);
$migrations = $migrator->getMigrationFiles('database/migrations');
$queries = [];

foreach($migrations as $migration) {
    $migration_name = $migration;
    $migration = $migrator->resolve($migration);

    $queries[] = [
        'name' => $migration_name,
        'queries' => array_column($db->pretend(function() use ($migration) { $migration->up(); }), 'query'),
    ];
}

dd($queries);

示例输出

array:2 [
  0 => array:2 [
    "name" => "2014_10_12_000000_create_users_table"
    "queries" => array:2 [
      0 => "create table "users" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar null, "created_at" datetime not null, "updated_at" datetime not null)"
      1 => "create unique index users_email_unique on "users" ("email")"
    ]
  ]
  1 => array:2 [
    "name" => "2014_10_12_100000_create_password_resets_table"
    "queries" => array:3 [
      0 => "create table "password_resets" ("email" varchar not null, "token" varchar not null, "created_at" datetime not null)"
      1 => "create index password_resets_email_index on "password_resets" ("email")"
      2 => "create index password_resets_token_index on "password_resets" ("token")"
    ]
  ]
]

此代码将包括所有迁移.要了解如何仅获取尚未迁移的内容,请查看vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php中的run()方法.

This code will include all the migrations. To see how to only get what isn't already migrated take a look at the run() method in vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php.

这篇关于如何将Laravel迁移转换为原始SQL脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆