Foreign keys in messages now delete when parent key is deleted

This commit is contained in:
shockrah 2020-11-01 22:33:25 -08:00
parent 41c28cc845
commit 543feef330
4 changed files with 13 additions and 14 deletions

View File

@ -64,15 +64,16 @@ impl FromDB<Channel> for Channel {
} }
async fn delete(p: &Pool, id: UBigInt) -> Response<Channel> { async fn delete(p: &Pool, id: UBigInt) -> Response<Channel> {
if let Ok(conn) = p.get_conn().await { let conn = p.get_conn().await;
let q = "DELETE FROM channels WHERE id = :id"; if conn.is_err() {
let result: Result<Conn, SqlError> = return Response::Other(no_conn!("Member::FromDB::delete"))
conn.drop_exec(q, params!{"id" => id}).await; }
return match result {
Ok(_) => Response::Success, // TODO: do something with the return values where possible
Err(_) => Response::Other(sql_err!("Member::FromDB::delete Listen i dont know either")) return match conn.unwrap().prep_exec("DELETE FROM CHANNELS WHERE id = :id", params!{"id" => id}).await {
} Ok(_) => Response::Success,
Err(_) => Response::Other(sql_err!("Channels::FromDB::delete"))
} }
return Response::Other(no_conn!("Member::FromDB::delete"))
} }
} }

View File

@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS `channels` ( CREATE TABLE IF NOT EXISTS `channels` (
`id` BIGINT UNSIGNED NOT NULL auto_increment, `id` BIGINT UNSIGNED NOT NULL auto_increment,
`name` UNIQUE VARCHAR(255) NOT NULL, `name` UNIQUE VARCHAR(255) NOT NULL,
`description` VARCHAR(1024), `description` VARCHAR(4096),
`kind` INTEGER NOT NULL, `kind` INTEGER NOT NULL,
PRIMARY KEY(`id`), UNIQUE KEY(`name`) PRIMARY KEY(`id`), UNIQUE KEY(`name`)
); );

View File

@ -4,8 +4,6 @@ CREATE TABLE IF NOT EXISTS `messages`(
`time` BIGINT NOT NULL, `time` BIGINT NOT NULL,
`content` VARCHAR(2048) NOT NULL, `content` VARCHAR(2048) NOT NULL,
`author_id` BIGINT UNSIGNED NOT NULL, `author_id` BIGINT UNSIGNED NOT NULL,
`channel_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
FOREIGN KEY (`author_id`) REFERENCES members(`id`), FOREIGN KEY (`author_id`) REFERENCES members(`id`) ON DELETE CASCADE
FOREIGN KEY (`channel_name`) REFERENCES channels(`name`)
); );

View File

@ -3,7 +3,7 @@ pub const INVITE_CREATE: &'static str = "/invite/create"; // requires none
pub const CHANNELS_LIST: &'static str = "/channels/list"; // requires none pub const CHANNELS_LIST: &'static str = "/channels/list"; // requires none
pub const CHANNELS_CREATE: &'static str = "/channels/create"; // requires @name @kind pub const CHANNELS_CREATE: &'static str = "/channels/create"; // requires @name @kind
pub const CHANNELS_DELETE: &'static str = "/channels/delete"; // requires @name pub const CHANNELS_DELETE: &'static str = "/channels/delete"; // requires @channel_id
pub const MESSAGE_SEND: &'static str = "/message/send"; // requires @content pub const MESSAGE_SEND: &'static str = "/message/send"; // requires @content