jgravois's avatar

Changing Test Database and Deleting My Local THREE times

Given that I have accidentally deleted and had to restore my local database three times in the last 2 hours, I am reaching out for help.

I have been using sqlite in :memory: for testing but I am starting to use some features not available in sqlite so I want to test on a mysql ddb.

[1] I created a new database called atc_phpunit (atc is the prod database)

[2] I modified my phpunit.xml file

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql"/>
        <server name="DB_DATABASE" value="atc_phpunit"/>
<!--        <server name="DB_CONNECTION" value="sqlite"/>-->
<!--        <server name="DB_DATABASE" value=":memory:"/>-->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>

[3] I added Refresh Database to my TestCase

<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, RefreshDatabase;
}

[4] I run php artisan migrate --env=testing and get nothing to migrate

[5] I run my test suite and my local database is now empty

LOL, after three times, I think I may be missing a step???

0 likes
10 replies
gitwithravish's avatar

try,

php artisan migrate:fresh --database=testing_database_name

One more thing you should consider is that whether you cleared cache or not. I ran into very weird problems while doing testing last week. My local database was getting flushed out everytime I ran test. But @Sinnbeck told me not to clear or config cache, because phpunit does some weird stuff. So consider that as well. Start a new project and see if this still continues.

Sinnbeck's avatar

As @gitwithravish says, there is a cache you have cached your config. This will cause laravel to not pick up your changes

So try clearing it

php artisan config:clear 

If you have done caching, you might want to ensure that all caches are cleared

php artisan optimize:clear
1 like
jgravois's avatar

php artisan optimize:clear got me passed the nothing to migrate but it still refreshed (blew away, demolished, required restore) my atc database and left atc_phpunit database empty when I ran php artisan migrate:fresh --database=testing

Sinnbeck's avatar

Your database is called mysql_testing not testing

jgravois's avatar

so if phpunit.xml is this

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql_testing"/>
        <server name="DB_DATABASE" value="atc_phpunit"/>
<!--        <server name="DB_CONNECTION" value="sqlite"/>-->
<!--        <server name="DB_DATABASE" value=":memory:"/>-->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>

I should run this, right???

php artisan optimize:clear
artisan migrate:fresh --database=mysql_testing

Snapey's avatar

are you confusing db_connection and database name?

jgravois's avatar

I don't know what I am doing, right now ... I just keep truncating my local database ... LOL.

I just ran php artisan migrate:fresh --database=mysql_testing and I now have a blank testing database (YAY)

I am going to restore my local (last time I hope) and try to run the tests ... hopefully it won't kill the DB again

1 like
jgravois's avatar

OK ... if I may, I just want to clarify things since this is a change in my workflow ....

[1] Since I am using Use RefreshDatabase the test suite will create records in atc_phpunit as needed and the rollback those records for each test JUST LIKE WHEN I WAS USING SQLITE?

[2] If I add new migration to the app, I need to run php artisan migrate --database=mysql_testing to pick up those changes in the testing DB just like I have to run php artisan migrate on atc

[3] my development database should be safe now when tests are run.

Sorry to be so thick here but I need to be sure.

Please or to participate in this conversation.