From 67747b43e24c6e1cf73bc022e620491d33891f65 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 7 Nov 2024 10:39:50 +0200 Subject: [PATCH 1/2] WIP migration script --- admin-page.php | 4 + load.php | 1 + mysql-to-sqlite-migration.php | 142 ++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 mysql-to-sqlite-migration.php diff --git a/admin-page.php b/admin-page.php index 6a44c0e7..dace6d37 100644 --- a/admin-page.php +++ b/admin-page.php @@ -111,9 +111,13 @@ function sqlite_integration_admin_screen() {

+ + + + get_results( 'SHOW TABLES' ) ); +} + +/** + * Create a table in the SQLite database. + * + * @param string $table_name The name of the table to create. + * @param string $create_table_query The CREATE TABLE query to execute. + */ +function sqlite_integration_create_table( $table_name ) { + global $wpdb, $sqlite_db; + // Get the table structure. + $table_structure = $wpdb->get_results( "SHOW CREATE TABLE $table_name" ); + // Execute the CREATE TABLE query. + $sqlite_db->query( $table_structure[0]->{'Create Table'} ); +} + +/** + * Migrate data for a single table. + * + * @param string $table_name The name of the table to migrate data for. + */ +function sqlite_integration_migrate_table( $table_name ) { + global $wpdb, $sqlite_db; + // Get the data from the MySQL table, and insert it into the SQLite table. + $data = $wpdb->get_results( "SELECT * FROM $table_name" ); + foreach ( $data as $row ) { + $sqlite_db->insert( $table_name, (array) $row ); + } +} + +/** + * Run AJAX action to migrate a table. + */ +function sqlite_integration_migrate_table_ajax() { + if ( ! isset( $_POST['table_name'] ) ) { + wp_send_json_error( 'Table name is required' ); + } + $table_name = sanitize_text_field( $_POST['table_name'] ); + sqlite_integration_create_table( $table_name ); + sqlite_integration_migrate_table( $table_name ); + wp_send_json_success( 'Table migrated' ); +} +add_action( 'wp_ajax_sqlite_integration_migrate_table', 'sqlite_integration_migrate_table_ajax' ); + +/** + * Run AJAX action to add the db.php file. + */ +function sqlite_integration_add_db_php_file_ajax() { + // Check if the sqlite_plugin_copy_db_file function exists. + if ( ! function_exists( 'sqlite_plugin_copy_db_file' ) ) { + require_once __DIR__ . '/activate.php'; + } + sqlite_plugin_copy_db_file(); + wp_send_json_success( 'db.php file added' ); +} +add_action( 'wp_ajax_sqlite_integration_add_db_php_file', 'sqlite_integration_add_db_php_file_ajax' ); + +/** + * Add a script to the admin page to handle the migration process. + */ +function sqlite_integration_add_admin_script() { + ?> + + Date: Thu, 7 Nov 2024 13:05:03 +0200 Subject: [PATCH 2/2] minor refactor --- activate.php | 26 ++---------------------- admin-notices.php | 8 ++++---- admin-page.php | 39 ++++++++---------------------------- constants.php | 9 +-------- deactivate.php | 2 +- db.copy => wp-content/db.php | 20 ++++++++---------- 6 files changed, 24 insertions(+), 80 deletions(-) rename db.copy => wp-content/db.php (57%) diff --git a/activate.php b/activate.php index e91e1819..2f1bb65f 100644 --- a/activate.php +++ b/activate.php @@ -84,29 +84,7 @@ function sqlite_plugin_copy_db_file() { // Place database drop-in if not present yet, except in case there is // another database drop-in present already. - if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { - // Init the filesystem to allow copying the file. - global $wp_filesystem; - - require_once ABSPATH . '/wp-admin/includes/file.php'; - - // Init the filesystem if needed, then copy the file, replacing contents as needed. - if ( ( $wp_filesystem || WP_Filesystem() ) && $wp_filesystem->touch( $destination ) ) { - - // Get the db.copy.php file contents, replace placeholders and write it to the destination. - $file_contents = str_replace( - array( - '{SQLITE_IMPLEMENTATION_FOLDER_PATH}', - '{SQLITE_PLUGIN}', - ), - array( - __DIR__, - str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ), - ), - file_get_contents( __DIR__ . '/db.copy' ) - ); - - $wp_filesystem->put_contents( $destination, $file_contents ); - } + if ( ! defined( 'SQLITE_DROPIN' ) && ! file_exists( $destination ) ) { + @symlink( __DIR__ . '/wp-content/db.php', $destination ); // phpcs:ignore } } diff --git a/admin-notices.php b/admin-notices.php index 8eaf2531..7f5e273a 100644 --- a/admin-notices.php +++ b/admin-notices.php @@ -29,18 +29,18 @@ function sqlite_plugin_admin_notice() { } /* - * If the SQLITE_DB_DROPIN_VERSION constant is not defined + * If the SQLITE_DROPIN constant is not defined * but there's a db.php file in the wp-content directory, then the module can't be activated. * The module should not have been activated in the first place * (there's a check in the can-load.php file), but this is a fallback check. */ - if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { + if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DROPIN' ) ) { printf( '

%s

', sprintf( - /* translators: 1: SQLITE_DB_DROPIN_VERSION constant, 2: db.php drop-in path */ + /* translators: 1: SQLITE_DROPIN constant, 2: db.php drop-in path */ __( 'The SQLite Integration module is active, but the %1$s constant is missing. It appears you already have another %2$s file present on your site. ', 'sqlite-database-integration' ), - 'SQLITE_DB_DROPIN_VERSION', + 'SQLITE_DROPIN', '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' ) ); diff --git a/admin-page.php b/admin-page.php index dace6d37..c6816498 100644 --- a/admin-page.php +++ b/admin-page.php @@ -32,7 +32,7 @@ function sqlite_integration_admin_screen() {
- +

@@ -55,41 +55,18 @@ function sqlite_integration_admin_screen() {

- - -
-

- ' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' - ); - ?> -

-
- + +
+

' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' ); ?> - - -

