VARCHAR -> CHAR for all ID fields, constraints moved out of table definition

This commit is contained in:
Oleg Silkin 2020-04-07 15:04:50 -04:00
parent c7e8d274f7
commit 7b7e6c66ac
2 changed files with 31 additions and 28 deletions

View file

@ -6,8 +6,7 @@ ALTER DATABASE `social`
DROP TABLE IF EXISTS `CHANNEL`; DROP TABLE IF EXISTS `CHANNEL`;
CREATE TABLE `CHANNEL` ( CREATE TABLE `CHANNEL` (
`claimid` VARCHAR(40) NOT NULL, `claimid` VARCHAR(40) NOT NULL,
-- i cant tell if max name length is 255 or 256 `name` CHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`name` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
CONSTRAINT `channel_pk` PRIMARY KEY (`claimid`) CONSTRAINT `channel_pk` PRIMARY KEY (`claimid`)
) )
CHARACTER SET utf8mb4 CHARACTER SET utf8mb4
@ -15,34 +14,42 @@ COLLATE utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `COMMENT`; DROP TABLE IF EXISTS `COMMENT`;
CREATE TABLE `COMMENT` ( CREATE TABLE `COMMENT` (
`commentid` VARCHAR(64) NOT NULL, -- should be changed to CHAR(64)
`lbryclaimid` VARCHAR(40) NOT NULL, `commentid` CHAR(64) NOT NULL,
`channelid` VARCHAR(40) DEFAULT NULL, -- should be changed to CHAR(40)
`body` VARCHAR(5000) `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 CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci COLLATE utf8mb4_unicode_ci
NOT NULL, NOT NULL,
`parentid` VARCHAR(64) DEFAULT NULL, `parentid` CHAR(64) DEFAULT NULL,
`signature` VARCHAR(128) DEFAULT NULL, `signature` CHAR(128) DEFAULT NULL,
-- 22 chars long is prolly enough
`signingts` VARCHAR(22) DEFAULT NULL, `signingts` VARCHAR(22) DEFAULT NULL,
`timestamp` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL,
-- there's no way that the timestamp will ever reach 22 characters -- there's no way that the timestamp will ever reach 22 characters
`ishidden` BOOLEAN DEFAULT FALSE, `ishidden` BOOLEAN DEFAULT FALSE,
CONSTRAINT `COMMENT_PRIMARY_KEY` PRIMARY KEY (`commentid`), CONSTRAINT `COMMENT_PRIMARY_KEY` PRIMARY KEY (`commentid`)
CONSTRAINT `comment_signature_sk` UNIQUE (`signature`), -- setting null implies comment is top level
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)
) )
CHARACTER SET utf8mb4 CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci; 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 `claim_comment_index` ON `COMMENT` (`lbryclaimid`, `commentid`);
CREATE INDEX `channel_comment_index` ON `COMMENT` (`channelid`, `commentid`); CREATE INDEX `channel_comment_index` ON `COMMENT` (`channelid`, `commentid`);
ALTER TABLE COMMENT ADD CONSTRAINT UNIQUE (`signature`, `channelid`);

View file

@ -13,19 +13,15 @@ from src.misc import clean
class Channel(Model): class Channel(Model):
claim_id = CharField(column_name='claimid', primary_key=True, max_length=40) claim_id = FixedCharField(column_name='claimid', primary_key=True, max_length=40)
name = CharField(column_name='name', max_length=256) name = CharField(column_name='name', max_length=255)
class Meta: class Meta:
table_name = 'CHANNEL' table_name = 'CHANNEL'
class Comment(Model): class Comment(Model):
comment = CharField( comment = TextField(column_name='body')
column_name='body',
max_length=5000,
)
channel = ForeignKeyField( channel = ForeignKeyField(
backref='comments', backref='comments',
column_name='channelid', column_name='channelid',
@ -33,9 +29,9 @@ class Comment(Model):
model=Channel, model=Channel,
null=True 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")]) 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( parent = ForeignKeyField(
column_name='ParentId', column_name='ParentId',
field='comment_id', field='comment_id',
@ -43,9 +39,9 @@ class Comment(Model):
null=True, null=True,
backref='replies' backref='replies'
) )
signature = CharField(max_length=128, column_name='Signature', null=True, unique=True) signature = FixedCharField(max_length=128, column_name='signature', null=True, unique=True)
signing_ts = TextField(column_name='SigningTs', null=True) signing_ts = TextField(column_name='signingts', null=True)
timestamp = IntegerField(column_name='Timestamp') timestamp = IntegerField(column_name='timestamp')
class Meta: class Meta:
table_name = 'COMMENT' table_name = 'COMMENT'