-
Notifications
You must be signed in to change notification settings - Fork 18
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
fix(dev-env): detection of URLs to replace for multisites #2081
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fbad25
fix(dev-env): detection of URLs to replace for multisites
sjinks 66bd4d6
fix: replacement takes paths into consideration
sjinks e8bfc16
refactor: simplify `replaceDomain()`
sjinks e235520
fix: update `wp_blogs`
sjinks aa5112c
Fix test cases - we don't have id's anymore
rinatkhaziev 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
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -5,7 +5,6 @@ import chalk from 'chalk'; | |
import fs from 'fs'; | ||
import Lando from 'lando'; | ||
import { pipeline } from 'node:stream/promises'; | ||
import urlLib from 'url'; | ||
|
||
import { DevEnvImportSQLCommand, DevEnvImportSQLOptions } from './dev-env-import-sql'; | ||
import { ExportSQLCommand } from './export-sql'; | ||
|
@@ -18,6 +17,27 @@ import { fixMyDumperTransform, getSqlDumpDetails, SqlDumpType } from '../lib/dat | |
import { makeTempDir } from '../lib/utils'; | ||
import { getReadInterface } from '../lib/validations/line-by-line'; | ||
|
||
/** | ||
* Replaces the domain in the given URL | ||
* | ||
* @param string str The URL to replace the domain in. | ||
* @param string domain The new domain | ||
* @return The URL with the new domain | ||
*/ | ||
const replaceDomain = ( str: string, domain: string ): string => | ||
str.replace( /^([^:]+:\/\/)([^:/]+)/, `$1${ domain }` ); | ||
|
||
/** | ||
* Strips the protocol from the URL | ||
* | ||
* @param string url The URL to strip the protocol from | ||
* @return The URL without the protocol | ||
*/ | ||
function stripProtocol( url: string ): string { | ||
const parts = url.split( '//', 2 ); | ||
return parts.length > 1 ? parts[ 1 ] : parts[ 0 ]; | ||
} | ||
|
||
/** | ||
* Finds the site home url from the SQL line | ||
* | ||
|
@@ -27,8 +47,12 @@ import { getReadInterface } from '../lib/validations/line-by-line'; | |
function findSiteHomeUrl( sql: string ): string | null { | ||
const regex = `['"](siteurl|home)['"],\\s?['"](.*?)['"]`; | ||
const url = sql.match( regex )?.[ 2 ] || ''; | ||
|
||
return urlLib.parse( url ).hostname || null; | ||
try { | ||
new URL( url ); | ||
return url; | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -42,17 +66,17 @@ async function extractSiteUrls( sqlFile: string ): Promise< string[] > { | |
const readInterface = await getReadInterface( sqlFile ); | ||
|
||
return new Promise( ( resolve, reject ) => { | ||
const domains: Set< string > = new Set(); | ||
const urls: Set< string > = new Set(); | ||
readInterface.on( 'line', line => { | ||
const domain = findSiteHomeUrl( line ); | ||
if ( domain ) { | ||
domains.add( domain ); | ||
const url = findSiteHomeUrl( line ); | ||
if ( url ) { | ||
urls.add( url ); | ||
} | ||
} ); | ||
|
||
readInterface.on( 'close', () => { | ||
// Soring by length so that longest domains are replaced first | ||
resolve( Array.from( domains ).sort( ( dom1, dom2 ) => dom2.length - dom1.length ) ); | ||
// Soring by length so that longest URLs are replaced first | ||
resolve( Array.from( urls ).sort( ( url1, url2 ) => url2.length - url1.length ) ); | ||
} ); | ||
|
||
readInterface.on( 'error', reject ); | ||
|
@@ -168,7 +192,9 @@ export class DevEnvSyncSQLCommand { | |
this.searchReplaceMap = {}; | ||
|
||
for ( const url of this.siteUrls ) { | ||
this.searchReplaceMap[ url ] = this.landoDomain; | ||
this.searchReplaceMap[ stripProtocol( url ) ] = stripProtocol( | ||
replaceDomain( url, this.landoDomain ) | ||
); | ||
} | ||
|
||
const networkSites = this.env.wpSitesSDS?.nodes; | ||
|
@@ -177,12 +203,18 @@ export class DevEnvSyncSQLCommand { | |
for ( const site of networkSites ) { | ||
if ( ! site?.blogId || site.blogId === 1 ) continue; | ||
|
||
const url = site?.homeUrl?.replace( /https?:\/\//, '' ); | ||
if ( ! url || ! this.searchReplaceMap[ url ] ) continue; | ||
const url = site?.homeUrl; | ||
if ( ! url ) continue; | ||
|
||
const strippedUrl = stripProtocol( url ); | ||
if ( ! this.searchReplaceMap[ strippedUrl ] ) continue; | ||
|
||
const domain = new URL( url ).hostname; | ||
const newDomain = `${ this.slugifyDomain( domain ) }.${ this.landoDomain }`; | ||
|
||
this.searchReplaceMap[ url ] = `${ this.slugifyDomain( url ) }-${ site.blogId }.${ | ||
this.landoDomain | ||
}`; | ||
this.searchReplaceMap[ stripProtocol( url ) ] = stripProtocol( | ||
replaceDomain( url, newDomain ) | ||
); | ||
} | ||
} | ||
|
||
|
@@ -213,6 +245,34 @@ export class DevEnvSyncSQLCommand { | |
await importCommand.run(); | ||
} | ||
|
||
public async fixBlogsTable(): Promise< void > { | ||
const networkSites = this.env.wpSitesSDS?.nodes; | ||
if ( ! networkSites ) { | ||
return; | ||
} | ||
|
||
const queries: string[] = []; | ||
for ( const site of networkSites ) { | ||
if ( ! site?.blogId || ! site?.homeUrl ) { | ||
continue; | ||
} | ||
|
||
const oldDomain = new URL( site.homeUrl ).hostname; | ||
const newDomain = | ||
site.blogId !== 1 | ||
? `${ this.slugifyDomain( oldDomain ) }.${ this.landoDomain }` | ||
: this.landoDomain; | ||
|
||
queries.push( | ||
`UPDATE wp_blogs SET domain = '${ newDomain }' WHERE blog_id = ${ Number( site.blogId ) };` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. custom prefixes is what gives me a pause here. |
||
); | ||
} | ||
|
||
if ( queries.length ) { | ||
await fs.promises.appendFile( this.sqlFile, queries.join( '\n' ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Sequentially runs the commands to export, search-replace, and import the SQL file | ||
* to the local environment | ||
|
@@ -273,6 +333,7 @@ export class DevEnvSyncSQLCommand { | |
} | ||
|
||
await this.runSearchReplace(); | ||
await this.fixBlogsTable(); | ||
console.log( `${ chalk.green( '✓' ) } Search-replace operation is complete` ); | ||
} catch ( err ) { | ||
const error = err as Error; | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node 18.17.0 has
URL.canParse()
; hopefully, we can use it instead of this code someday.