-

- ' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' - ); - ?> -

-
- +

+

@@ -133,7 +110,7 @@ function sqlite_integration_admin_screen() { function sqlite_plugin_adminbar_item( $admin_bar ) { global $wpdb; - if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { + if ( defined( 'SQLITE_DROPIN' ) && SQLITE_DROPIN && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { $title = '' . __( 'Database: SQLite', 'sqlite-database-integration' ) . ''; } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { $title = '' . __( 'Database: MariaDB', 'sqlite-database-integration' ) . ''; diff --git a/constants.php b/constants.php index 15e6772a..ce88d557 100644 --- a/constants.php +++ b/constants.php @@ -8,14 +8,7 @@ // Temporary - This will be in wp-config.php once SQLite is merged in Core. if ( ! defined( 'DB_ENGINE' ) ) { - if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { - define( 'DB_ENGINE', 'sqlite' ); - } elseif ( defined( 'DATABASE_ENGINE' ) ) { - // backwards compatibility with previous versions of the plugin. - define( 'DB_ENGINE', DATABASE_ENGINE ); - } else { - define( 'DB_ENGINE', 'mysql' ); - } + define( 'DB_ENGINE', 'mysql' ); } /** diff --git a/deactivate.php b/deactivate.php index 2b79d9a3..d9284bee 100644 --- a/deactivate.php +++ b/deactivate.php @@ -12,7 +12,7 @@ * When the plugin gets merged in wp-core, this is not to be ported. */ function sqlite_plugin_remove_db_file() { - if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { + if ( ! defined( 'SQLITE_DROPIN' ) || ! SQLITE_DROPIN || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { return; } diff --git a/db.copy b/wp-content/db.php similarity index 57% rename from db.copy rename to wp-content/db.php index 0b0797e8..416a4bdf 100644 --- a/db.copy +++ b/wp-content/db.php @@ -11,17 +11,12 @@ * @package wp-sqlite-integration */ -define( 'SQLITE_DB_DROPIN_VERSION', '1.8.0' ); - -// Tweak to allow copy-pasting the file without having to run string-replacements. -$sqlite_plugin_implementation_folder_path = '{SQLITE_IMPLEMENTATION_FOLDER_PATH}'; -if ( ! file_exists( $sqlite_plugin_implementation_folder_path ) ) { // Check that the folder exists. - $sqlite_plugin_implementation_folder_path = realpath( __DIR__ . '/plugins/sqlite-database-integration' ); +if ( ! defined( 'ABSPATH' ) ) { + exit; } -// Bail early if the SQLite implementation was not located in the plugin. -if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php' ) ) { - return; +if ( ! defined( 'SQLITE_DROPIN' ) ) { + define( 'SQLITE_DROPIN', true ); } // Constant for backward compatibility. @@ -34,7 +29,7 @@ } // Require the implementation from the plugin. -require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php'; +require_once dirname( __DIR__ ) . '/wp-includes/sqlite/db.php'; // Activate the performance-lab plugin if it is not already activated. add_action( @@ -46,11 +41,12 @@ function() { if ( ! function_exists( 'activate_plugin' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } - if ( is_plugin_inactive( '{SQLITE_PLUGIN}' ) ) { + $plugin_file = str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ); + if ( is_plugin_inactive( $plugin_file ) ) { // If `activate_plugin()` returns a value other than null (like WP_Error), // the plugin could not be found. Try with a hardcoded string, // because that probably means the file was directly copy-pasted. - if ( null !== activate_plugin( '{SQLITE_PLUGIN}', '', false, true ) ) { + if ( null !== activate_plugin( $plugin_file, '', false, true ) ) { activate_plugin( 'sqlite-database-integration/load.php', '', false, true ); } }