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`;
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`);

View file

@ -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'