関数
Schema::addTables()
説明 説明
Note that we need to update some MyISAM tables to InnoDB so we can take advantage of foreign key constraints.
Tables use InnoDB by default as of MySQL 5.7 but there are still sites out there that installed WordPress with an older version, resulting in all tables (posts, postmeta, etc.) being MyISAM
ファイル: src/Schema.php
public static function addTables() { global $wpdb; // Get Welcart tables first. This call will throw a fatal error if Welcart // isn't activated, which is why we do it first $skust = usces_get_tablename('usces_skus'); $ordercartt = usces_get_tablename('usces_ordercart'); require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $charset_collate = $wpdb->get_charset_collate(); $cst = self::getComboSetsTable(); $csgt = self::getComboSetGroupsTable(); $csgit = self::getComboSetGroupItemsTable(); $csgioct = self::getComboSetGroupItemsOrderCartTable(); $csgiocmt = self::getComboSetGroupItemsOrderCartMetaTable(); /** * Change engine for Welcart tables to InnoDB */ foreach ([$wpdb->posts, $skust, $ordercartt] as $table) { $sql = 'SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' . '\'' . DB_NAME . '\'' . " AND TABLE_NAME = '{$table}'"; $engine = $wpdb->get_var($sql); if ($engine === 'MyISAM') { $wpdb->query("ALTER TABLE {$table} ENGINE = InnoDB;"); } } if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', [$cst])) !== $cst) { $enableItemOptions = (int)self::ENABLE_ITEM_OPTIONS_DEFAULT; $enableMultiprice = (int)self::ENABLE_MULTIPRICE_DEFAULT; $sql = "CREATE TABLE {$cst} ( ID BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT, post_id BIGINT(20) UNSIGNED NOT NULL, sku_meta_id BIGINT(20) UNSIGNED NOT NULL UNIQUE, enable_item_options TINYINT(1) NOT NULL DEFAULT {$enableItemOptions}, enable_multiprice TINYINT(1) NOT NULL DEFAULT {$enableMultiprice}, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, FOREIGN KEY (post_id) REFERENCES {$wpdb->posts}(ID) ON DELETE CASCADE ) ENGINE=InnoDB {$charset_collate};"; dbDelta($sql); } if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', [$csgt])) !== $csgt) { $sql = "CREATE TABLE {$csgt} ( ID BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT, combo_set_id BIGINT(20) UNSIGNED NOT NULL, group_label VARCHAR(200), optional TINYINT(1) NOT NULL DEFAULT 0, allow_multiple_selections TINYINT(1) NOT NULL DEFAULT 0, position INTEGER UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, FOREIGN KEY (combo_set_id) REFERENCES {$cst}(ID) ON DELETE CASCADE ) ENGINE=InnoDB {$charset_collate};"; dbDelta($sql); } if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', [$csgit])) !== $csgit) { $sql = "CREATE TABLE {$csgit} ( ID BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT, group_id BIGINT(20) UNSIGNED NOT NULL, welitem_sku_meta_id BIGINT(20) UNSIGNED NOT NULL, item_label VARCHAR(200), item_quantity SMALLINT NOT NULL DEFAULT 1, price_modifier INTEGER NOT NULL DEFAULT 0, position INTEGER UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, FOREIGN KEY (group_id) REFERENCES {$csgt}(ID) ON DELETE CASCADE ) ENGINE=InnoDB {$charset_collate};"; dbDelta($sql); } if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', [$csgioct])) !== $csgioct) { $sql = "CREATE TABLE {$csgioct} ( cart_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, combo_set_cart_id BIGINT(20) UNSIGNED NOT NULL, order_id BIGINT(20) NOT NULL, group_id INT(3) NOT NULL DEFAULT '0', row_index INT(3) NOT NULL, post_id BIGINT(20) NOT NULL, item_code VARCHAR(100) NOT NULL, item_name VARCHAR(250) NOT NULL, cprice DECIMAL(12, 0) DEFAULT NULL, sku_code VARCHAR(100) NOT NULL, sku_name VARCHAR(250) DEFAULT NULL, price DECIMAL(12, 0) NOT NULL, quantity FLOAT NOT NULL, unit VARCHAR(50) DEFAULT NULL, tax DECIMAL(10, 0) DEFAULT NULL, destination_id INT(10) DEFAULT NULL, cart_serial TEXT, PRIMARY KEY (cart_id), KEY order_id (order_id), KEY post_id (post_id), KEY item_code (item_code), KEY item_name (item_name(191)), KEY sku_code (sku_code), KEY sku_name (sku_name(191)), FOREIGN KEY (combo_set_cart_id) REFERENCES {$ordercartt}(cart_id) ON DELETE CASCADE ) AUTO_INCREMENT=1000 ENGINE=InnoDB {$charset_collate};"; dbDelta($sql); } if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', [$csgiocmt])) !== $csgiocmt) { $sql = "CREATE TABLE {$csgiocmt} ( cartmeta_id BIGINT(20) NOT NULL AUTO_INCREMENT, cart_id BIGINT(20) UNSIGNED NOT NULL, meta_type VARCHAR(100) NOT NULL, meta_key VARCHAR(255) DEFAULT NULL, meta_value LONGTEXT, PRIMARY KEY (cartmeta_id), KEY cart_id (cart_id), KEY meta_key (meta_key(191)), FOREIGN KEY (cart_id) REFERENCES {$csgioct}(cart_id) ON DELETE CASCADE ) ENGINE=InnoDB {$charset_collate};"; dbDelta($sql); } }