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