Fix: Restart migrations with named constraint this time..
This commit is contained in:
parent
44859b1ecd
commit
eea0151ff0
@ -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
|
||||
);
|
||||
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);
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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`;
|
||||
@ -1,5 +0,0 @@
|
||||
ALTER TABLE `Notes`
|
||||
DROP COLUMN `last_viewed`;
|
||||
|
||||
ALTER TABLE `Notes`
|
||||
ADD COLUMN `updated_at` datetime;
|
||||
@ -1,2 +0,0 @@
|
||||
ALTER TABLE `Chapters`
|
||||
ADD COLUMN `title` varchar(50);
|
||||
@ -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;
|
||||
@ -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;
|
||||
Loading…
x
Reference in New Issue
Block a user