Sqlite and Laravel

Posted: 2015-01-18 19:50:31

It is really really nice to use this for testing and local since any reseed work is quick.

Overall the swap out is easy and I do not think, so far, there has been any issues going to mysql on production.

The only two things to set over all is

Seeding

In my DatabaseSeeder.php file I set this

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();

        if(DB::getDriverName() != 'sqlite') {
            DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        }

        $this->call('AppTableSeeder');

        if(DB::getDriverName() != 'sqlite') {
            DB::statement('SET FOREIGN_KEY_CHECKS=1;');
        }
    }

}

This allows me to reseed without issues.

Null fields

On fields that are allowed to be null just add this

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddGithubTokenToUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->string('name')->nullable();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->dropColumn('name');
        });
    }

}

Check out Speeding up PHPunit tests and Behat in Laravel for Database refreshes to see more info of how you can use this.

Also see php artisan migrate:reset not working for some extra setup.

Update 2-11-2015

Andrew Cavanagh noted that since renameColumn does not work with Sqlite you can do this to drop and make the column. Also without the two steps in up() seeding failed.

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('diff_requests', function(Blueprint $table)
        {
            $table->dropColumn('bundle_id');
        });

        Schema::table('diff_requests', function(Blueprint $table)
        {
            $table->string('project_id', 36)->nullable();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('diff_requests', function(Blueprint $table)
        {
            $table->dropColumn('project_id');

            $table->string('bundle_id', 36)->nullable();
        });
    }