number6's avatar

SQLite memory error: "migrations" table already exists?

I've been having some weird errors with phpunit and Laravel Dusk. My first issue was the enviornment variables set in phpunit.xml were not being honored. I am guessing that was b/c of the local .env file. I solved that by using force=true on variables in my XML file. As a side question, is that the correct solution?

My real question is on the weird error in the subject line. During the setup process, I get this error:

PDOException: SQLSTATE[HY000]: General error: 1 table "migrations" already exists

However, before the parent::setUp() is run (where this error is thrown), I tested dd(env('DB_CONNECTION')) and dd(env('DB_DATABASE'))

They returned 'sqlite' and ':memory:' as expected. So....if it's an in memory database, how does the DB table already exist?

0 likes
3 replies
number6's avatar

OK. I just removed the RefreshDatabase line, and now I get a .unknown database :memory: error

use DatabaseMigrations; //use RefreshDatabase;

number6's avatar

I am at a loss on why nothing is working. I had to add force="true" to all env vars in phpunit.xml to get this far and then removed the :memory: database. Now it just says the tables don't exist so it's obviously not running migrations.

I will also add I am on Homestead if that matters at all.

My phpunit.xml (I removed the DB_DATABASE entry for :memory: since it was giving the error above.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing" force="true" />
        <env name="CACHE_DRIVER" value="array" force="true" />
        <env name="SESSION_DRIVER" value="array" force="true" />
        <env name="QUEUE_DRIVER" value="sync" force="true" />
        <env name="DB_CONNECTION" value="sqlite" force="true" />
        <env name="MONGO_DATABASE" value="tap_testing" force="true" />


    </php>
</phpunit>

How my TestClass is setup. It fails when trying to create a user, says the table doesn't exist (Migrations are all in place; it's worked in the past...)

<?php

namespace Tests\Unit;

use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

class ApplicationTest extends TestCase
{
  use DatabaseMigrations;

  public function setUp()
  {
    //dd(env('DB_CONNECTION')); // these are correct
    //dd(env('DB_DATABASE'));
    parent::setUp();

    $this->user = create('App\User');
    $this->signIn($this->user);
  }
}
number6's avatar
number6
OP
Best Answer
Level 6

OK I finally got there. Because I am using a hybrid mysql & mongodb setup, my User model required this:

class User extends SparkUser
{
  use HybridRelations;

  protected $connection = 'mysql';

I updated my user constructor to look like this, and all tests pass:

public function __construct(array $attributes = [])
  {
    // For unit tests
    if (env('APP_ENV') === 'testing') {
      $this->setConnection('sqlite');
    }
    parent::__construct($attributes);
  }

Please or to participate in this conversation.