我团队的开发人员真的已经习惯了Laravel迁移的功能,他们在本地计算机和我们的开发服务器上工作得非常好。但是客户的数据库管理员将不接受Laravel迁移。他要求为我们的应用程序的每个新版本提供原始SQL脚本。
是否有任何工具或编程技术可捕获从Laravel迁移到向上/向下SQL脚本的输出?
如果在创建生产版本时将SQL脚本生成集成到CI系统(TeamCity)中,那将是完美的。
顺便说一下,我们将在该项目中使用Laravel 5和PostgreSQL。
您可以--pretend在运行php artisan migrate以将查询输出到终端时添加标志:
--pretend
php artisan migrate
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的情况下 重定向输出 即可 :
php artisan migrate --pretend --no-ansi > migrate.sql
该命令仅包括尚未迁移的迁移。
要进一步自定义获取查询的方式,请考虑破解源代码并制作自己的自定义命令或类似的命令。为了让您开始使用,这里有一些快速代码来进行所有迁移。
$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")" ] ] ]
此代码将包括 所有 迁移。要了解如何仅获取尚未迁移的内容,请查看中的run()方法vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php。
run()
vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php