From 13ce3d078436f56646640cf99da47a737b21f00c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 21 Nov 2024 03:21:25 +0100 Subject: [PATCH] Increased `posts.content` size for MySQL. `TEXT` -> 64kB `MEDIUMTEXT` -> 16MB Closes: https://github.com/writefreely/writefreely/issues/987 --- db/create.go | 9 +++++++++ migrations/v16.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ schema.sql | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 migrations/v16.go diff --git a/db/create.go b/db/create.go index 1e9e679..164eab8 100644 --- a/db/create.go +++ b/db/create.go @@ -57,6 +57,7 @@ const ( ColumnTypeChar ColumnType = iota ColumnTypeVarChar ColumnType = iota ColumnTypeText ColumnType = iota + ColumnTypeLongText ColumnType = iota ColumnTypeDateTime ColumnType = iota ) @@ -125,6 +126,14 @@ func (d ColumnType) Format(dialect DialectType, size OptionalInt) (string, error return "DATETIME", nil case ColumnTypeText: return "TEXT", nil + case ColumnTypeLongText: + { + // MySQL TEXT is limited to 64KB, so use MEDIUMTEXT for larger sizes (up to 16MB) + if dialect == DialectMySQL { + return "MEDIUMTEXT", nil + } + return "TEXT", nil + } } return "", fmt.Errorf("unsupported column type %d for dialect %d and size %v", d, dialect, size) } diff --git a/migrations/v16.go b/migrations/v16.go new file mode 100644 index 0000000..bf0a730 --- /dev/null +++ b/migrations/v16.go @@ -0,0 +1,50 @@ +/* + * Copyright © 2019-2024 Musing Studio LLC. + * + * This file is part of WriteFreely. + * + * WriteFreely is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + +package migrations + +import ( + "context" + "database/sql" + + wf_db "github.com/writefreely/writefreely/db" +) + +func increasePostContentSize(db *datastore) error { + if db.driverName != driverMySQL { + // Only MySQL databases need this migration + return nil + } + + dialect := wf_db.DialectMySQL + return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error { + builders := []wf_db.SQLBuilder{ + dialect.AlterTable("posts"). + ChangeColumn("content", + dialect.Column( + "column", + wf_db.ColumnTypeLongText, + wf_db.OptionalInt{ + Set: false, + Value: 0, + }).SetNullable(false)), + } + for _, builder := range builders { + query, err := builder.ToSQL() + if err != nil { + return err + } + if _, err := tx.ExecContext(ctx, query); err != nil { + return err + } + } + return nil + }) +} diff --git a/schema.sql b/schema.sql index b3fae97..c043532 100644 --- a/schema.sql +++ b/schema.sql @@ -136,7 +136,7 @@ CREATE TABLE IF NOT EXISTS `posts` ( `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `view_count` int(6) NOT NULL, `title` varchar(160) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, - `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, + `content` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_slug` (`collection_id`,`slug`), UNIQUE KEY `owner_id` (`owner_id`,`id`),