From 13ce3d078436f56646640cf99da47a737b21f00c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 21 Nov 2024 03:21:25 +0100 Subject: [PATCH 1/4] 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`), From f01d388dd38ed85a86184a95159f0553311c4c44 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 13 Feb 2025 18:43:38 +0100 Subject: [PATCH 2/4] Bumped migration number --- migrations/v17.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 migrations/v17.go diff --git a/migrations/v17.go b/migrations/v17.go new file mode 100644 index 0000000..19607af --- /dev/null +++ b/migrations/v17.go @@ -0,0 +1,51 @@ +/* + * 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 + }) +} + From 7b2d65ad33661c67bce2753ac1f422eeb0b52e88 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 13 Feb 2025 18:48:26 +0100 Subject: [PATCH 3/4] Addressed comment: https://github.com/writefreely/writefreely/pull/1145/commits/13ce3d078436f56646640cf99da47a737b21f00c#r1878577878 --- schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema.sql b/schema.sql index c043532..b3fae97 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` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, + `content` text 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`), From 0c78d4095a4e8f57cfa1f64f2e8c7a32722cfe8e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 25 Feb 2025 20:40:38 +0100 Subject: [PATCH 4/4] Addressed comment: https://github.com/writefreely/writefreely/pull/1145#pullrequestreview-2642070334 --- migrations/migrations.go | 1 + 1 file changed, 1 insertion(+) diff --git a/migrations/migrations.go b/migrations/migrations.go index 6b5b094..ed5a434 100644 --- a/migrations/migrations.go +++ b/migrations/migrations.go @@ -72,6 +72,7 @@ var migrations = []Migration{ New("support password resetting", supportPassReset), // V13 -> V14 New("speed up blog post retrieval", addPostRetrievalIndex), // V14 -> V15 New("support ActivityPub likes", supportRemoteLikes), // V15 -> V16 (v0.16.0) + New("posts.content column size", increasePostContentSize), // V16 -> V17 } // CurrentVer returns the current migration version the application is on