首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel迁移|在原始SQL中删除/创建表无错误,无影响

Laravel迁移|在原始SQL中删除/创建表无错误,无影响
EN

Stack Overflow用户
提问于 2019-07-04 14:16:43
回答 1查看 178关注 0票数 2

对于一个新的Laravel项目,我需要使用现有的MySQL数据库(176个表)。我不想为每个现有的表创建Laravel迁移,所以我将数据库结构导出到一个sql文件中。

在迁移中,我希望执行SQL文件,如下所示:

代码语言:javascript
运行
复制
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。在迁移过程中,我需要修改一些表。

我不想为每个表创建迁移。

EN

Stack Overflow用户

发布于 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中添加

代码语言:javascript
运行
复制
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)改变

代码语言:javascript
运行
复制
$app = new Application(
    realpath(__DIR__.'/../')
); 

代码语言:javascript
运行
复制
$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

在bootstrap/app.php中

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56881660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档