5

I'm trying to use artisan migrate to create tables in sqlite.

I have the following in database.php

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
    ],

and this is my migrate class up function

    Schema::connection('sqlite')->create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

Note:- I have set DB_CONNECTION to be sqlite via the environment variable.

On the command line I get nothing to migrate, and no db is created (also no table).

Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?

I have no problem creating mysql tables via artisan.

Thanks

2
  • What does artisan migrate:status show? Commented Jul 1, 2016 at 11:42
  • +------+--------------------------------------+ | Ran? | Migration | +------+--------------------------------------+ | Y | 2014_10_12_000000_create_users_table | | Y | 2016_07_01_101905_create_test_table | +------+--------------------------------------+ Commented Jul 1, 2016 at 12:10

4 Answers 4

7

its because you didnt create the db. use touch command like this to create new sqlite database.

touch database/database.sqlite

then configure the environment variable like this

DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
Sign up to request clarification or add additional context in comments.

1 Comment

I should have mentioned I already tried that and it didn't work, besides sqlite creates a db file there isn't one.
4

In order to answer this part of the OP's question: "Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?"

In some cases where deployment and migrations need to be scripted I use a Service Provider to check the Sqlite DB exists and if not create it.

Here are the steps:

1) Create a Provider and save it to app/Providers/SqliteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class SqliteServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $databaseFile = config('database.connections.sqlite.database');
        if (!file_exists($databaseFile)) {
            info('Make Sqlite File "' . $databaseFile . '"');
            file_put_contents($databaseFile, '');
        }
    }
}

2) Register the provider in config/app.php. For example in a fresh L5.8 install the SqliteServiceProvider would be placed under the RouteServiceProvider like so:

'providers' => [

    //...

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Providers\SqliteServiceProvider::class, // <--- HERE

],

3) Add/replace the following to the 'connections' element of config/database.php

'sqlite' => [
    'driver' => 'sqlite',
    'database' => env('DB_SQLITE_FILEPATH', database_path('database.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

4) In my .env file I add this:

DB_SQLITE_FILEPATH=/full/path/to/mysqlitefilename.sqlite

...but if you prefer you can skip this step and the DB will be created as database/database.sqlite.

5) Run your migration as usual and the sqlite file should be created then populated.

Comments

0

Ok I got it. artisan migrate seems to ignore the env DB_CONNECTION and the database.php default (which reads the env anyway). Instead it like the .env file DB_CONNECTION, with this set to sqlite it works (plus, as mentioned above you do need to manually create the database file, which is a pain for making dbs on the fly.

Also means you can't really change artisan migrate database types (ie: sqlite and mysql) on the fly as you need to change the .env variable each time you change the target db type.

go laravel.

1 Comment

Why would you change .env variable each time? All you have to change is the target db and all settings will be picked up. You can give your variables names specific to the database. For example DB_SQLITE_DATABASE, DB_PGSQL_DATABASE, DB_PGSQ_USERNAME etc
0

That's right, you need to create empty database.sqlite file.

You should also use artisan --env parameter if you want artisan to load the concrete .env file. For example, artisan --env=testing migrate will force artisan to load .env.testing file.

1 Comment

Nice custom env tip!

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.