Skip to content

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

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
merged 4 commits into from
Jun 10, 2025

Conversation

mehrancodes
Copy link
Owner

@mehrancodes mehrancodes commented Jun 8, 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 self-assigned this Jun 8, 2025
@mehrancodes mehrancodes changed the title Fix #134: Force Remove or Update Existing Database Fix #134: Add option to force delete and recreate existing database Jun 8, 2025
@mehrancodes mehrancodes requested a review from Copilot June 8, 2025 20:21
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new mechanism to optionally force-delete and recreate existing databases when provisioning a site.

  • Adds a dedicated RecreateDatabase action to encapsulate recreating logic and integrates it into the database creation pipeline
  • Introduces a force_delete_old_database flag in settings and config to control deletion behavior
  • Updates tests to cover both force-delete and non-force-delete scenarios

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
app/Services/Forge/Actions/RecreateDatabase.php New action class handling optional deletion and recreation of databases
app/Services/Forge/Pipeline/CreateDatabase.php Updated pipeline to use the new action and removed old helpers
app/Services/Forge/ForgeSetting.php Added forceDeleteOldDatabase property and validation
config/forge.php Exposed force_delete_old_database via environment
tests/Unit/Services/Forge/Actions/RecreateDatabaseTest.php Added tests for force-delete and non-force-delete paths
.github/workflows/run-tests.yml Bumped cache action to v4
Comments suppressed due to low confidence (2)

app/Services/Forge/Pipeline/CreateDatabase.php:40

  • The Str class is used but not imported. Add use Illuminate\Support\Str; at the top of the file to avoid a class not found error.
$dbPassword = Str::random(16);

tests/Unit/Services/Forge/Actions/RecreateDatabaseTest.php:115

  • The forceDeleteOldDatabase flag isn’t explicitly set in this test, so its default (null) may lead to unexpected behavior. Set it to false or true for clarity.
$this->service->setting = $this->settingMock;

@mehrancodes mehrancodes merged commit 083d01f into main Jun 10, 2025
1 check passed
@mehrancodes mehrancodes deleted the fix/issue-134-force-remove-database branch June 10, 2025 16:56
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
1 participant