SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
SET GLOBAL time_zone = 'Europe/Madrid';

-- ----------------------------
--  Table structure for `entity`
-- ----------------------------
DROP TABLE IF EXISTS `entity`;
CREATE TABLE `entity`
(
    `id`         int(11)      NOT NULL AUTO_INCREMENT,
    `type`       int(11)      NOT NULL,
    `status`     int(11)      NOT NULL,
    `token`      varchar(255) NOT NULL,
    `created_at` datetime     NOT NULL,
    `updated_at` datetime     NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `token` (`token`) USING BTREE,
    KEY `type` (`type`),
    CONSTRAINT `entity_ibfk_1` FOREIGN KEY (`type`) REFERENCES `entity_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  AUTO_INCREMENT = 4
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `entity`
-- ----------------------------
BEGIN;
INSERT INTO `entity` VALUES ('1', '1', '1', '7488e331b8b64e5794da3fa4eb10ad5d', '2020-03-05 13:00:00', '2020-03-05 13:00:00'), ('2', '2', '1', '44b85c98e94039c8a0a015f6d3a3449e', '2020-03-05 13:00:01', '2020-03-05 13:00:01'), ('3', '1', '1', '2eb2d5785df4cdcb7847e1a4b3372b5a', '2020-03-05 13:10:00', '2020-03-05 13:10:00');
COMMIT;

-- ----------------------------
--  Table structure for `entity_status`
-- ----------------------------
DROP TABLE IF EXISTS `entity_status`;
CREATE TABLE `entity_status`
(
    `id`     int(11) NOT NULL AUTO_INCREMENT,
    `status` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 4
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `entity_status`
-- ----------------------------
BEGIN;
INSERT INTO `entity_status` VALUES ('1', 'active'), ('2', 'hidden'), ('3', 'deleted');
COMMIT;

-- ----------------------------
--  Table structure for `entity_type`
-- ----------------------------
DROP TABLE IF EXISTS `entity_type`;
CREATE TABLE `entity_type`
(
    `id`   int(11) NOT NULL AUTO_INCREMENT,
    `type` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 4
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `entity_type`
-- ----------------------------
BEGIN;
INSERT INTO `entity_type` VALUES ('1', 'user'), ('2', 'oath'), ('3', 'upload');
COMMIT;

-- ----------------------------
--  Table structure for `oauth`
-- ----------------------------
DROP TABLE IF EXISTS `oauth`;
CREATE TABLE `oauth`
(
    `id`       int(11) NOT NULL,
    `type_id`  int(11)      DEFAULT NULL,
    `password` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `type_id` (`type_id`),
    CONSTRAINT `oauth_ibfk_2` FOREIGN KEY (`type_id`) REFERENCES `oauth_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `oauth_ibfk_1` FOREIGN KEY (`id`) REFERENCES `entity` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `oauth`
-- ----------------------------
BEGIN;
INSERT INTO `oauth` VALUES ('2', '1', '827ccb0eea8a706c4c34a16891f84e7b');
COMMIT;

-- ----------------------------
--  Table structure for `oauth_type`
-- ----------------------------
DROP TABLE IF EXISTS `oauth_type`;
CREATE TABLE `oauth_type`
(
    `id`   int(11) NOT NULL AUTO_INCREMENT,
    `type` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 3
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `oauth_type`
-- ----------------------------
BEGIN;
INSERT INTO `oauth_type` VALUES ('1', 'user'), ('2', 'admin');
COMMIT;

-- ----------------------------
--  Table structure for `upload`
-- ----------------------------
DROP TABLE IF EXISTS `upload`;
CREATE TABLE `upload`
(
    `id`       int(11) NOT NULL,
    `type_id`  int(11)      DEFAULT NULL,
    `filename` varchar(255) DEFAULT NULL,
    `path`     varchar(255) DEFAULT NULL,
    `url`      varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `type_id` (`type_id`),
    CONSTRAINT `upload_ibfk_1` FOREIGN KEY (`id`) REFERENCES `entity` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `upload_ibfk_2` FOREIGN KEY (`type_id`) REFERENCES `upload_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Table structure for `upload_type`
-- ----------------------------
DROP TABLE IF EXISTS `upload_type`;
CREATE TABLE `upload_type`
(
    `id`   int(11) NOT NULL AUTO_INCREMENT,
    `type` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 6
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `upload_type`
-- ----------------------------
BEGIN;
INSERT INTO `upload_type` VALUES ('1', 'jpg'), ('2', 'png'), ('3', 'pdf'), ('4', 'txt'), ('5', 'zip');
COMMIT;

-- ----------------------------
--  Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
    `id`       int(11) NOT NULL,
    `name`     varchar(255) DEFAULT NULL,
    `lastname` varchar(255) DEFAULT NULL,
    `email`    varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `user_ibfk_1` FOREIGN KEY (`id`) REFERENCES `entity` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `user`
-- ----------------------------
BEGIN;
INSERT INTO `user` VALUES ('1', 'admin', 'dummy', 'admin@dummy.com'), ('3', 'user', 'test', 'user@test.com');
COMMIT;

-- ----------------------------
--  Table structure for `user_oauth`
-- ----------------------------
DROP TABLE IF EXISTS `user_oauth`;
CREATE TABLE `user_oauth`
(
    `id`       int(11) NOT NULL AUTO_INCREMENT,
    `user_id`  int(11) NOT NULL,
    `oauth_id` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `user_oauth_id` (`user_id`, `oauth_id`) USING BTREE,
    KEY `user_id` (`user_id`),
    KEY `oauth_id` (`oauth_id`) USING BTREE,
    CONSTRAINT `user_oauth_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `user_oauth_ibfk_2` FOREIGN KEY (`oauth_id`) REFERENCES `oauth` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  AUTO_INCREMENT = 2
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  Records of `user_oauth`
-- ----------------------------
BEGIN;
INSERT INTO `user_oauth` VALUES ('1', '1', '2');
COMMIT;

-- ----------------------------
--  Table structure for `user_upload`
-- ----------------------------
DROP TABLE IF EXISTS `user_upload`;
CREATE TABLE `user_upload`
(
    `id`        int(11) NOT NULL AUTO_INCREMENT,
    `user_id`   int(11) NOT NULL,
    `upload_id` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `user_upload_id` (`user_id`, `upload_id`) USING BTREE,
    KEY `user_id` (`user_id`),
    KEY `upload_id` (`upload_id`) USING BTREE,
    CONSTRAINT `user_upload_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `user_upload_ibfk_2` FOREIGN KEY (`upload_id`) REFERENCES `upload` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- ----------------------------
--  View structure for `users`
-- ----------------------------
DROP VIEW IF EXISTS `users`;
CREATE ALGORITHM = UNDEFINED DEFINER =`{database}`@`%` SQL SECURITY DEFINER VIEW `users` AS
select `u`.`id` AS `id`, `e`.`token` AS `token`, `u`.`name` AS `name`, `u`.`lastname` AS `lastname`, `u`.`email` AS `email`
from (`user` `u`
         join `entity` `e` on ((`e`.`id` = `u`.`id`)))
where (`e`.`status` = 1)
order by `e`.`created_at` desc;

-- ----------------------------
--  View structure for `users_oauth`
-- ----------------------------
DROP VIEW IF EXISTS `users_oauth`;
CREATE ALGORITHM = UNDEFINED DEFINER =`{database}`@`%` SQL SECURITY DEFINER VIEW `users_oauth` AS
select `u`.`id` AS `id`, `e`.`token` AS `token`, `u`.`email` AS `email`, `o`.`password` AS `password`
from ((((`user_oauth` `uo` join `user` `u` on ((`u`.`id` = `uo`.`user_id`))) join `oauth` `o` on ((`o`.`id` = `uo`.`oauth_id`))) join `entity` `e` on ((`e`.`id` = `u`.`id`)))
         join `entity` `ee` on ((`ee`.`id` = `o`.`id`)))
where (`ee`.`status` = 1)
order by `e`.`created_at` desc;

-- ----------------------------
--  View structure for `users_uploads`
-- ----------------------------
DROP VIEW IF EXISTS `users_uploads`;
CREATE ALGORITHM = UNDEFINED DEFINER =`{database}`@`%` SQL SECURITY DEFINER VIEW `users_uploads` AS
select `e`.`token` AS `token`, `p`.`filename` AS `filename`, `t`.`type` AS `type`, `u`.`name` AS `name`, `u`.`lastname` AS `lastname`, `u`.`email` AS `email`, `p`.`url` AS `url`
from ((((`upload` `p` join `upload_type` `t` on ((`t`.`id` = `p`.`type_id`))) join `user_upload` `up` on ((`up`.`upload_id` = `p`.`id`))) join `user` `u` on ((`u`.`id` = `up`.`user_id`)))
         join `entity` `e` on ((`e`.`id` = `p`.`id`)))
where (`e`.`status` = 1)
order by `e`.`created_at` desc;

SET FOREIGN_KEY_CHECKS = 1;
