-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatic sqlite-database-integration upgrade (#136)
* feat: Download latest sqlite-databse-integration release tag Improve stability and enable version comparisons for future upgrades. * feat: Starting the app updates sqlite-database-integration installation Ensure new sites receive the latest sqlite-database-integration fixes and improvements. * feat: Starting a site updates an outdated sqlite-integration-plugin Ensure existing sites receive the latest fixes and features. * fix: Avoid unnecessary sqlite-database-integration upgrades If version comparison fails, assume the installed version is the latest. For example, fetching the latest version while offline will result in a comparison failure. * feat: Cache sqlite-database-integration versions for app session Avoid repeatedly fetching the latest versions each time a site starts. * refactor: Remove unused import * refactor: Remove unused references to specific past release versions We only ever install the latest version, so we do not need the unnecessary complexity of fetching past versions. Also, this allows us to use the same download source the `sqlite-database-integration` plugin throughout the source. * refactor: Remove `-main` branch suffix from SQLite install The `-main` suffix is no longer accurate now that we rely upon release tags rather than downloading the latest development branch.
- Loading branch information
Showing
8 changed files
with
189 additions
and
13 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import path from 'path'; | ||
import * as Sentry from '@sentry/electron/main'; | ||
import fs from 'fs-extra'; | ||
import semver from 'semver'; | ||
import { downloadSqliteIntegrationPlugin } from '../../vendor/wp-now/src/download'; | ||
import getSqlitePath from '../../vendor/wp-now/src/get-sqlite-path'; | ||
|
||
export async function updateLatestSqliteVersion() { | ||
let shouldOverwrite = false; | ||
const installedPath = getSqlitePath(); | ||
const installedFiles = ( await fs.pathExists( installedPath ) ) | ||
? await fs.readdir( installedPath ) | ||
: []; | ||
if ( installedFiles.length !== 0 ) { | ||
shouldOverwrite = await isSqliteInstallationOutdated( installedPath ); | ||
} | ||
|
||
await downloadSqliteIntegrationPlugin( { overwrite: shouldOverwrite } ); | ||
|
||
await removeLegacySqliteIntegrationPlugin( installedPath ); | ||
} | ||
|
||
export async function isSqliteInstallationOutdated( installationPath: string ): Promise< boolean > { | ||
const installedVersion = getSqliteVersionFromInstallation( installationPath ); | ||
const latestVersion = await getLatestSqliteVersion(); | ||
|
||
if ( ! installedVersion ) { | ||
return true; | ||
} | ||
|
||
if ( ! latestVersion ) { | ||
return false; | ||
} | ||
|
||
try { | ||
return semver.lt( installedVersion, latestVersion ); | ||
} catch ( _error ) { | ||
return false; | ||
} | ||
} | ||
|
||
function getSqliteVersionFromInstallation( installationPath: string ): string { | ||
let versionFileContent = ''; | ||
try { | ||
versionFileContent = fs.readFileSync( path.join( installationPath, 'load.php' ), 'utf8' ); | ||
} catch ( err ) { | ||
return ''; | ||
} | ||
const matches = versionFileContent.match( /\s\*\sVersion:\s*([0-9a-zA-Z.-]+)/ ); | ||
return matches?.[ 1 ] || ''; | ||
} | ||
|
||
let latestSqliteVersionsCache: string | null = null; | ||
|
||
async function getLatestSqliteVersion() { | ||
// Only fetch the latest version once per app session | ||
if ( latestSqliteVersionsCache ) { | ||
return latestSqliteVersionsCache; | ||
} | ||
|
||
try { | ||
const response = await fetch( | ||
'https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&slug=sqlite-database-integration' | ||
); | ||
const data: Record< string, string > = await response.json(); | ||
latestSqliteVersionsCache = data.version; | ||
} catch ( _error ) { | ||
// Discard the failed fetch, return the cache | ||
} | ||
|
||
return latestSqliteVersionsCache; | ||
} | ||
|
||
/** | ||
* Removes legacy `sqlite-integration-plugin` installations from the specified | ||
* installation path that including a `-main` branch suffix. | ||
* | ||
* @param installPath - The path where the plugin is installed. | ||
* | ||
* @returns A promise that resolves when the plugin is successfully removed. | ||
* | ||
* @todo Remove this function after a few releases. | ||
*/ | ||
export async function removeLegacySqliteIntegrationPlugin( installPath: string ) { | ||
try { | ||
const legacySqlitePluginPath = `${ installPath }-main`; | ||
if ( await fs.pathExists( legacySqlitePluginPath ) ) { | ||
await fs.remove( legacySqlitePluginPath ); | ||
} | ||
} catch ( error ) { | ||
// If the removal fails, log the error but don't throw | ||
Sentry.captureException( 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
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