Skip to content

Fix #134: Add option to force delete and recreate existing database #139

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

Merged

Conversation

mehrancodes
Copy link
Owner

@mehrancodes mehrancodes commented Jun 10, 2025

Overview

This PR fixes issue #134 by implementing a new RecreateDatabase action class that can optionally force delete existing databases and recreate them with new credentials.

Changes

  • Created a new RecreateDatabase action class that encapsulates database recreation logic
  • The action can check if a database exists, optionally delete it based on config, and recreate it
  • Updated CreateDatabase pipeline to leverage the new action class
  • Added PestPHP tests for the new RecreateDatabase action
  • Added a forceDeleteOldDatabase flag to ForgeSetting to control this behavior

How to Test

Scenario 1: Existing database with force delete enabled

When you have an existing database on the server with the same name:

  1. Set FORCE_DELETE_OLD_DATABASE=true in your .env file
  2. Run Harbor provision to create a site
  3. Expected behavior:
    • Harbor deletes the existing database
    • Harbor deletes any existing database user with the same name
    • A new database is created with the same name but new credentials

Scenario 2: Existing database with force delete disabled

When you have an existing database on the server with the same name:

  1. Set FORCE_DELETE_OLD_DATABASE=false in your .env file (or leave it unset)
  2. Run Harbor to create a site
  3. Expected behavior:
    • Harbor preserves the existing database
    • Harbor displays a warning about the existing database
    • No new database is created

Scenario 3: No existing database

When no database exists with the specified name:

  1. Run Harbor to create a site (regardless of FORCE_DELETE_OLD_DATABASE setting)
  2. Expected behavior:
    • A new database is created with the specified name
    • New database credentials are set in the site's .env file

Fixes #134

@mehrancodes mehrancodes merged commit 6225eb9 into main Jun 11, 2025
1 check passed
@mehrancodes mehrancodes deleted the revert-138-revert-137-fix/issue-134-force-remove-database branch June 11, 2025 10:23
@michal78
Copy link

Thank you! 🙏

@mehrancodes
Copy link
Owner Author

Thank you! 🙏

@michal78 Glad it helped 🙌
Curious - would you mind sharing how it helped with your preview deploys? Were you running into issues where Forge failed the first deploy because the DB password in the site’s .env didn’t match the newly created DB’s password? Just trying to understand the context a bit better.

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

Successfully merging this pull request may close these issues.

Force Remove or Update Existing Database
2 participants