Fix database constraints

This commit is contained in:
grandeljay 2022-06-16 08:54:49 +02:00
parent adba2c5d0a
commit 962bbcd89c
2 changed files with 85 additions and 75 deletions

View file

@ -124,82 +124,88 @@ switch ($step) {
* Users
*/
$database->query('DROP TABLE IF EXISTS `users`;');
$database->query('CREATE TABLE `users` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(64) NOT NULL UNIQUE,
`password` VARCHAR(128) NOT NULL,
`password_reset_token` VARCHAR(128) NULL DEFAULT NULL,
`password_reset_valid_until` DATETIME NOT NULL DEFAULT NOW(),
`last_login` DATETIME NOT NULL DEFAULT NOW(),
`power` INT NOT NULL DEFAULT 0,
`birthdate` DATE NULL DEFAULT NULL,
`locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '",
`name_first` VARCHAR(32) NULL DEFAULT NULL,
`name_last` VARCHAR(32) NULL DEFAULT NULL,
`name_nick` VARCHAR(32) NULL DEFAULT NULL,
`channel` VARCHAR(24) NULL DEFAULT NULL
);');
$database->query('CREATE INDEX `idx_password` ON `users` (`password`);');
$database->query(
'CREATE TABLE `users` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(64) NOT NULL UNIQUE,
`password` VARCHAR(128) NOT NULL,
`password_reset_token` VARCHAR(128) NULL DEFAULT NULL,
`password_reset_valid_until` DATETIME NOT NULL DEFAULT NOW(),
`last_login` DATETIME NOT NULL DEFAULT NOW(),
`power` INT NOT NULL DEFAULT 0,
`birthdate` DATE NULL DEFAULT NULL,
`locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '",
`name_first` VARCHAR(32) NULL DEFAULT NULL,
`name_last` VARCHAR(32) NULL DEFAULT NULL,
`name_nick` VARCHAR(32) NULL DEFAULT NULL,
`channel` VARCHAR(24) NULL DEFAULT NULL,
INDEX `idx_password` (`password`)
);'
);
/**
* Wishlists
*/
$database->query('DROP TABLE IF EXISTS `wishlists`;');
$database->query('CREATE TABLE `wishlists` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`name` VARCHAR(128) NOT NULL,
`hash` VARCHAR(128) NOT NULL,
FOREIGN KEY (`user`)
REFERENCES `users` (`id`)
ON DELETE CASCADE
);');
$database->query('CREATE INDEX `idx_hash` ON `wishlists` (`hash`);');
$database->query(
'CREATE TABLE `wishlists` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`name` VARCHAR(128) NOT NULL,
`hash` VARCHAR(128) NOT NULL,
INDEX `idx_hash` (`hash`),
CONSTRAINT `FK_wishlists_user` FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON DELETE CASCADE
);'
);
/**
* Wishlists Saved
*/
$database->query('DROP TABLE IF EXISTS `wishlists_saved`;');
$database->query('CREATE TABLE `wishlists_saved` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`wishlist` INT NOT NULL,
FOREIGN KEY (`user`)
REFERENCES `users` (`id`)
ON DELETE CASCADE
);');
$database->query('CREATE INDEX `idx_wishlist` ON `wishlists_saved` (`wishlist`);');
$database->query(
'CREATE TABLE `wishlists_saved` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`wishlist` INT NOT NULL,
INDEX `idx_wishlist` (`wishlist`),
CONSTRAINT `FK_wishlists_saved_user` FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON DELETE CASCADE
);'
);
/**
* Wishes
*/
$database->query('DROP TABLE IF EXISTS `wishes`;');
$database->query('CREATE TABLE `wishes` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`wishlist` INT NOT NULL,
`title` VARCHAR(128) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
`image` TEXT NULL DEFAULT NULL,
`url` VARCHAR(255) NULL DEFAULT NULL,
`priority` TINYINT(1) NULL DEFAULT NULL,
`status` VARCHAR(32) NULL DEFAULT NULL,
`is_purchasable` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`wishlist`)
REFERENCES `wishlists` (`id`)
ON DELETE CASCADE
);');
$database->query('CREATE INDEX `idx_url` ON `wishes` (`url`);');
$database->query(
'CREATE TABLE `wishes` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`wishlist` INT NOT NULL,
`title` VARCHAR(128) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
`image` TEXT NULL DEFAULT NULL,
`url` VARCHAR(255) NULL DEFAULT NULL,
`priority` TINYINT(1) NULL DEFAULT NULL,
`status` VARCHAR(32) NULL DEFAULT NULL,
`is_purchasable` BOOLEAN NOT NULL DEFAULT FALSE,
`edited` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX `idx_url` (`url`),
CONSTRAINT `FK_wishes_wishlists` FOREIGN KEY (`wishlist`) REFERENCES `wishlists` (`id`) ON DELETE CASCADE
);'
);
/**
* Products
*/
$database->query(
'CREATE TABLE `products` (
`wish` INT NOT NULL PRIMARY KEY,
`price` FLOAT NULL DEFAULT NULL,
FOREIGN KEY (`wish`)
REFERENCES `wishes` (`id`)
ON DELETE CASCADE
`wish` INT NOT NULL PRIMARY KEY,
`price` FLOAT NULL DEFAULT NULL,
CONSTRAINT `FK_products_wishes` FOREIGN KEY (`wish`) REFERENCES `wishes` (`id`) ON DELETE CASCADE
);'
);
@ -207,31 +213,35 @@ switch ($step) {
* Options
*/
$database->query('DROP TABLE IF EXISTS `options`;');
$database->query('CREATE TABLE `options` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`key` VARCHAR(64) NOT NULL UNIQUE,
`value` VARCHAR(128) NOT NULL
);');
$database->query(
'CREATE TABLE `options` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`key` VARCHAR(64) NOT NULL UNIQUE,
`value` VARCHAR(128) NOT NULL
);'
);
$database->query('INSERT INTO `options`
(`key`, `value`) VALUES
$database->query(
'INSERT INTO `options`
(`key`, `value`)
VALUES
("isInstalled", true),
("version", "' . VERSION . '")
;');
("version", "' . VERSION . '");'
);
/**
* Sessions
*/
$database->query('DROP TABLE IF EXISTS `sessions`;');
$database->query('CREATE TABLE `sessions` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`session` VARCHAR(32) NOT NULL,
FOREIGN KEY (`user`)
REFERENCES `users` (`id`)
ON DELETE CASCADE
);');
$database->query('CREATE INDEX `idx_user` ON `sessions` (`user`);');
$database->query(
'CREATE TABLE `sessions` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`user` INT NOT NULL,
`session` VARCHAR(32) NOT NULL,
INDEX `idx_user` (`session`),
CONSTRAINT `FK_sessions_users` FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON DELETE CASCADE
);'
);
$database->query('SET foreign_key_checks = 1;');
?>

View file

@ -2,7 +2,7 @@
* Wishes
*/
ALTER TABLE `wishes` MODIFY `image` TEXT NULL DEFAULT NULL,
ADD COLUMN `edited` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ADD COLUMN `edited` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
/**
* Products