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

ArchiveRestoreCommands::importDatabase problem with DB drop or create logic #5639

Closed
Sweetchuck opened this issue Jun 8, 2023 · 0 comments
Closed

Comments

@Sweetchuck
Copy link
Contributor

Sweetchuck commented Jun 8, 2023

Describe the bug
ArchiveRestoreCommands::importDatabase()
https://github.com/drush-ops/drush/blob/12.x/src/Commands/core/ArchiveRestoreCommands.php#L576

It tries to ensure that the database is exists and it is empty.

        if ($isDbExist && !$sql->drop($sql->listTablesQuoted())) {
            throw new Exception(
                dt('Failed to drop database !database.', ['!database' => $databaseSpec['database']])
            );
        } elseif (!$sql->createdb(true)) {
            throw new Exception(
                dt('Failed to create database !database.', ['!database' => $databaseSpec['database']])
            );
        }

I think the problem with the code above is that:

  1. in case of $isDbExists === true
  2. then it tries to drop all the tables in the database
  3. if the $sql->drop() is also successful, that means the overall if () is false
  4. then it goes to elseif (!$sql->createdb(true))
  5. which will always fail: "database cannot be created, because it is already exists".

Actual behavior
Wrong exception with misleading error message.

Workaround

        if ($isDbExist && !$sql->drop($sql->listTablesQuoted())) {
            throw new Exception(
                dt('Failed to drop database !database.', ['!database' => $databaseSpec['database']])
            );
        }

        if (!$isDbExist && !$sql->createdb(true)) {
            throw new Exception(
                dt('Failed to create database !database.', ['!database' => $databaseSpec['database']])
            );
        }

System Configuration

Q A
Drush version? 11.x
Drupal version? 10.x
PHP version 8.1
OS? Linux
DB engine PostgreSQL 15.3
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

1 participant