laravel / telescope

An elegant debug assistant for the Laravel framework.
https://laravel.com/docs/telescope
MIT License
4.84k stars 578 forks source link

Telescope ignores migrate's --database parameter #540

Closed loykianos closed 4 years ago

loykianos commented 5 years ago

Whenever I try using artisan migrate or migrate:refresh with the --database parameter (f.e. different testing database), telescope produces a "duplicate table" error.

Migrating: 2018_08_08_100000_create_telescope_entries_table

Illuminate\Database\QueryException : SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "telescope_entries" already exists (SQL: create table "telescope_entries" ("sequence" bigserial primary key not null, "uuid" uuid not null, "batch_id" uuid not null, "family_hash" varchar(255) null, "should_display_on_index" boolean not null default '1', "type" varchar(20) not null, "content" text not null, "created_at" timestamp(0) without time zone null))

simioluwatomi commented 5 years ago

I just wanted to report this. It is becoming annoying having to fiddle with Telescope's database connection value in config/telescope everytime I want to migrate my testing database.

Screenshot below

image

diogogomeswww commented 5 years ago

My solution was to create an .env.testing with the entries:

APP_ENV=testing
APP_KEY=...
DB_CONNECTION=mysql_testing
TELESCOPE_ENABLED=0
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync

And then run: art migrate:fresh --env=testing

that env flag will load the .env.testing file and will fix the issue with telescope migrations

tim-mortimer commented 5 years ago

@diogogomeswww nice work! I'm assuming that means you don't need to specify the --database=mysql_testing option when running the command then?

diogogomeswww commented 5 years ago

@tim-mortimer ah yes, you are right, of course: art migrate:fresh --env=testing is enough. (going to edit above)

ntraykov commented 5 years ago

I also have this issue. I also reached the point where the only solution that I have was to create an .env.testing file, but still, this shouldn't work like that, because it is confusing. Telescope should respect the --database parameter.

themsaid commented 4 years ago

This is not an issue with Telescope, the migration is just a regular laravel migration and when you run migrate --database=x you ask Laravel to run all the migrations on connection x.

Since you have telescope's migration using a specific connection, the migrator will try to run the migration on this connection and in which case the table already exists, hence the error.

ssddanbrown commented 4 years ago

I think both Laravel itself and telescope default their connections to the DB_CONNECTION env variable so DB_CONNECTION=mysql_testing php artisan migrate might work for some.

Just to add to the comment by themsaid above, since I got caught out by this and was confused as to what was occuring, Your default or, when using --database, specified connection acts as the master record in regards to migrations. Telescope's migrations will use the telescope configured connection but that connection does not store a migration record itself, that's still in your "main" db connection.

Therefore when you attempt to use the --database parameter Laravel will scan your specified connection for pending migrations. If the telescope migrations have not ran according to the migrations table of the specified connection then they will be ran but using the connection defined in the telescope config.

Hence, if you have a testing database on a different connection and you try to migrate it, it may look in it's own migration table and see that the telescope migration has not ran, try to run it but that specific migration will be ran against the telescope connection which still points to the other database.

Maybe a way to tell telescope to use the default database connection, instead of its own defined connection, would help?

mabasic commented 4 years ago

Just stumbled upon this issue. It took me half an hour to figure out what was going on...

driesvints commented 4 years ago

We don't have plans on changing this ourselves but feel free to attempt a PR if you like.

cgreuling commented 4 years ago

Came across this today and the easiest fix for myself ended up being:

TELESCOPE_ENABLED=0 php artisan migrate:fresh --database=testing

Hope this might help someone else if they get stuck.

chopstik commented 2 years ago

I got around this issue by publishing the migrations... php artisan vendor:publish --tag=telescope-migrations

Then in database/migrations/2018_08_08_100000_create_telescope_entries_table.php adjusting the getConnection() method.

public function getConnection()
    {
        return config('database.default');
        //return config('telescope.storage.database.connection');
    }

config('database.default') at that point will return the database specified by your --database flag on the migration command: php artisan migrate --database=testing