diff --git a/api/resources/db/migration/V1__Create_user_table.sql b/api/resources/db/migration/V1__Create_user_table.sql index 041f5c7..14a6c98 100644 --- a/api/resources/db/migration/V1__Create_user_table.sql +++ b/api/resources/db/migration/V1__Create_user_table.sql @@ -1,9 +1,46 @@ -CREATE TABLE `Users` +create table Users ( - `id` int PRIMARY KEY AUTO_INCREMENT, - `username` varchar(50) UNIQUE NOT NULL, - `email` varchar(255) UNIQUE NOT NULL, - `password` varchar(255) NOT NULL, - `created_at` datetime NOT NULL, - `last_login` datetime -); \ No newline at end of file + id int auto_increment primary key, + username varchar(50) not null, + email varchar(255) not null, + password varchar(255) not null, + created_at datetime not null, + last_login datetime null, + + constraint email unique (email), + constraint username unique (username) +); + +create table Notes +( + uuid binary(16) not null primary key, + title varchar(50) not null, + user_id int not null, + updated_at datetime null, + + constraint Notes_fk_user foreign key (user_id) references Users (id) on delete cascade +); + +create table Chapters +( + id int auto_increment primary key, + number int not null, + title varchar(50) not null, + content text not null, + note_uuid binary(16) not null, + constraint Chapters_fk_note foreign key (note_uuid) references Notes (uuid) on delete cascade +); + +create index note_uuid on Chapters (note_uuid); + +create index user_id on Notes (user_id); + +create table Tags +( + id int auto_increment primary key, + name varchar(50) not null, + note_uuid binary(16) not null, + constraint Tags_fk_note foreign key (note_uuid) references Notes (uuid) on delete cascade +); + +create index note_uuid on Tags (note_uuid); diff --git a/api/resources/db/migration/V2__Create_note_table.sql b/api/resources/db/migration/V2__Create_note_table.sql deleted file mode 100644 index 4904664..0000000 --- a/api/resources/db/migration/V2__Create_note_table.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE `Notes` -( - `id` int PRIMARY KEY AUTO_INCREMENT, - `title` varchar(50) NOT NULL, - `content` text NOT NULL, - `user_id` int NOT NULL, - `last_viewed` datetime -); - -ALTER TABLE `Notes` - ADD FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT; \ No newline at end of file diff --git a/api/resources/db/migration/V3__Create_tag_table.sql b/api/resources/db/migration/V3__Create_tag_table.sql deleted file mode 100644 index 71f9b44..0000000 --- a/api/resources/db/migration/V3__Create_tag_table.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE `Tags` -( - `id` int PRIMARY KEY AUTO_INCREMENT, - `name` varchar(50) NOT NULL, - `note_id` int NOT NULL -); - -ALTER TABLE `Tags` - ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT; \ No newline at end of file diff --git a/api/resources/db/migration/V4__Create_chapters_table.sql b/api/resources/db/migration/V4__Create_chapters_table.sql deleted file mode 100644 index 2867403..0000000 --- a/api/resources/db/migration/V4__Create_chapters_table.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE `Chapters` -( - `id` int PRIMARY KEY AUTO_INCREMENT, - `number` int NOT NULL, - `content` text NOT NULL, - `note_id` int NOT NULL -); - -ALTER TABLE `Chapters` - ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT; - -ALTER TABLE `Notes` - DROP COLUMN `content`; \ No newline at end of file diff --git a/api/resources/db/migration/V5__Update_note_table.sql b/api/resources/db/migration/V5__Update_note_table.sql deleted file mode 100644 index 980637b..0000000 --- a/api/resources/db/migration/V5__Update_note_table.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE `Notes` - DROP COLUMN `last_viewed`; - -ALTER TABLE `Notes` - ADD COLUMN `updated_at` datetime; diff --git a/api/resources/db/migration/V6__Update_chapter_table.sql b/api/resources/db/migration/V6__Update_chapter_table.sql deleted file mode 100644 index 1478385..0000000 --- a/api/resources/db/migration/V6__Update_chapter_table.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE `Chapters` - ADD COLUMN `title` varchar(50); diff --git a/api/resources/db/migration/V7__Update_constraints_cascade_delete.sql b/api/resources/db/migration/V7__Update_constraints_cascade_delete.sql deleted file mode 100644 index ed821bf..0000000 --- a/api/resources/db/migration/V7__Update_constraints_cascade_delete.sql +++ /dev/null @@ -1,19 +0,0 @@ --- ON DELETE -> CASCADE - -ALTER TABLE `Notes` - DROP CONSTRAINT IF EXISTS `Notes_ibfk_1`; - -ALTER TABLE `Notes` - ADD FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; - -ALTER TABLE `Chapters` - DROP CONSTRAINT IF EXISTS `Chapters_ibfk_1`; - -ALTER TABLE `Chapters` - ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; - -ALTER TABLE `Tags` - DROP CONSTRAINT IF EXISTS `Tags_ibfk_1`; - -ALTER TABLE `Tags` - ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; diff --git a/api/resources/db/migration/V8__Notes_uuid.sql b/api/resources/db/migration/V8__Notes_uuid.sql deleted file mode 100644 index 301e6f4..0000000 --- a/api/resources/db/migration/V8__Notes_uuid.sql +++ /dev/null @@ -1,37 +0,0 @@ --- no need to migrate existing data yet -drop table if exists Chapters; -drop table if exists Tags; -drop table if exists Notes; - -CREATE TABLE `Notes` -( - `uuid` binary(16) PRIMARY KEY, - `title` varchar(50) NOT NULL, - `user_id` int NOT NULL, - `updated_at` datetime -); - -ALTER TABLE `Notes` - ADD FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT; - -CREATE TABLE `Tags` -( - `id` int PRIMARY KEY AUTO_INCREMENT, - `name` varchar(50) NOT NULL, - `note_uuid` binary(16) NOT NULL -); - -ALTER TABLE `Tags` - ADD FOREIGN KEY (`note_uuid`) REFERENCES `Notes` (`uuid`) ON DELETE CASCADE ON UPDATE RESTRICT; - -CREATE TABLE `Chapters` -( - `id` int PRIMARY KEY AUTO_INCREMENT, - `number` int NOT NULL, - `title` varchar(50) NOT NULL, - `content` text NOT NULL, - `note_uuid` binary(16) NOT NULL -); - -ALTER TABLE `Chapters` - ADD FOREIGN KEY (`note_uuid`) REFERENCES `Notes` (`uuid`) ON DELETE CASCADE ON UPDATE RESTRICT;