在 Yii 或 Laravel 中从现有数据库生成迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13605767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:26:23  来源:igfitidea点击:

Generating migration from existing database in Yii or Laravel

phpdatabaseyiimigrationlaravel

提问by DamirDiz

I'm working on a project that has a fairly complex database (150+ tables). In order to be able to maintain changes, I've decided to add migrations, preferably using Yii or Laravel.

我正在处理一个具有相当复杂数据库(150 多个表)的项目。为了能够维护更改,我决定添加迁移,最好使用 Yii 或 Laravel。

Does anybody know, if it is possible to generate a initial migration from an existing database?

有人知道,是否可以从现有数据库生成初始迁移?

Creating it by hand would:

手动创建它会:

  • take for ever and
  • be very error-prone.
  • 永远和
  • 非常容易出错。

If there is no way, does anybody know a good PHP-based framework, that supports such functionality?

如果没有办法,有没有人知道一个很好的基于 PHP 的框架,它支持这样的功能?

回答by bmarston

Instructions for accomplishing this in Yii:

在 Yii 中完成此操作的说明:

  1. Add your database connection settings to protected/config/console.php.

  2. Run yiic migrate create initialto create the stub code for the migration.

  3. Copy contents of this gistto protected/commands/InitialDbMigrationCommand.php.

  4. Run yiic initialdbmigration 'name_of_your_database' > initial_migration.phpto generate up()and down()methods for initial database migration.

  5. Copy and paste up()and down()methods from initial_migration.phpto the file created in the protected/migrationsfolder in step 2.

  1. 将您的数据库连接设置添加到protected/config/console.php.

  2. 运行yiic migrate create initial以创建用于迁移的存根代码。

  3. 将此要点的内容复制到protected/commands/InitialDbMigrationCommand.php.

  4. 运行yiic initialdbmigration 'name_of_your_database' > initial_migration.php生成up()down()初始数据库迁移的方法。

  5. 将方法up()down()方法复制并粘贴initial_migration.phpprotected/migrations步骤 2中在文件夹中创建的文件。

回答by SuperDuck

'Doctrine Project' (aka Doctrine) has the ability to create DB migrations for existing DB structures, so you can recreate the existing structure. It can be easily implemented in Symfony, Laravel, also in Yii and many frameworks.

Doctrine Project”(又名 Doctrine)能够为现有数据库结构创建数据库迁移,因此您可以重新创建现有结构。它可以在 Symfony、Laravel、Yii 和许多框架中轻松实现。

Sample from:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations

样本来自:http:
//symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations

From Database

If you have an existing database you can build a set of migration classes that will re-create your database by running the following command.

$ ./symfony doctrine:generate-migrations-db

From Models

If you have an existing set of models you can build a set of migration classes that will create your database by running the following command.

$ ./symfony doctrine:generate-migrations-models

从数据库

如果您有一个现有的数据库,您可以构建一组迁移类,这些类将通过运行以下命令重新创建您的数据库。

$ ./symfony doctrine:generate-migrations-db

从模型

如果您有一组现有模型,您可以构建一组迁移类,通过运行以下命令来创建您的数据库。

$ ./symfony doctrine:generate-migrations-models

回答by Niklas Modess

Well since migration is about setting up your database structure and make changes to it, not to reflect a current database there is no such way.

好吧,由于迁移是关于设置您的数据库结构并对其进行更改,而不是为了反映当前的数据库,因此没有这种方法。

And this is also not a step you have to make. You can start from where you are at the moment, which will make you able to rollback up to this point. Which means you can make migrations for your current tables without having to specify their entire structure, but just the changes only.

而且这也不是您必须执行的步骤。您可以从当前位置开始,这将使您能够回滚到这一点。这意味着您可以对当前表进行迁移,而无需指定它们的整个结构,而只需指定更改即可。

Let's say you have a table called user and want to add their firstname to it.

假设您有一个名为 user 的表,并希望将他们的名字添加到其中。

php artisan migrate:make add_firstname_to_user

Now go into application/migrationsand find the migration file, add this

现在进入application/migrations并找到迁移文件,添加这个

public function up()
{
    Schema::table('user', function($table)
    {
        $table->string('firstname');
    });
}

public function down() {
    Schema::table('user', function($table)
    {
        $table->drop_column('firstname');
    });
}

Now you can add migrate it

现在您可以添加迁移它

php artisan migrate:install // if you haven't run this, should only be once
php artisan migrate

.. and rollback using

.. 和回滚使用

php artisan migrate:rollback

This will add or drop the column firstname, without affecting your table in any other way.

这将添加或删除列 firstname,而不会以任何其他方式影响您的表。

回答by Xethron

Here is a Laravel package I created that does exactly that. It automatically generates clean and accurate Laravel migrations from your existing database.

这是我创建的 Laravel 包,它正是这样做的。它会自动从您现有的数据库中生成干净准确的 Laravel 迁移。

As it doesn't make any assumptions of the database, it should work on any database structure while even keeping the original index and foreign key names.

由于它不对数据库做任何假设,因此它应该适用于任何数据库结构,同时甚至保留原始索引和外键名称。

https://github.com/Xethron/migrations-generator

https://github.com/Xethron/migrations-generator

回答by trejder

As for Yii 1.x, schmunkhas created a wonderful database-commandyiic command.

至于 Yii 1.x,schmunk创造了一个美妙的database-commandyiic 命令。

