diff --git a/config/conf.yml b/config/conf.yml index b311ef1..fc22322 100644 --- a/config/conf.yml +++ b/config/conf.yml @@ -12,6 +12,7 @@ testing: # actual database should be running MySQL production: + charset: utf8mb4 database: mysql name: social user: lbry diff --git a/database/default_after.sql b/database/default_after.sql new file mode 100644 index 0000000..1ee19f4 --- /dev/null +++ b/database/default_after.sql @@ -0,0 +1,48 @@ +USE `social`; +ALTER DATABASE `social` + DEFAULT CHARACTER SET utf8mb4 + DEFAULT COLLATE utf8mb4_unicode_ci; + +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, + CONSTRAINT `channel_pk` PRIMARY KEY (`claimid`) + ) +CHARACTER SET utf8mb4 +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) + CHARACTER SET utf8mb4 + COLLATE utf8mb4_unicode_ci + NOT NULL, + `parentid` VARCHAR(64) DEFAULT NULL, + `signature` VARCHAR(128) DEFAULT NULL, + `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) + ) +CHARACTER SET utf8mb4 +COLLATE utf8mb4_unicode_ci; + +CREATE INDEX `claim_comment_index` ON `COMMENT` (`lbryclaimid`, `commentid`); +CREATE INDEX `channel_comment_index` ON `COMMENT` (`channelid`, `commentid`); + + + diff --git a/docker-compose.yml b/docker-compose.yml index 41530f7..e5f9099 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: mysql: image: mysql/mysql-server:5.7.27 restart: "no" + command: --character_set_server=utf8mb4 --max_allowed_packet=1073741824 ports: - "3306:3306" environment: diff --git a/src/database/models.py b/src/database/models.py index 9c40586..9e72d33 100644 --- a/src/database/models.py +++ b/src/database/models.py @@ -13,24 +13,28 @@ 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 = CharField(column_name='claimid', primary_key=True, max_length=40) + name = CharField(column_name='name', max_length=256) class Meta: table_name = 'CHANNEL' class Comment(Model): - comment = CharField(column_name='Body', max_length=2000) + comment = CharField( + column_name='body', + max_length=5000, + + ) channel = ForeignKeyField( backref='comments', - column_name='ChannelId', + column_name='channelid', field='claim_id', model=Channel, null=True ) - comment_id = CharField(column_name='CommentId', primary_key=True, max_length=64) - is_hidden = BooleanField(column_name='IsHidden', constraints=[SQL("DEFAULT 0")]) + comment_id = CharField(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') parent = ForeignKeyField( column_name='ParentId', diff --git a/src/misc.py b/src/misc.py index e9d5be8..7c3d4bf 100644 --- a/src/misc.py +++ b/src/misc.py @@ -16,7 +16,7 @@ async def get_claim_from_id(app, claim_id, **kwargs): def clean_input_params(kwargs: dict): for k, v in kwargs.items(): - if type(v) is str and k is not 'comment': + if type(v) is str and k != 'comment': kwargs[k] = v.strip() if k in ID_LIST: kwargs[k] = v.lower() diff --git a/src/server/app.py b/src/server/app.py index 389083e..37e89da 100644 --- a/src/server/app.py +++ b/src/server/app.py @@ -28,6 +28,7 @@ def setup_database(app): host=config[mode]['host'], password=config[mode]['password'], port=config[mode]['port'], + charset=config[mode]['charset'], ) elif config[mode]['database'] == 'sqlite': app['db'] = SqliteDatabase(