Update urls in DB if site is being created from existing folder - #962
Conversation
| interface StartedSiteDetails extends StoppedSiteDetails { | ||
| running: true; | ||
|
|
||
| port: number; |
There was a problem hiding this comment.
Missed removing it in this my PR, so decided to remove in this PR
| if ( ! ( await pathExists( nodePath.join( path, 'wp-config.php' ) ) ) ) { | ||
| await installSqliteIntegration( path ); | ||
| } else { | ||
| await updateSiteUrlToLocal( details.id ); |
There was a problem hiding this comment.
I was thinking about different options and places to do it, but this one looks as the most correct one, since existence of wp-config.php tells us - was the site already initialized or not.
There was a problem hiding this comment.
Looks like a good approach to fix the URL when the site is created.
| // This method can be overridden by subclasses to prepare the SQL file before import. | ||
| } | ||
|
|
||
| protected async replaceSiteUrl( siteId: string ) { |
There was a problem hiding this comment.
Moved it outside as a separate function since now it's used not only for import flow, so it's better to move it outside of import/export modules.
|
|
||
| for ( const urlToReplace of oldUrlVariants ) { | ||
| const { stderr, exitCode } = await server.executeWpCliCommand( | ||
| `search-replace '${ urlToReplace }' '${ studioUrl }' --skip-columns=guid`, |
There was a problem hiding this comment.
The whole function is totally the same as it was before, so not necessary to review it, but two things:
- Renamed to
updateSiteUrlToLocal. - I added
--skip-columns=guidto skip theguidcolumn of posts, which is considered, AFAIU, as a best practice in WP world. I know that in thisTesting instructionsand similar cases it won't be a problem, but I believe that it's worth adding, sinceguidis just id, despite that fact that they look like urls.
There was a problem hiding this comment.
Thanks for summarizing the changes.
| if ( ! ( await pathExists( nodePath.join( path, 'wp-config.php' ) ) ) ) { | ||
| await installSqliteIntegration( path ); | ||
| } else { | ||
| await updateSiteUrlToLocal( details.id ); |
There was a problem hiding this comment.
Looks like a good approach to fix the URL when the site is created.
|
|
||
| for ( const urlToReplace of oldUrlVariants ) { | ||
| const { stderr, exitCode } = await server.executeWpCliCommand( | ||
| `search-replace '${ urlToReplace }' '${ studioUrl }' --skip-columns=guid`, |
There was a problem hiding this comment.
Thanks for summarizing the changes.
Related issues
Proposed Changes
The initial issue was about broken URLs for images, but actually we have much bigger problems (Antonio was right here) - in 100% cases the website is completely broken. But this PR indeed fixes both - images and broken wp-admin.
Testing Instructions
1.1. Create a few websites (e.g. three)
1.2 Open wp admin for the last one (let's assume that it has port 8883) and create a new post with adding some image there
1.3 Remove the last one, but keep a folder on your laptop
1.4 Remove other websites, together with folders, we don't need them anymore
/wp-content/databases/.ht.sqliteand check for examplewp-options->home||siteurl)Local pathin the creation dialogwp_options,wp_usersandwp_posts)