CREATE TABLE IF NOT EXISTS `wp_tmapp_registration_tokens` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `mobile_normalized` VARCHAR(20) NOT NULL,
  `token_hash` CHAR(64) NOT NULL,
  `expires_at` DATETIME NOT NULL,
  `used_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `token_hash` (`token_hash`),
  KEY `mobile_normalized` (`mobile_normalized`),
  KEY `expires_at` (`expires_at`),
  KEY `used_at` (`used_at`)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `wp_tmapp_registration_tokens`
  ADD COLUMN IF NOT EXISTS `mobile_normalized` VARCHAR(20) NOT NULL AFTER `id`;

ALTER TABLE `wp_tmapp_registration_tokens`
  ADD COLUMN IF NOT EXISTS `token_hash` CHAR(64) NOT NULL AFTER `mobile_normalized`;

ALTER TABLE `wp_tmapp_registration_tokens`
  ADD COLUMN IF NOT EXISTS `expires_at` DATETIME NOT NULL AFTER `token_hash`;

ALTER TABLE `wp_tmapp_registration_tokens`
  ADD COLUMN IF NOT EXISTS `used_at` DATETIME DEFAULT NULL AFTER `expires_at`;

ALTER TABLE `wp_tmapp_registration_tokens`
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP AFTER `used_at`;
