Automattic\WooCommerce\Internal\Fulfillments

FulfillmentsController::maybe_create_db_tablesprivateWC 1.0

Create the database tables if they do not exist.

Method of the class: FulfillmentsController{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->maybe_create_db_tables(): void;

FulfillmentsController::maybe_create_db_tables() code WC 10.3.3

private function maybe_create_db_tables(): void {
	global $wpdb;

	if ( get_option( 'woocommerce_fulfillments_db_tables_created', false ) ) {
		// The tables already exist, no need to create them again.
		return;
	}

	// Drop the tables if they exist, to ensure a clean slate.
	// If one table exists and the other does not, it will be an issue.
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wc_order_fulfillments" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wc_order_fulfillment_meta" );

	// Bulk delete order fulfillment status meta from legacy and HPOS order tables.
	$this->bulk_delete_order_fulfillment_status_meta();

	$collate       = '';
	$container     = wc_get_container();
	$database_util = $container->get( DatabaseUtil::class );

	$max_index_length = $database_util->get_max_index_length();
	if ( $wpdb->has_cap( 'collation' ) ) {
		$collate = $wpdb->get_charset_collate();
	}

	$schema = "CREATE TABLE {$wpdb->prefix}wc_order_fulfillments (
		fulfillment_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
		entity_type varchar(255) NOT NULL,
		entity_id bigint(20) unsigned NOT NULL,
		status varchar(255) NOT NULL,
		is_fulfilled tinyint(1) NOT NULL DEFAULT 0,
		date_updated datetime NOT NULL,
		date_deleted datetime NULL,
		PRIMARY KEY (fulfillment_id),
		KEY entity_type_id (entity_type({$max_index_length}), entity_id)
	) $collate;
	CREATE TABLE {$wpdb->prefix}wc_order_fulfillment_meta (
		meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
		fulfillment_id bigint(20) unsigned NOT NULL,
		meta_key varchar(255) NULL,
		meta_value longtext NULL,
		date_updated datetime NOT NULL,
		date_deleted datetime NULL,
		PRIMARY KEY (meta_id),
		KEY meta_key (meta_key({$max_index_length})),
		KEY fulfillment_id (fulfillment_id)
	) $collate;";

	$database_util->dbdelta( $schema );

	// Update the option to indicate that the tables have been created.
	update_option( 'woocommerce_fulfillments_db_tables_created', true );
}