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

Migration fails when database name contains a dot (.) #54825

Closed
arnolem opened this issue Feb 27, 2025 · 9 comments
Closed

Migration fails when database name contains a dot (.) #54825

arnolem opened this issue Feb 27, 2025 · 9 comments

Comments

@arnolem
Copy link

arnolem commented Feb 27, 2025

Laravel Version

12.0.1

PHP Version

8.3

Database Driver & Version

MySQL

Description

When running php artisan migrate:fresh in Laravel 12, the migration process fails if the database name contains a dot (.). This happens because Laravel splits table names on the dot character when compiling the DROP TABLE statement, leading to an invalid SQL query.

Cause

The issue originates from Illuminate\Database\Grammar.php, where table names are split on . using explode(), assuming it’s a schema/table separator. However, this does not account for databases with dots in their names.

Relevant Code

public function compileDropAllTables($tables)
{
    return 'drop table '.implode(',', $this->wrapArray($tables));
}

public function wrapArray(array $values)
{
    return array_map($this->wrap(...), $values);
}

public function wrap($value)
{
    return $this->wrapSegments(explode('.', $value));
}

Expected Behavior

Laravel should correctly wrap the full database name and table name without splitting on dots inside database names.

Database Driver & Version: [e.g., MySQL 8.0]

Would appreciate any guidance on how to resolve this!

Steps To Reproduce

Set up a database with a name containing a dot, e.g., website.com.

Configure .env with this database name:

DB_DATABASE=website.com

Run:

php artisan migrate:fresh

Laravel generates an invalid SQL query:

drop table website.com.cache``

Instead of:

drop table website.com.cache``

@rez1dent3
Copy link
Contributor

rez1dent3 commented Feb 27, 2025

@arnolem duplicate/similar. #54817

@macropay-solutions

This comment has been minimized.

@hafezdivandari
Copy link
Contributor

hafezdivandari commented Feb 27, 2025

Just sent PR #54834 to resolve this issue.

@rez1dent3
Copy link
Contributor

@hafezdivandari Hello. I think your solution did not solve this problem, because explode goes to all points.

@hafezdivandari
Copy link
Contributor

hafezdivandari commented Feb 27, 2025

@rez1dent3 You are right, that PR doesn't solve this issue.

@arnolem The problem is that . is a special character in SQL. For example, database.table, database.table.column, database.column.index, and etc. used to call a DB object by reference. Almost every escaping/wrapping functions in Schema/Query of Builder/Grammar classes, explode the references by . to wrap values (e.g. database.table becomes `database`.`table`). So my suggestion is to not use . in any of your DB object names including database, schema, table, column, index, foreign keys names.

@rez1dent3
Copy link
Contributor

@hafezdivandari I think so too, but I'm not the author of the issue. I was passing by and noticed)

@crynobone
Copy link
Member

Thanks @hafezdivandari

@arnolem
Copy link
Author

arnolem commented Mar 3, 2025

@hafezdivandari The problem is that while the dot is indeed a permitted character in quoted identifiers, Laravel does not respect this rule. I have been using dots in database names for over 20 years and have never encountered any issues with any drivers, software, or frameworks—including Symfony, Laravel <11, or Laminas in the PHP ecosystem.

You can find the MySQL documentation here: https://dev.mysql.com/doc/refman/8.4/en/identifiers.html

Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000: ASCII: U+0001 .. U+007F, Extended: U+0080 .. U+FFFF

@macropay-solutions
Copy link

@arnolem can you identify the commit that broke this?

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

No branches or pull requests

5 participants