Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 3c92135

Browse files
authored
Merge pull request #10 from liquidweb/feature/cleanup-table-installation
Cleanup the table installation script
2 parents a7322a3 + 874176c commit 3c92135

File tree

4 files changed

+99
-49
lines changed

4 files changed

+99
-49
lines changed

includes/class-wc-custom-order-table-install.php

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,43 @@
99
class WC_Custom_Order_Table_Install {
1010

1111
/**
12-
* The database table schema version.
13-
*
14-
* @var int
15-
*/
16-
protected $table_version = 1;
17-
18-
19-
/**
20-
* Actions to perform on plugin activation.
21-
*/
22-
public function activate() {
23-
$this->maybe_install_tables();
24-
}
25-
26-
/**
27-
* Retrieve the latest table schema version.
28-
*
29-
* @return int The latest schema version.
12+
* The option key that contains the current schema version.
3013
*/
31-
public function get_latest_table_version() {
32-
return absint( $this->table_version );
33-
}
14+
const SCHEMA_VERSION_KEY = 'wc_orders_table_version';
3415

3516
/**
36-
* Retrieve the current table version from the options table.
17+
* The database table schema version.
3718
*
38-
* @return int The current schema version.
19+
* @var int
3920
*/
40-
public function get_installed_table_version() {
41-
return absint( get_option( 'wc_orders_table_version' ) );
42-
}
21+
protected static $table_version = 1;
4322

4423
/**
45-
* Install or update the tables if the site is not using the current schema.
24+
* Actions to perform on plugin activation.
4625
*/
47-
protected function maybe_install_tables() {
48-
if ( $this->get_installed_table_version() < $this->get_latest_table_version() ) {
49-
$this->install_tables();
26+
public static function activate() {
27+
// We're already on the latest schema version.
28+
if ( (int) self::$table_version === (int) get_option( self::SCHEMA_VERSION_KEY ) ) {
29+
return false;
5030
}
31+
32+
self::install_tables();
5133
}
5234

5335
/**
5436
* Perform the database delta to create the table.
5537
*
5638
* @global $wpdb
5739
*/
58-
protected function install_tables() {
40+
protected static function install_tables() {
5941
global $wpdb;
6042

6143
// Load wp-admin/includes/upgrade.php, which defines dbDelta().
6244
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
6345

64-
$collate = '';
65-
66-
if ( $wpdb->has_cap( 'collation' ) ) {
67-
$collate = $wpdb->get_charset_collate();
68-
}
69-
70-
$table = wc_custom_order_table()->get_table_name();
71-
$tables = "
46+
$table = wc_custom_order_table()->get_table_name();
47+
$collate = $wpdb->get_charset_collate();
48+
$tables = "
7249
CREATE TABLE {$table} (
7350
order_id BIGINT UNSIGNED NOT NULL,
7451
order_key varchar(100) NOT NULL,
@@ -119,6 +96,6 @@ protected function install_tables() {
11996
dbDelta( $tables );
12097

12198
// Store the table version in the options table.
122-
update_option( 'wc_orders_table_version', $this->get_latest_table_version() );
99+
update_option( self::SCHEMA_VERSION_KEY, (int) self::$table_version, false );
123100
}
124101
}

tests/test-installation.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,82 @@ public function test_table_is_created_on_plugin_activation() {
2525
'Upon activation, the table should be created.'
2626
);
2727
}
28+
29+
public function test_table_is_only_installed_if_it_does_not_already_exist() {
30+
self::reactivate_plugin();
31+
32+
$this->assertTrue(
33+
self::orders_table_exists(),
34+
'Upon activation, the table should be created.'
35+
);
36+
37+
// Deactivate, then re-activate the plugin.
38+
self::reactivate_plugin();
39+
40+
$this->assertTrue(
41+
self::orders_table_exists(),
42+
'The table should still exist, just as it was.'
43+
);
44+
}
45+
46+
public function test_can_install_table() {
47+
$this->assertFalse(
48+
self::orders_table_exists(),
49+
'The wp_woocommerce_orders table should not exist at the beginning of this test.'
50+
);
51+
52+
WC_Custom_Order_Table_Install::activate();
53+
54+
$this->assertTrue(
55+
self::orders_table_exists(),
56+
'Upon activation, the table should be created.'
57+
);
58+
$this->assertNotEmpty(
59+
get_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY ),
60+
'The schema version should be stored in the options table.'
61+
);
62+
}
63+
64+
public function test_returns_early_if_already_on_latest_schema_version() {
65+
WC_Custom_Order_Table_Install::activate();
66+
67+
$this->assertFalse(
68+
WC_Custom_Order_Table_Install::activate(),
69+
'The activate() method should return false if the schema versions match.'
70+
);
71+
}
72+
73+
public function test_can_upgrade_table() {
74+
WC_Custom_Order_Table_Install::activate();
75+
76+
// Get the current schema version, then increment it.
77+
$property = new ReflectionProperty( 'WC_Custom_Order_Table_Install', 'table_version' );
78+
$property->setAccessible( true );
79+
$version = $property->getValue();
80+
$property->setValue( $version + 1 );
81+
82+
// Run the activation script again.
83+
WC_Custom_Order_Table_Install::activate();
84+
85+
$this->assertEquals(
86+
$version + 1,
87+
get_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY ),
88+
'The schema version should have been incremented.'
89+
);
90+
}
91+
92+
public function test_current_schema_version_is_not_autoloaded() {
93+
global $wpdb;
94+
95+
WC_Custom_Order_Table_Install::activate();
96+
97+
$this->assertEquals(
98+
'no',
99+
$wpdb->get_var( $wpdb->prepare(
100+
"SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1",
101+
WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY
102+
) ),
103+
'The schema version should not be autoloaded.'
104+
);
105+
}
28106
}

tests/testcase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ protected static function drop_orders_table() {
3131
global $wpdb;
3232

3333
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_orders" );
34+
35+
delete_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY );
3436
}
3537

3638
/**

wc-custom-order-table.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@
2626
}
2727

2828
/**
29-
* Installation procedure for the plugin.
30-
*
31-
* This function is responsible for creating the new plugin database tables.
29+
* Install the database tables upon plugin activation.
3230
*/
33-
function wc_custom_order_table_install() {
34-
$installer = new WC_Custom_Order_Table_Install();
35-
$installer->activate();
36-
}
37-
38-
register_activation_hook( __FILE__, 'wc_custom_order_table_install' );
31+
register_activation_hook( __FILE__, array( 'WC_Custom_Order_Table_Install', 'activate' ) );
3932

4033
/**
4134
* Retrieve an instance of the WC_Custom_Order_Table class.

0 commit comments

Comments
 (0)