Skip to content

Database configuration and migration #42

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

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/verify-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Verify plugin version

on:
push:
branches:
- main
pull_request:

jobs:
verify-version:
name: Verify plugin version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's that workflow? Let's document its purpose inline or give it a name that explains the intent

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Extract version from "load.php"
id: load_version
run: |
VERSION=$(grep "Version:" load.php | sed "s/.*Version: \([^ ]*\).*/\1/")
echo "load_version=$VERSION" >> $GITHUB_OUTPUT

- name: Extract version from "version.php"
id: const_version
run: |
VERSION=$(php -r "require 'version.php'; echo SQLITE_DRIVER_VERSION;")
echo "const_version=$VERSION" >> $GITHUB_OUTPUT

- name: Compare versions
run: |
if [ "${{ steps.load_version.outputs.load_version }}" != "${{ steps.const_version.outputs.const_version }}" ]; then
echo "Version mismatch detected!"
echo " load.php version: ${{ steps.load_version.outputs.load_version }}"
echo " version.php constant: ${{ steps.const_version.outputs.const_version }}"
exit 1
fi
6 changes: 6 additions & 0 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
* @package wp-sqlite-integration
*/

/**
* Load the "SQLITE_DRIVER_VERSION" constant.
* This constant needs to be updated whenever the plugin version changes!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So on releases?

*/
require_once __DIR__ . '/version.php';

define( 'SQLITE_MAIN_FILE', __FILE__ );

require_once __DIR__ . '/php-polyfills.php';
Expand Down
4 changes: 0 additions & 4 deletions tests/WP_SQLite_Driver_Metadata_Tests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';

use PHPUnit\Framework\TestCase;

