Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel does not drop system versioned tables when running artisan migrate:fresh command. #49400

Closed
fnzr opened this issue Dec 15, 2023 · 2 comments · Fixed by #49509
Closed

Comments

@fnzr
Copy link

fnzr commented Dec 15, 2023

Laravel Version

10.37.3

PHP Version

8.2.12

Database Driver & Version

MariaDB 10.7

Description

MariaDB supports System Versioned Tables, allowing operations on temporal data.

If a migration-managed table is system versioned, the artisan command migrate:fresh command does not drop the table, and subsequently bringing the migrations up fails, since the migration tries to create a table that already exists.

The issue occurs because a system versioned table type is SYSTEM VERSIONED, and not BASE TABLE. When collecting the tables to be dropped, the system versioned table is not selected. Selecting SYSTEM VERSIONED tables fixes the issue.

I am willing to provide a PR fixing the problem, but it seems like it might be a risky change, I don't know. Please advise.

Steps To Reproduce

  1. Create base laravel project: composer create-project laravel/laravel example-app
  2. Setup MariaDB 10.7+(ish?) and configure .env to connect to the database.
  3. Run the default migrations with php artisan migrate
  4. Create a new migration php artisan make:migration add_users_table_versioning
  5. Execute the add versioning statement in the migration file:
public function up(): void
    {
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE users ADD SYSTEM VERSIONING;");
    }
  1. Run the migration. It works.
  2. Run php artisan migrate:fresh
    The command will fail with the message:
...
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
...
  1. Navigate to MysqlGrammar:86.
  2. Edit the query to
"from information_schema.tables where table_schema = %s and table_type = 'BASE TABLE' or table_type = 'SYSTEM VERSIONED' "
  1. Execute php artisan migrate:fresh

The command runs successfully.

Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@monish-khatri
Copy link

@fnzr @crynobone ,
Hey,
Please let me know. If I can assist you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants