diff --git a/database/default_after.sql b/database/default_after.sql index 1ee19f4..e6a606b 100644 --- a/database/default_after.sql +++ b/database/default_after.sql @@ -6,8 +6,7 @@ ALTER DATABASE `social` DROP TABLE IF EXISTS `CHANNEL`; CREATE TABLE `CHANNEL` ( `claimid` VARCHAR(40) NOT NULL, - -- i cant tell if max name length is 255 or 256 - `name` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + `name` CHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, CONSTRAINT `channel_pk` PRIMARY KEY (`claimid`) ) CHARACTER SET utf8mb4 @@ -15,34 +14,42 @@ COLLATE utf8mb4_unicode_ci; DROP TABLE IF EXISTS `COMMENT`; CREATE TABLE `COMMENT` ( - `commentid` VARCHAR(64) NOT NULL, - `lbryclaimid` VARCHAR(40) NOT NULL, - `channelid` VARCHAR(40) DEFAULT NULL, - `body` VARCHAR(5000) + -- should be changed to CHAR(64) + `commentid` CHAR(64) NOT NULL, + -- should be changed to CHAR(40) + `lbryclaimid` CHAR(40) NOT NULL, + -- can be null, so idk if this should be char(40) + `channelid` CHAR(40) DEFAULT NULL, + `body` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, - `parentid` VARCHAR(64) DEFAULT NULL, - `signature` VARCHAR(128) DEFAULT NULL, + `parentid` CHAR(64) DEFAULT NULL, + `signature` CHAR(128) DEFAULT NULL, + -- 22 chars long is prolly enough `signingts` VARCHAR(22) DEFAULT NULL, `timestamp` INTEGER NOT NULL, -- there's no way that the timestamp will ever reach 22 characters `ishidden` BOOLEAN DEFAULT FALSE, - CONSTRAINT `COMMENT_PRIMARY_KEY` PRIMARY KEY (`commentid`), - CONSTRAINT `comment_signature_sk` UNIQUE (`signature`), - CONSTRAINT `comment_channel_fk` FOREIGN KEY (`channelid`) REFERENCES `CHANNEL` (`claimid`) - ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `comment_parent_fk` FOREIGN KEY (`parentid`) REFERENCES `COMMENT` (`commentid`) - ON UPDATE CASCADE ON DELETE CASCADE, -- setting null implies comment is top level - CONSTRAINT `channel_signature` - CHECK ( `signature` IS NOT NULL AND `signingts` IS NOT NULL) + CONSTRAINT `COMMENT_PRIMARY_KEY` PRIMARY KEY (`commentid`) + -- setting null implies comment is top level ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +ALTER TABLE COMMENT + ADD CONSTRAINT `comment_channel_fk` FOREIGN KEY (`channelid`) REFERENCES `CHANNEL` (`claimid`) + ON DELETE CASCADE ON UPDATE CASCADE, + ADD CONSTRAINT `comment_parent_fk` FOREIGN KEY (`parentid`) REFERENCES `COMMENT` (`commentid`) + ON UPDATE CASCADE ON DELETE CASCADE +; + CREATE INDEX `claim_comment_index` ON `COMMENT` (`lbryclaimid`, `commentid`); CREATE INDEX `channel_comment_index` ON `COMMENT` (`channelid`, `commentid`); +ALTER TABLE COMMENT ADD CONSTRAINT UNIQUE (`signature`, `channelid`); + diff --git a/src/database/models.py b/src/database/models.py index 9e72d33..b35f122 100644 --- a/src/database/models.py +++ b/src/database/models.py @@ -13,19 +13,15 @@ from src.misc import clean class Channel(Model): - claim_id = CharField(column_name='claimid', primary_key=True, max_length=40) - name = CharField(column_name='name', max_length=256) + claim_id = FixedCharField(column_name='claimid', primary_key=True, max_length=40) + name = CharField(column_name='name', max_length=255) class Meta: table_name = 'CHANNEL' class Comment(Model): - comment = CharField( - column_name='body', - max_length=5000, - - ) + comment = TextField(column_name='body') channel = ForeignKeyField( backref='comments', column_name='channelid', @@ -33,9 +29,9 @@ class Comment(Model): model=Channel, null=True ) - comment_id = CharField(column_name='commentid', primary_key=True, max_length=64) + comment_id = FixedCharField(column_name='commentid', primary_key=True, max_length=64) is_hidden = BooleanField(column_name='ishidden', constraints=[SQL("DEFAULT 0")]) - claim_id = CharField(max_length=40, column_name='LbryClaimId') + claim_id = FixedCharField(max_length=40, column_name='lbryclaimid') parent = ForeignKeyField( column_name='ParentId', field='comment_id', @@ -43,9 +39,9 @@ class Comment(Model): null=True, backref='replies' ) - signature = CharField(max_length=128, column_name='Signature', null=True, unique=True) - signing_ts = TextField(column_name='SigningTs', null=True) - timestamp = IntegerField(column_name='Timestamp') + signature = FixedCharField(max_length=128, column_name='signature', null=True, unique=True) + signing_ts = TextField(column_name='signingts', null=True) + timestamp = IntegerField(column_name='timestamp') class Meta: table_name = 'COMMENT'