biniyam20's avatar

Downloaded telescope and now my migrations to my test database won't work

Hi I downloaded telescope and followed the instructions to the t on a seperate branch. Then I had to make a hot fix on my prod branch so I checked out copy of my prod branch to expose and make the fix via a test. However to run the test, I need to migrate my db structure to my test database and when I try I keep getting this error

  Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'telescope_entries' already exists (SQL: create table `telescope_entries` (`sequence` bigint unsigned not null auto_increment primary key, `uuid` char(36) not null, `batch_id` char(36) not null, `family_hash` varchar(191) null, `should_display_on_index` tinyint(1) not null default '1', `type` varchar(20) not null, `content` text not null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at /Users/Biniyam/code/NextDorm/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664

I've looked at this detailed laracasts question where the user got the same error but using migrate:reset, migrate:fresh, etc... has not been successful. : https://laracasts.com/discuss/channels/code-review/sqlstate42s01-base-table-or-view-already-exists-1050-table-users-already-exists

In addition, there is not actually a telescope_entries table in that database because if there was, I could manually delete the table and rerun migrate.

I am not sure how to rememdy this issue and any guidance would be much appreciated.

The artisan command I ran is;

php artisan migrate:fresh --database=mysql_testing
0 likes
2 replies
biniyam20's avatar
biniyam20
OP
Best Answer
Level 2

Update: realizing that since none of the telescope tables were in my test database however the artisan command was acting like they were, I switched to my normal db connection and did

php artisan migrate:rollback 
Rolling back: 2018_08_08_100000_create_telescope_entries_table
Rolled back:  2018_08_08_100000_create_telescope_entries_table

Then I reran my migrate command on the test database and it worked!

php artisan migrate:fresh --database=mysql_testing

I am unclear why it worked and the results afterward because the telescope tables ARE still in the main database after I ran my second command but not in my testing database.

My conclusion is that considering there are no specific create_telescope_migrations files underneath the migration folder for the user, in the background on any php migrate that file gets run if it hasn't and gets created to the main database even if I am specifying a database to override. Is this a bug or expected behavior?

UPDATE: After spending some time away from my computer, the telescope tables have appeared in my testing database.

pmatseykanets's avatar

Most likely you don't want to have any piece of Laravel Telescope loaded while running your tests: neither its migrations nor having it collect anything.

In order to fully achieve this you need to

  • make laravel/telescope undiscoverable by default
    "extra": {
        "laravel": {
            "providers": [
                "Laravel\Telescope\TelescopeServiceProvider"
            ]
        }
    },
  • make sure you enable both Telescope Service Providers (the base and application one) only for specific environments (i.e. only local) in your AppServiceProvider
    public function register()
    {
        if ($this->app->isLocal()) {
            $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
            $this->app->register(TelescopeServiceProvider::class);
        }
    }

Then to migrate your test database you do

php artisan migrate --database=mysql_testing --env=testing

Note --env option. Technically, in our example, it have to be set to anything other than local

1 like

Please or to participate in this conversation.