对于一个新的Laravel项目,我需要使用现有的MySQL数据库(176个表)。我不想为每个现有的表创建Laravel迁移,所以我将数据库结构导出到一个sql文件中。
在迁移中,我希望执行SQL文件,如下所示:
public function up()
{
DB::unprepared(file_get_contents('/path/to/file.sql'));
}unprepared返回true,但是导入似乎不会(完全)执行。没有错误,没有影响(有时,会创建1到2个表,例如,在执行sql文件之前删除并重新创建数据库之后)。
当我使用mysql source /path/to/file.sql执行这个文件时,导入工作正常(会根据版本差异报告一些错误,但会继续执行)。
我的问题:出于测试目的,我想在迁移过程中从SQL文件创建176 old/existing tables。在迁移过程中,我需要修改一些表。
我不想为每个表创建迁移。
发布于 2019-07-05 19:43:44
您可以通过以下步骤对所有表执行反向迁移:-
1)composer require -dev "xethron/migrations-generator“
2)在bootstrap/app -$app->register(\Way\Generators\GeneratorsServiceProvider::class);$app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);中
3)在bootstrap/app.php中添加
class Application extends Laravel\Lumen\Application
{
/**
* Get the path to the application configuration files.
*
* @param string $path Optionally, a path to append to the config path
* @return string
*/
public function configPath($path = '')
{
return $this->basePath.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
if (!function_exists('config_path')) {
/**
* Get the configuration path.
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}
if (!function_exists('app_path')) {
/**
* Get the path to the application folder.
*
* @param string $path
* @return string
*/
function app_path($path = '')
{
return app('path') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}
}
class_alias('Illuminate\Support\Facades\Config', 'Config');
$app = new Application(
realpath(__DIR__.'/../')
);4)编写php artisan migrate:在终端生成
5)改变
$app = new Application(
realpath(__DIR__.'/../')
); 至
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);在bootstrap/app.php中
https://stackoverflow.com/questions/56881660
复制相似问题