laravel SQLite 数据库文件不存在时迁移失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17872110/
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 08:11:07  来源:igfitidea点击:

Migrations fail when SQLite database file does not exist?

sqlitelaravellaravel-4

提问by Brian Ortiz

It seems that migrations (sort of) fail silently when the database file does not exist. The migration executes but no db file is created and I can run the migration again. (It never says "nothing to migrate") If I create a blank file then it works.

当数据库文件不存在时,迁移(某种程度上)似乎会以静默方式失败。迁移执行但未创建 db 文件,我可以再次运行迁移。(它从不说“无需迁移”)如果我创建了一个空白文件,那么它就可以工作。

This is odd because I thought SQLite always created the db file if it was not found so I'm not sure if this is a bug or something I've done wrong. Maybe it's a permissions problem? But everything else is working so I don't know. I'm using Windows 7 and the project is in my

这很奇怪,因为我认为 SQLite 总是在找不到 db 文件时创建它,所以我不确定这是一个错误还是我做错了什么。也许这是一个权限问题?但其他一切都在工作,所以我不知道。我正在使用 Windows 7 并且该项目在我的

回答by Daniel A.A. Pelsmaeker

User blamhsuggestedto add the following snippet to app/start/artisan.phpto automatically recreate the database when it doesn't exist, instead of throwing an exception.

用户blamh建议添加以下代码段以app/start/artisan.php在数据库不存在时自动重新创建数据库,而不是抛出异常。

if (Config::get('database.default') === 'sqlite') {
    $path = Config::get('database.connections.sqlite.database');
    if (!file_exists($path) && is_dir(dirname($path))) {
        touch($path);
    }
}

With this, you can safely delete the SQLite database and then re-migrate and re-seed it, if you wish.

有了这个,您可以安全地删除 SQLite 数据库,然后根据需要重新迁移和重新播种。

回答by Dwight

I've issued this bug against laravel/framework.

我已经针对laravel/framework发布了这个错误。

Hopefully future versions will give an error if the database doesn't exist, or automatically create one.

如果数据库不存在,希望未来的版本会出错,或者自动创建一个。

回答by Gummibeer

This is an updated and more flexible solution from Virtlinks answer

这是Virtlinks answer的更新和更灵活的解决方案

<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class SqliteServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (DB::getDriverName() === 'sqlite') {
            $path = DB::getConfig('database');
            if (!file_exists($path) && is_dir(dirname($path))) {
                touch($path);
            }
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

回答by Parziphal

Here's yet another way to automatically create the database file, tested on Laravel 5.4.

这是在 Laravel 5.4 上测试的另一种自动创建数据库文件的方法。

This is the same as Gummibeer's answer except that I moved the logic to the App\Console\Kernelclass (app/Console/Kernel.php), and the check will be performed only when running the migratecommand.

这与 Gummibeer 的回答相同,只是我将逻辑移到了App\Console\Kernel类 ( app/Console/Kernel.php) 中,并且仅在运行migrate命令时才会执行检查。

<?php

use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
     * @return int
     */
    public function handle($input, $output = null)
    {
        $this->touchSQLiteDatabase($input);

        return parent::handle($input, $output);
    }

    protected function touchSQLiteDatabase($input)
    {
        $this->bootstrap();

        if (substr((string)$input, 0, 7) == 'migrate' && DB::getDriverName() === 'sqlite') {
            $path = DB::getConfig('database');

            if (!file_exists($path) && is_dir(dirname($path))) {
                touch($path);
            }
        }
    }
}