This command covers only up migrations. You must write your own down migrations.

此命令仅涵盖迁移。你必须写下你自己的迁移

To use it:

要使用它:

  1. Get the newest version from GitHuband put it's contents into /protected/commandsfolder (create one, if it does not exist). Note, that you need to put contents as is(without subfolderfor this particular command), which is contrary to what we do for example for extensions.

  2. Rename EDatabaseCommand.phpfile (and class inside) to DatabaseCommand.php, if you want to use yiic databasecommand (as suggested in docs). Without this fix, you'll have to use yiic edatabasecommand, as there's slight inconsistency between docs and the code(at least in the newest version, as of writing this; maybe schmunkis going to fix this).

  3. Having this, navigate back to protectedfolder in your console and execute yiic database dump migration_name --prefix=table_name.

  1. 从 GitHub获取最新版本并将其内容放入/protected/commands文件夹(创建一个,如果它不存在)。请注意,您需要按原样放置内容(此特定命令没有子文件夹),这与我们为扩展所做的工作相反。

  2. 如果要使用命令(如文档中建议的那样),请将EDatabaseCommand.php文件(和内部的类)重命名为 。如果没有此修复程序,您将不得不使用命令,因为文档和代码之间存在轻微的不一致(至少在最新版本中,在撰写本文时;也许schmunk会修复此问题)。DatabaseCommand.phpyiic databaseyiic edatabase

  3. 有了这个,导航回protected控制台中的文件夹并执行yiic database dump migration_name --prefix=table_name.

This will create a migration protected/runtime/migration_name.phpfile with proper date and time in the beginning of file name, filled with series of CDbMigrationcommands to recreate your database schema. Visit "Usage" section in the docs to read more about customizing command.

这将protected/runtime/migration_name.php在文件名的开头创建一个具有正确日期和时间的迁移文件,其中填充了一系列CDbMigration命令来重新创建您的数据库架构。访问文档中的“用法”部分以阅读有关自定义命令的更多信息。

回答by Yorty Ruiz

I think that the answer is: https://github.com/jamband/yii2-schemadumpfor Yii2 "This command to generate the schema from an existing database."

我认为答案是:https: //github.com/jamband/yii2-schemadumpfor Yii2“此命令从现有数据库生成模式。”

回答by Ponte

There is one now for Yii:

Yii 现在有一个:

This allows a distributed team to easily update the db locally and then distribute it's updates with thee other developers automatically with the rest of the code via a versioning control system (I used git). It also performs a full initial db dump to xml and to a migration file.

这允许分布式团队轻松地在本地更新数据库,然后通过版本控制系统(我使用 git)自动将其更新与其他开发人员以及其余代码一起分发。它还执行完整的初始数据库转储到 xml 和迁移文件。

project home: https://code.google.com/p/yii-automatically-generated-migration-files/

项目主页:https: //code.google.com/p/yii-automatically-generated-migration-files/

source code: https://code.google.com/p/yii-automatically-generated-migration-files/source/checkout

源代码:https: //code.google.com/p/yii-automatically-generated-migration-files/source/checkout

I've created it from scratch as I was annoyed with the fact that I had to do this manually in order to distribute it to my team.

我从头开始创建它,因为我对必须手动执行此操作才能将其分发给我的团队这一事实感到恼火。

Hope it helps!

希望能帮助到你!

Feel free to share bugs, improvements and comments.

随意分享错误、改进和评论。

回答by Mihai P.

I use both Yii and Laravel and I could not find what you require for either of them. They both create empty files and you need to create the migration script yourself. For a table of 150 tables it will be challenge to create the migrations yourself, but it is not quite as hard as you imagine. Because you already have the information on the fields it should not take so long to create.

我同时使用 Yii 和 Laravel,但找不到您需要的任何一个。它们都创建空文件,您需要自己创建迁移脚本。对于一个包含 150 个表的表,自己创建迁移将是一项挑战,但这并不像您想象的那么难。因为您已经拥有有关字段的信息,所以创建它应该不会花很长时间。

回答by Tom

After doing some research, here's what you're going to need for Laravel: https://github.com/XCMer/larry-four-generator

在做了一些研究之后,这里是 Laravel 需要的东西:https: //github.com/XCMer/larry-four-generator

(version 4 at least, who knows how long this will work, Laravel changes too fast and has too many breaking changes)

(至少是第 4 版,谁知道这能用多久,Laravel 变化太快,破坏性变化太多)

You'll want to run php artisan larry:fromdband it'll show you the tables...You can also exclude or only process certain tables (look at the readme).

你会想要运行php artisan larry:fromdb,它会向你显示表......你也可以排除或只处理某些表(查看自述文件)。

Again, super super useful if you like to build your schema in something like MySQL Workbench. I also saw mention of a package that would parse the workbench files...But the link was dead.

同样,如果你喜欢在 MySQL Workbench 之类的东西中构建你的架构,超级超级有用。我还看到提到一个可以解析工作台文件的包......但链接已经失效。

You may also wish to use this larry package with: https://github.com/JeffreyWay/Laravel-4-Generators

您可能还希望将这个 larry 包用于:https: //github.com/JeffreyWay/Laravel-4-Generators

You can then create scaffolding a la CakePHP style.

然后,您可以创建 CakePHP 风格的脚手架。

Alternatively, try this package: https://github.com/barryvdh/laravel-migration-generator

或者,试试这个包:https: //github.com/barryvdh/laravel-migration-generator