我可以将 mysql 转储导入 Laravel 迁移吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21307464/
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
Can I import a mysql dump to a laravel migration?
提问by vladzur
I have a complete database and need to create migration. I guess there must be a way to do it from a dump but not sure. Is there any way automatically or at least easier to do this task?
我有一个完整的数据库,需要创建迁移。我想一定有办法从转储中做到这一点,但不确定。有什么方法可以自动或至少更容易地完成这项任务?
采纳答案by Kevin Gorjan
Laravel can't do that, but I think this will help: Laravel migration generator
Laravel 不能这样做,但我认为这会有所帮助:Laravel 迁移生成器
It generate migrations based on existing tables.
它基于现有表生成迁移。
回答by Tomas Buteler
You can import dumps in Laravel like this:
你可以像这样在 Laravel 中导入转储:
DB::unprepared(file_get_contents('full/path/to/dump.sql'));
If I were to refactor an existing app, though, I'd take the time to write migrations from scratch, import the dump into different tables (or a different db, if table names are the same) then import the content to the new structure via seeds.
但是,如果我要重构现有的应用程序,我会花时间从头开始编写迁移,将转储导入不同的表(或不同的数据库,如果表名相同),然后将内容导入新结构通过种子。
回答by shock_gone_wild
This question is answered already, but recently in a project, the provided answers did not satisfy my needs any longer. It also does not import a whole database dump, but one (large) table. I felt I should share that with you.
这个问题已经回答了,但最近在一个项目中,提供的答案不再满足我的需求。它也不会导入整个数据库转储,而是导入一个(大)表。我觉得我应该和你分享。
The problem was, I wanted to import a quite large table (list of zipcodes) during my artisan:migrate operation. The solution with DB::unprepared($dump) took way to long and I found an alternative which is MUCH faster.
问题是,我想在 artisan:migrate 操作期间导入一个相当大的表(邮政编码列表)。DB::unprepared($dump) 的解决方案花了很长时间,我找到了一个更快的替代方案。
Just export your table as CSV and use the following Code in your migration's up() function.
只需将您的表导出为 CSV 并在迁移的 up() 函数中使用以下代码。
// i had to str_replace the backslash on windows dev system... but works on linux, too
$filename = str_replace("\", "/", storage_path('path/in/storage/to/your/file.csv'));
$query = "LOAD DATA LOCAL INFILE '".$filename."' INTO TABLE yourtable
FIELDS TERMINATED BY '\t'
ENCLOSED BY ''
LINES TERMINATED BY '\n'
IGNORE 0 LINES
(col1,col2,...);";
DB::unprepared($query);
Just update the query as you need. And of course, you should make sure, that the table with the cols 'col1', 'col2' etc... exists. I created it just before the importing of the file. with Schema::create()...
只需根据需要更新查询。当然,您应该确保带有 cols 'col1'、'col2' 等的表存在。我在导入文件之前创建了它。使用 Schema::create()...
If you run into following error message:
如果您遇到以下错误消息:
PDO::exec(): LOAD DATA LOCAL INFILE forbidden
There is a way you can get rid of this message: Although it's not really documented you can just add an 'options' key to your config/database.php file. For example mine looks like that:
有一种方法可以消除此消息:虽然它没有真正记录在案,但您只需在 config/database.php 文件中添加一个“选项”键即可。例如我的看起来像这样:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'options' => array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
)
Note: i'm currently using laravel 5 but it should work with laravel 4, too.
注意:我目前使用的是 laravel 5,但它也应该与 laravel 4 一起使用。
回答by ceejayoz
I have a complete database and need to create migration. I guess there must be a way to do it from a dump but not sure. Is there any way automatically or at least easier to do this task?
我有一个完整的数据库,需要创建迁移。我想一定有办法从转储中做到这一点,但不确定。有什么方法可以自动或至少更容易地完成这项任务?
Not automatically, but we run dumps in a migration using DB::unprepared()
. You could use file_get_contents
to import from a .sql
file and thus not have to worry about escaping the entire dump's "
marks...
不是自动的,但我们在迁移中使用DB::unprepared()
. 您可以使用file_get_contents
从.sql
文件导入,因此不必担心转义整个转储的"
标记......
<?php
use Illuminate\Database\Migrations\Migration;
class ImportDump extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared("YOUR SQL DUMP HERE");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
回答by Chtheitroadul MA
another solution work for me in Laravel 5.2:
在 Laravel 5.2 中对我来说有效的另一个解决方案:
DB::unprepared(File::get('full/path/to/dump.sql'));
DB::unprepared(File::get('full/path/to/dump.sql'));
回答by zmonteca
Another alternative is using the PDO directly:
另一种选择是直接使用 PDO:
$sql_dump = File::get('/path/to/file.sql');
DB::connection()->getPdo()->exec($sql_dump);
回答by mastercheef85
i recently standing in front of the same problem. i didn't want to install a package specially for that, so i decided to write a little tool to help me and others ;)
我最近站在同样的问题面前。我不想为此专门安装一个包,所以我决定写一个小工具来帮助我和其他人;)
Here is the link: http://laravel.stonelab.ch/sql-seeder-converter/
这是链接:http: //laravel.stonelab.ch/sql-seeder-converter/
And here you can comment it, if you have any improvement proposals or questions: http://www.stonelab.ch/en/sql-to-laravel-seeder-converter/
如果您有任何改进建议或问题,可以在这里发表评论:http: //www.stonelab.ch/en/sql-to-laravel-seeder-converter/
回答by Sriraman
You can create laravel migration and models directly from database using https://github.com/XCMer/larry-four-generator
您可以使用https://github.com/XCMer/larry-four-generator直接从数据库创建 Laravel 迁移和模型
Execute the following code after installing the package
安装包后执行以下代码
php artisan larry:fromdb
回答by JohnTaa
You can use Raahul/Larryfour Package, A model and migration generator for Laravel 4
您可以使用 Raahul/Larryfour 包,Laravel 4 的模型和迁移生成器
After insallation you can use a command line to create a migration from existed database like this:
安装后,您可以使用命令行从现有数据库创建迁移,如下所示:
php artisan raahul:fromdb --only yourdatabase
And you will find the migration in app/migrations/ folder
你会在 app/migrations/ 文件夹中找到迁移
回答by Akash khan
Simple solution provided by Laravel Article for generating migration file from an existing database table.
Laravel 文章提供的简单解决方案,用于从现有数据库表生成迁移文件。
Try: https://laravelarticle.com/laravel-migration-generator-online
试试:https: //laravelarticle.com/laravel-migration-generator-online