3

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

4 Answers 4

16

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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 Comments

I just created an artisan script that creates one if it doesn't exist.
@BrianOrtiz, would you share it? :)
0

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()
    {
        //
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.