Migrations fail when SQLite database file does not exist?

不羁岁月 提交于 2019-12-09 07:33:24

问题


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.

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


回答1:


I've issued this bug against laravel/framework.

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




回答2:


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

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.




回答3:


This is an updated and more flexible solution from 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()
    {
        //
    }
}



回答4:


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

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

<?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);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/17872110/migrations-fail-when-sqlite-database-file-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!