USE `smb`;
CREATE TABLE orders_original (SELECT * FROM `orders`);


CREATE TABLE `smb`.`orders_new` (
  `id` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `realm_id` char(127) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(127) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `comments` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `url` varchar(2048) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_on` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_by` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP /*!40100 ON UPDATE CURRENT_TIMESTAMP */,
  `modified_by` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `state` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `merchant_id` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `currency_code` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `timezone` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `country_code` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL,
  `scheduled_start` timestamp NULL DEFAULT NULL,
  `scheduled_end` timestamp NULL DEFAULT NULL,
  `json` longtext COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`id`),
  KEY `realm_merchant` (`realm_id`,`merchant_id`),
  KEY `realm_name` (`realm_id`,`name`),
  KEY `realm_state_name` (`realm_id`,`state`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `orders_new` SELECT * FROM `orders`;

RENAME TABLE `orders` TO `orders_bak`, `orders_new` TO `orders`;

-- Once happy...
-- DROP TABLE IF EXISTS `orders_original`;
-- DROP TABLE IF EXISTS `orders_bak`;

