Skip to content

Commit

Permalink
fix(dev-env) sync: don't slugify domain if it matches the primary dom…
Browse files Browse the repository at this point in the history
…ain (#2153)

* fix(dev-env): do not slugify domain if it matches the primary domain

* fix: domain names for `wp_blogs` table

* Add blogId=1 in DevEnvSyncSQL test case to match the data returned by sds

* test: update tests

---------

Co-authored-by: Volodymyr Kolesnykov <[email protected]>
  • Loading branch information
rinatkhaziev and sjinks authored Dec 10, 2024
1 parent 8deb2b8 commit e15866b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion __tests__/commands/dev-env-sync-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe( 'commands/DevEnvSyncSQLCommand', () => {
name: 'test-env',
wpSitesSDS: {
nodes: [
{
blogId: 1,
homeUrl: 'https://test.go-vip.com',
},
{
blogId: 2,
homeUrl: 'https://subsite.com',
Expand All @@ -38,6 +42,10 @@ describe( 'commands/DevEnvSyncSQLCommand', () => {
blogId: 3,
homeUrl: 'https://another.com/path',
},
{
blogId: 4,
homeUrl: 'https://test.go-vip.com/path',
},
],
},
};
Expand Down Expand Up @@ -69,13 +77,14 @@ describe( 'commands/DevEnvSyncSQLCommand', () => {
it( 'should return a map of search-replace values for multisite', () => {
const cmd = new DevEnvSyncSQLCommand( app, msEnv, 'test-slug', lando );
cmd.slug = 'test-slug';
cmd.siteUrls = [ 'https://test.go-vip.com', 'http://subsite.com', 'http://another.com/path' ];
cmd.siteUrls = msEnv.wpSitesSDS.nodes.map( node => node.homeUrl );
cmd.generateSearchReplaceMap();

expect( cmd.searchReplaceMap ).toEqual( {
'test.go-vip.com': 'test-slug.vipdev.lndo.site',
'subsite.com': 'subsite-com.test-slug.vipdev.lndo.site',
'another.com/path': 'another-com.test-slug.vipdev.lndo.site/path',
'test.go-vip.com/path': 'test-slug.vipdev.lndo.site/path',
} );
} );
} );
Expand Down
13 changes: 11 additions & 2 deletions src/commands/dev-env-sync-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ export class DevEnvSyncSQLCommand {
const networkSites = this.env.wpSitesSDS?.nodes;
if ( ! networkSites ) return;

const primaryUrl = networkSites.find( site => site?.blogId === 1 )?.homeUrl;
const primaryDomain = primaryUrl ? new URL( primaryUrl ).hostname : '';

for ( const site of networkSites ) {
if ( ! site?.blogId || site.blogId === 1 ) continue;

Expand All @@ -210,7 +213,10 @@ export class DevEnvSyncSQLCommand {
if ( ! this.searchReplaceMap[ strippedUrl ] ) continue;

const domain = new URL( url ).hostname;
const newDomain = `${ this.slugifyDomain( domain ) }.${ this.landoDomain }`;
const newDomain =
primaryDomain === domain
? this.landoDomain
: `${ this.slugifyDomain( domain ) }.${ this.landoDomain }`;

this.searchReplaceMap[ stripProtocol( url ) ] = stripProtocol(
replaceDomain( url, newDomain )
Expand Down Expand Up @@ -266,6 +272,9 @@ CALL vip_sync_update_blog_domains();
DROP PROCEDURE vip_sync_update_blog_domains;
`;

const primaryUrl = networkSites.find( site => site?.blogId === 1 )?.homeUrl;
const primaryDomain = primaryUrl ? new URL( primaryUrl ).hostname : '';

const queries: string[] = [];
for ( const site of networkSites ) {
if ( ! site?.blogId || ! site?.homeUrl ) {
Expand All @@ -274,7 +283,7 @@ DROP PROCEDURE vip_sync_update_blog_domains;

const oldDomain = new URL( site.homeUrl ).hostname;
const newDomain =
site.blogId !== 1
primaryDomain !== oldDomain
? `${ this.slugifyDomain( oldDomain ) }.${ this.landoDomain }`
: this.landoDomain;

Expand Down

0 comments on commit e15866b

Please sign in to comment.