class WP_SQLite_Driver_Metadata_Tests extends TestCase {
Expand Down
4 changes: 0 additions & 4 deletions tests/WP_SQLite_Driver_Query_Tests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';

use PHPUnit\Framework\TestCase;

/**
Expand Down
5 changes: 0 additions & 5 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

require_once __DIR__ . '/WP_SQLite_Translator_Tests.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';

use PHPUnit\Framework\TestCase;

class WP_SQLite_Driver_Tests extends TestCase {
Expand Down
4 changes: 0 additions & 4 deletions tests/WP_SQLite_Driver_Translation_Tests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';

use PHPUnit\Framework\TestCase;

class WP_SQLite_Driver_Translation_Tests extends TestCase {
Expand Down
178 changes: 178 additions & 0 deletions tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

use PHPUnit\Framework\TestCase;

class WP_SQLite_Information_Schema_Reconstructor_Tests extends TestCase {
/** @var WP_SQLite_Driver */
private $engine;

/** @var WP_SQLite_Information_Schema_Reconstructor */
private $reconstructor;

/** @var PDO */
private $sqlite;

public static function setUpBeforeClass(): void {
// if ( ! defined( 'PDO_DEBUG' )) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: commented code

// define( 'PDO_DEBUG', true );
// }
if ( ! defined( 'FQDB' ) ) {
define( 'FQDB', ':memory:' );
define( 'FQDBDIR', __DIR__ . '/../testdb' );
}
error_reporting( E_ALL & ~E_DEPRECATED );
if ( ! isset( $GLOBALS['table_prefix'] ) ) {
$GLOBALS['table_prefix'] = 'wptests_';
}
if ( ! isset( $GLOBALS['wpdb'] ) ) {
$GLOBALS['wpdb'] = new stdClass();
$GLOBALS['wpdb']->suppress_errors = false;
$GLOBALS['wpdb']->show_errors = true;
}

// Mock symols that are used for WordPress table reconstruction.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Mock symols that are used for WordPress table reconstruction.
// Mock symbols that are used for WordPress table reconstruction.

if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ );
}
if ( ! function_exists( 'wp_installing' ) ) {
function wp_installing() {
return false;
}
}
if ( ! function_exists( 'wp_get_db_schema' ) ) {
function wp_get_db_schema() {
// Output from "wp_get_db_schema" as of WordPress 6.8.0.
// See: https://github.com/WordPress/wordpress-develop/blob/6.8.0/src/wp-admin/includes/schema.php#L36
return "CREATE TABLE wp_users ( ID bigint(20) unsigned NOT NULL auto_increment, user_login varchar(60) NOT NULL default '', user_pass varchar(255) NOT NULL default '', user_nicename varchar(50) NOT NULL default '', user_email varchar(100) NOT NULL default '', user_url varchar(100) NOT NULL default '', user_registered datetime NOT NULL default '0000-00-00 00:00:00', user_activation_key varchar(255) NOT NULL default '', user_status int(11) NOT NULL default '0', display_name varchar(250) NOT NULL default '', PRIMARY KEY (ID), KEY user_login_key (user_login), KEY user_nicename (user_nicename), KEY user_email (user_email) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_usermeta ( umeta_id bigint(20) unsigned NOT NULL auto_increment, user_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (umeta_id), KEY user_id (user_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_termmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY term_id (term_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_terms ( term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200) NOT NULL default '', slug varchar(200) NOT NULL default '', term_group bigint(10) NOT NULL default 0, PRIMARY KEY (term_id), KEY slug (slug(191)), KEY name (name(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_taxonomy ( term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default 0, taxonomy varchar(32) NOT NULL default '', description longtext NOT NULL, parent bigint(20) unsigned NOT NULL default 0, count bigint(20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), UNIQUE KEY term_id_taxonomy (term_id,taxonomy), KEY taxonomy (taxonomy) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_relationships ( object_id bigint(20) unsigned NOT NULL default 0, term_taxonomy_id bigint(20) unsigned NOT NULL default 0, term_order int(11) NOT NULL default 0, PRIMARY KEY (object_id,term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_commentmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, comment_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY comment_id (comment_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_comments ( comment_ID bigint(20) unsigned NOT NULL auto_increment, comment_post_ID bigint(20) unsigned NOT NULL default '0', comment_author tinytext NOT NULL, comment_author_email varchar(100) NOT NULL default '', comment_author_url varchar(200) NOT NULL default '', comment_author_IP varchar(100) NOT NULL default '', comment_date datetime NOT NULL default '0000-00-00 00:00:00', comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', comment_content text NOT NULL, comment_karma int(11) NOT NULL default '0', comment_approved varchar(20) NOT NULL default '1', comment_agent varchar(255) NOT NULL default '', comment_type varchar(20) NOT NULL default 'comment', comment_parent bigint(20) unsigned NOT NULL default '0', user_id bigint(20) unsigned NOT NULL default '0', PRIMARY KEY (comment_ID), KEY comment_post_ID (comment_post_ID), KEY comment_approved_date_gmt (comment_approved,comment_date_gmt), KEY comment_date_gmt (comment_date_gmt), KEY comment_parent (comment_parent), KEY comment_author_email (comment_author_email(10)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_links ( link_id bigint(20) unsigned NOT NULL auto_increment, link_url varchar(255) NOT NULL default '', link_name varchar(255) NOT NULL default '', link_image varchar(255) NOT NULL default '', link_target varchar(25) NOT NULL default '', link_description varchar(255) NOT NULL default '', link_visible varchar(20) NOT NULL default 'Y', link_owner bigint(20) unsigned NOT NULL default '1', link_rating int(11) NOT NULL default '0', link_updated datetime NOT NULL default '0000-00-00 00:00:00', link_rel varchar(255) NOT NULL default '', link_notes mediumtext NOT NULL, link_rss varchar(255) NOT NULL default '', PRIMARY KEY (link_id), KEY link_visible (link_visible) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_options ( option_id bigint(20) unsigned NOT NULL auto_increment, option_name varchar(191) NOT NULL default '', option_value longtext NOT NULL, autoload varchar(20) NOT NULL default 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), KEY autoload (autoload) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_postmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, post_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_posts ( ID bigint(20) unsigned NOT NULL auto_increment, post_author bigint(20) unsigned NOT NULL default '0', post_date datetime NOT NULL default '0000-00-00 00:00:00', post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content longtext NOT NULL, post_title text NOT NULL, post_excerpt text NOT NULL, post_status varchar(20) NOT NULL default 'publish', comment_status varchar(20) NOT NULL default 'open', ping_status varchar(20) NOT NULL default 'open', post_password varchar(255) NOT NULL default '', post_name varchar(200) NOT NULL default '', to_ping text NOT NULL, pinged text NOT NULL, post_modified datetime NOT NULL default '0000-00-00 00:00:00', post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content_filtered longtext NOT NULL, post_parent bigint(20) unsigned NOT NULL default '0', guid varchar(255) NOT NULL default '', menu_order int(11) NOT NULL default '0', post_type varchar(20) NOT NULL default 'post', post_mime_type varchar(100) NOT NULL default '', comment_count bigint(20) NOT NULL default '0', PRIMARY KEY (ID), KEY post_name (post_name(191)), KEY type_status_date (post_type,post_status,post_date,ID), KEY post_parent (post_parent), KEY post_author (post_author) ) DEFAULT CHARACTER SET utf8mb4;";
}
}
}

// Before each test, we create a new database
public function setUp(): void {
$this->sqlite = new PDO( 'sqlite::memory:' );
$this->engine = new WP_SQLite_Driver(
array(
'connection' => $this->sqlite,
'database' => 'wp',
)
);

$builder = new WP_SQLite_Information_Schema_Builder(
'wp',
WP_SQLite_Driver::RESERVED_PREFIX,
array( $this->engine, 'execute_sqlite_query' )
);

$this->reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
$this->engine,
$builder
);
}

