-- Equivalent of running:
--   php artisan migrate --path=database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php
--
-- Only needed if the `personal_access_tokens` table does not already exist.
-- Run in phpMyAdmin -> select the app database -> SQL tab -> paste -> Go.
--
-- Charset/collation match config/database.php (utf8mb4 / utf8mb4_unicode_ci).

CREATE TABLE IF NOT EXISTS `personal_access_tokens` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tokenable_type` varchar(255) NOT NULL,
  `tokenable_id` bigint unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `token` varchar(64) NOT NULL,
  `abilities` text DEFAULT NULL,
  `last_used_at` timestamp NULL DEFAULT NULL,
  `expires_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `personal_access_tokens_token_unique` (`token`),
  KEY `personal_access_tokens_tokenable_type_tokenable_id_index` (`tokenable_type`,`tokenable_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Record the migration as applied so a future `php artisan migrate` skips it
-- instead of failing on "table already exists". The derived table is required:
-- MySQL will not read from `migrations` in a subquery of its own INSERT.
INSERT INTO `migrations` (`migration`, `batch`)
SELECT '2019_12_14_000001_create_personal_access_tokens_table',
       COALESCE(MAX(`batch`), 0) + 1
FROM (SELECT `batch` FROM `migrations`) AS existing
WHERE NOT EXISTS (
  SELECT 1 FROM (SELECT `migration` FROM `migrations`) AS dup
  WHERE dup.`migration` = '2019_12_14_000001_create_personal_access_tokens_table'
);
