CREATE TABLE IF NOT EXISTS `applications` (
`id` bigint(20) unsigned NOT NULL,
`application_key` varchar(127) NOT NULL,
`application_name` varchar(127) NOT NULL,
`application_description` varchar(255),
`client_id` varchar(64) NOT NULL,
`client_secret` varchar(127) NOT NULL,
`redirect_uri` varchar(255) NOT NULL,
`status` int(10) NOT NULL COMMENT 'lt 0 deleted, 0 pendding, 1 valid',
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`),
KEY `idx_applications_client_id` (`client_id`),
UNIQUE KEY `udx_applications_application_key` (`application_key`)
);
CREATE TABLE IF NOT EXISTS `roles` (
`id` bigint(20) unsigned NOT NULL,
`role_name` varchar(127) NOT NULL,
`role_description` varchar(255),
`status` int(10) NOT NULL COMMENT '-1 deleted 0 pendding 1 valid',
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) unsigned NOT NULL,
`ref` bigint(20) unsigned NOT NULL COMMENT 'ref to userID who invited this user',
`out_key` varchar(255) COMMENT 'outer system''s key',
`first_name` varchar(127) NOT NULL,
`last_name` varchar(127) NOT NULL,
`avatar_url` varchar(255),
`photo_url` varchar(255),
`password` varchar(255) NOT NULL,
`remember_token` varchar(255),
`status` int(10) NOT NULL COMMENT '-1 deleted 0 pendding 1 valid',
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `application_role` (
`application_id` bigint(20) unsigned NOT NULL,
`role_id` bigint(20) unsigned NOT NULL,
`right` bigint(20) NOT NULL,
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`application_id`, `role_id`),
CONSTRAINT `fk_application_role_application_id` FOREIGN KEY (`application_id`) REFERENCES `applications` (`id`),
CONSTRAINT `fk_application_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
);
CREATE TABLE IF NOT EXISTS `application_user` (
`application_id` bigint(20) unsigned NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
`right` bigint(20) NOT NULL,
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`application_id`, `user_id`),
CONSTRAINT `fk_application_user_application_id` FOREIGN KEY (`application_id`) REFERENCES `applications` (`id`),
CONSTRAINT `fk_application_user_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
CREATE TABLE IF NOT EXISTS `emails` (
`id` bigint(20) unsigned NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
`email_address` varchar(127) NOT NULL,
`email_verified_at` timestamp NULL,
`status` int(10) NOT NULL COMMENT '-1 deleted 0 pendding 1 valid',
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `udx_emails_email_address` (`email_address`),
CONSTRAINT `fk_emails_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
CREATE TABLE IF NOT EXISTS `phones` (
`id` bigint(20) unsigned NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
`phone_number` varchar(16) NOT NULL,
`phone_verified_at` timestamp NULL,
`status` int(10) NOT NULL COMMENT 'lt 0 deleted, 0 pendding, 1 valid',
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `udx_phones_phone_number` (`phone_number`),
CONSTRAINT `fk_phones_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
CREATE TABLE IF NOT EXISTS `role_user` (
`role_id` bigint(20) unsigned NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`role_id`, `user_id`),
KEY `idx_role_user_role_id_user_id` (`role_id`, `user_id`),
CONSTRAINT `fk_role_user_role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
CONSTRAINT `fk_role_user_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
CREATE TABLE IF NOT EXISTS `tokens` (
`id` bigint(20) unsigned NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
`access_token` varchar(255) NOT NULL,
`refresh_token` varchar(255) NOT NULL,
`scope` varchar(255) NOT NULL,
`code` varchar(32) NOT NULL COMMENT 'use in oauth2 code',
`code_challenge` varchar(64) COMMENT 'oauth2 PKCE',
`token_type` varchar(255) NOT NULL COMMENT 'Bearer',
`expire_at` timestamp NULL,
`client` varchar(255) NOT NULL,
`version` varchar(255) NOT NULL,
`created_at` timestamp NULL,
`updated_at` timestamp NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_tokens_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);