public function testReconstructInformationSchemaTable(): void {
$this->engine->get_pdo()->exec(
'
CREATE TABLE t (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
role TEXT,
score REAL,
priority INTEGER DEFAULT 0,
data BLOB,
UNIQUE (name)
)
'
);
$this->engine->get_pdo()->exec( 'CREATE INDEX idx_score ON t (score)' );
$this->engine->get_pdo()->exec( 'CREATE INDEX idx_role_score ON t (role, priority)' );
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' );
$this->assertEquals( 0, count( $result ) );

$this->reconstructor->ensure_correct_information_schema();
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' );
$this->assertEquals( 1, count( $result ) );

$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
$this->assertSame(
implode(
"\n",
array(
'CREATE TABLE `t` (',
' `id` int NOT NULL AUTO_INCREMENT,',
' `email` text NOT NULL,',
' `name` text NOT NULL,',
' `role` text DEFAULT NULL,',
' `score` float DEFAULT NULL,',
" `priority` int DEFAULT '0',",
' `data` blob DEFAULT NULL,',
' PRIMARY KEY (`id`),',
' KEY `idx_role_score` (`role`(100), `priority`),',
' KEY `idx_score` (`score`),',
' UNIQUE KEY `sqlite_autoindex_t_2` (`name`(100)),',
' UNIQUE KEY `sqlite_autoindex_t_1` (`email`(100))',
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
)
),
$result[0]->{'Create Table'}
);
}

public function testReconstructInformationSchemaTableWithWpTables(): void {
// Create a WP table with any columns.
$this->engine->get_pdo()->exec( 'CREATE TABLE wp_posts ( id INTEGER )' );

// Reconstruct the information schema.
$this->reconstructor->ensure_correct_information_schema();
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "wp_posts"' );
$this->assertEquals( 1, count( $result ) );

// The reconstructed schema should correspond to the original WP table definition.
$result = $this->assertQuery( 'SHOW CREATE TABLE wp_posts' );
$this->assertSame(
implode(
"\n",
array(
'CREATE TABLE `wp_posts` (',
' `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,',
" `post_author` bigint(20) unsigned NOT NULL DEFAULT '0',",
" `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
" `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
' `post_content` longtext NOT NULL,',
' `post_title` text NOT NULL,',
' `post_excerpt` text NOT NULL,',
" `post_status` varchar(20) NOT NULL DEFAULT 'publish',",
" `comment_status` varchar(20) NOT NULL DEFAULT 'open',",
" `ping_status` varchar(20) NOT NULL DEFAULT 'open',",
" `post_password` varchar(255) NOT NULL DEFAULT '',",
" `post_name` varchar(200) NOT NULL DEFAULT '',",
' `to_ping` text NOT NULL,',
' `pinged` text NOT NULL,',
" `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
" `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
' `post_content_filtered` longtext NOT NULL,',
" `post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',",
" `guid` varchar(255) NOT NULL DEFAULT '',",
" `menu_order` int(11) NOT NULL DEFAULT '0',",
" `post_type` varchar(20) NOT NULL DEFAULT 'post',",
" `post_mime_type` varchar(100) NOT NULL DEFAULT '',",
" `comment_count` bigint(20) NOT NULL DEFAULT '0',",
' PRIMARY KEY (`ID`),',
' KEY `post_name` (`post_name`(191)),',
' KEY `type_status_date` (`post_type`, `post_status`, `post_date`, `ID`),',
' KEY `post_parent` (`post_parent`),',
' KEY `post_author` (`post_author`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
)
),
$result[0]->{'Create Table'}
);
}

private function assertQuery( $sql ) {
$retval = $this->engine->query( $sql );
$this->assertNotFalse( $retval );
return $retval;
}
}
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once __DIR__ . '/wp-sqlite-schema.php';
require_once __DIR__ . '/../version.php';
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser-grammar.php';
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser.php';
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser-node.php';
Expand All @@ -13,6 +14,11 @@
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-token.php';
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-translator.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-configurator.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';

/**
* Polyfills for WordPress functions
Expand Down
2 changes: 2 additions & 0 deletions tests/tools/dump-sqlite-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-token.php';
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-parser.php';
require_once __DIR__ . '/../../wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-configurator.php';
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';

$driver = new WP_SQLite_Driver(
array(
Expand Down
8 changes: 8 additions & 0 deletions version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* The version of the SQLite driver.
*
* This constant needs to be updated whenever the plugin version changes!
*/
define( 'SQLITE_DRIVER_VERSION', '2.1.17-alpha' );
Loading