Summer Sale! All accounts are 50% off this week.

vincent15000's avatar

Using FastRefreshDatabase to drop views

Hello,

I'm using FastRefreshDatabase, but I'd like to drop the views too.

How is it possible to do that ? Something like overloading a method ? Some other way ?

Thanks for your help.

V

0 likes
5 replies
LaryAI's avatar
Level 58

To drop views when using the FastRefreshDatabase trait in Laravel, you can extend the functionality by overriding the refreshTestDatabase method. This method is responsible for refreshing the database state before each test. You can add custom logic to drop views within this method.

Here's a step-by-step solution:

  1. Create a Custom Trait: Create a new trait that extends the FastRefreshDatabase trait.

  2. Override the refreshTestDatabase Method: In your custom trait, override the refreshTestDatabase method to include logic for dropping views.

  3. Use the Custom Trait: Use your custom trait in your test classes instead of the default FastRefreshDatabase.

Here's an example implementation:

Explanation:

  • Custom Trait: The CustomFastRefreshDatabase trait extends the FastRefreshDatabase trait.
  • refreshTestDatabase Method: This method is overridden to include a call to dropAllViews before running migrations.
  • dropAllViews Method: This method retrieves all views from the database and drops them using SQL statements.

Usage:

  • Use the CustomFastRefreshDatabase trait in your test classes to ensure views are dropped before each test.
use Tests\CustomFastRefreshDatabase;

class ExampleTest extends TestCase
{
    use CustomFastRefreshDatabase;

    // Your test methods
}

This approach ensures that your database views are dropped and recreated as needed during testing, maintaining a clean state.

vincent15000's avatar

@Tray2 I wanted to do that because the migration is re-executed by default and if the view isn't dropped, it generates an error.

But effectively I just have to add create or replace.

Tray2's avatar

@vincent15000 Unless you use SQLite, it doesn't support the Create or replace.

DROP VIEW IF EXISTS [vtable]; -- "OR REPLACE"
CREATE VIEW [vtable] AS SELECT * FROM Files_Table ORDER BY File;
1 like
vincent15000's avatar

@Tray2 I just replaced the code with create or replace, it's with MariaDB and it works.

1 like

Please or to participate in this conversation.