-
Notifications
You must be signed in to change notification settings - Fork 93
Preserve external MySQL database names #4690
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
Open
atirna
wants to merge
12
commits into
Automattic:trunk
Choose a base branch
from
atirna:fix/preserve-external-mysql-db-name
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−3
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7fd51a1
Preserve external MySQL database names
atirna fd2782f
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 1c0982a
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 61d0c52
Scan for a define once
atirna 40a2525
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 60061d7
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna d12057c
fix: derive database name ownership from SQLite state
atirna fc90b6a
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 7ad05c8
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 4045328
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna 9249fbb
Merge remote-tracking branch 'origin/trunk' into fix/preserve-externa…
atirna 4b4d08e
Merge branch 'trunk' into fix/preserve-external-mysql-db-name
atirna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { ensureWpConfig } from '../site-setup'; | ||
|
|
||
| const { ensurePhpBinaryAvailable, runPhpCommand } = vi.hoisted( () => ( { | ||
| ensurePhpBinaryAvailable: vi.fn( async () => undefined ), | ||
| runPhpCommand: vi.fn(), | ||
| } ) ); | ||
|
|
||
| vi.mock( '../../dependency-management/php-binary', () => ( { | ||
| ensurePhpBinaryAvailable, | ||
| } ) ); | ||
|
|
||
| vi.mock( '../php-process', () => ( { | ||
| runPhpCommand, | ||
| } ) ); | ||
|
|
||
| const PHP_VERSION = '8.4' as Parameters< typeof ensureWpConfig >[ 1 ]; | ||
| const TRANSFORMER_PATH = path.resolve( | ||
| import.meta.dirname, | ||
| '../../../php/wp-config-transformer.php' | ||
| ); | ||
|
|
||
| function phpAvailable(): boolean { | ||
| try { | ||
| execFileSync( 'php', [ '--version' ], { stdio: 'ignore' } ); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| describe.skipIf( ! phpAvailable() )( 'ensureWpConfig', () => { | ||
| let tmpDir: string; | ||
|
|
||
| beforeEach( () => { | ||
| tmpDir = fs.mkdtempSync( path.join( os.tmpdir(), 'studio-site-setup-' ) ); | ||
| ensurePhpBinaryAvailable.mockClear(); | ||
| runPhpCommand.mockImplementation( async ( args: string[] ) => { | ||
| const [ , script, , wpConfigPath, constants ] = args; | ||
| const runnerPath = path.join( tmpDir, 'run-transformer.php' ); | ||
| fs.writeFileSync( runnerPath, `<?php\n${ script }` ); | ||
| execFileSync( 'php', [ runnerPath, TRANSFORMER_PATH, wpConfigPath, constants ], { | ||
| stdio: 'pipe', | ||
| } ); | ||
| } ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| fs.rmSync( tmpDir, { recursive: true, force: true } ); | ||
| } ); | ||
|
|
||
| it( 'preserves an external database name while updating debug constants', async () => { | ||
| const wpConfigPath = path.join( tmpDir, 'wp-config.php' ); | ||
| fs.writeFileSync( | ||
| wpConfigPath, | ||
| "<?php\ndefine( 'DB_NAME', 'database_demo' );\ndefine( 'WP_DEBUG', true );\n" | ||
| ); | ||
|
|
||
| await ensureWpConfig( tmpDir, PHP_VERSION ); | ||
|
|
||
| const contents = fs.readFileSync( wpConfigPath, 'utf8' ); | ||
| expect( contents ).toContain( "define( 'DB_NAME', 'database_demo' );" ); | ||
| expect( contents ).toContain( "define( 'WP_DEBUG', false );" ); | ||
| } ); | ||
|
|
||
| it( 'replaces the WordPress sample database placeholder for local sites', async () => { | ||
| const wpConfigPath = path.join( tmpDir, 'wp-config.php' ); | ||
| fs.writeFileSync( wpConfigPath, "<?php\ndefine( 'DB_NAME', 'database_name_here' );\n" ); | ||
|
|
||
| await ensureWpConfig( tmpDir, PHP_VERSION ); | ||
|
|
||
| expect( fs.readFileSync( wpConfigPath, 'utf8' ) ).toContain( | ||
| "define( 'DB_NAME', 'wordpress' );" | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'ignores a commented-out external database name', async () => { | ||
| const wpConfigPath = path.join( tmpDir, 'wp-config.php' ); | ||
| fs.writeFileSync( wpConfigPath, "<?php\n// define( 'DB_NAME', 'database_demo' );\n" ); | ||
|
|
||
| await ensureWpConfig( tmpDir, PHP_VERSION ); | ||
|
|
||
| expect( fs.readFileSync( wpConfigPath, 'utf8' ) ).toContain( | ||
| "define( 'DB_NAME', 'wordpress' );" | ||
| ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.