From 501d454af57c94a36a2c362f0228bf481779b972 Mon Sep 17 00:00:00 2001 From: Kyriakos Z <3829551+koox00@users.noreply.github.com> Date: Tue, 22 Feb 2022 12:13:04 +0200 Subject: [PATCH] MM-39351: fixes LastRootPostAt being null (#19574) * MM-39351: fixes LastRootPostAt being null After upgrading to v6.1 there is a chance for LastRootPostAt to be NULL. This might be due to a faulty migration when upgrading to v5.35. v5.35 had `create_msg_root_count` where LastRootPostAt was used as a temporary column which at the end was deleted. Then on v6.1 we are adding that column again, this time to keep. If the migration to v5.35 had failed and it didn't drop the column the `upgrade_cte` (v6.1) migration would leave some channels with `LastRootPostAt` being NULL. This resulted in crashing the app. This commit fixes that by running again a migration to again set LastRootPostAt only when it's NULL this time and then set it to 0 for channels that might still have LastRootPostAt being NULL (channels with no posts). PS: the whole issue arose when upon creating the `upgrade_cte` migration the create_msg_root_count migration was manually edited to change the column name from LastRootPostAt to LastRootAt. Co-authored-by: Mattermod --- .editorconfig | 2 +- db/migrations/bindata.go | 92 +++++++++++++++++++ .../000076_upgrade_lastrootpostat.down.sql | 1 + .../000076_upgrade_lastrootpostat.up.sql | 47 ++++++++++ .../000076_upgrade_lastrootpostat.down.sql | 1 + .../000076_upgrade_lastrootpostat.up.sql | 50 ++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 db/migrations/mysql/000076_upgrade_lastrootpostat.down.sql create mode 100644 db/migrations/mysql/000076_upgrade_lastrootpostat.up.sql create mode 100644 db/migrations/postgres/000076_upgrade_lastrootpostat.down.sql create mode 100644 db/migrations/postgres/000076_upgrade_lastrootpostat.up.sql diff --git a/.editorconfig b/.editorconfig index d3722ef2ba..d0d47befdd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,7 +8,7 @@ insert_final_newline = true charset = utf-8 trim_trailing_whitespace = true -[*.go] +[*.{go,sql}] indent_style = tab [*.{js,jsx,json,html}] diff --git a/db/migrations/bindata.go b/db/migrations/bindata.go index 378493b62d..5ad25f607f 100644 --- a/db/migrations/bindata.go +++ b/db/migrations/bindata.go @@ -150,6 +150,8 @@ // mysql/000074_upgrade_users_v6.3.up.sql // mysql/000075_alter_upload_sessions_index.down.sql // mysql/000075_alter_upload_sessions_index.up.sql +// mysql/000076_upgrade_lastrootpostat.down.sql +// mysql/000076_upgrade_lastrootpostat.up.sql // postgres/000001_create_teams.down.sql // postgres/000001_create_teams.up.sql // postgres/000002_create_team_members.down.sql @@ -300,6 +302,8 @@ // postgres/000074_upgrade_users_v6.3.up.sql // postgres/000075_alter_upload_sessions_index.down.sql // postgres/000075_alter_upload_sessions_index.up.sql +// postgres/000076_upgrade_lastrootpostat.down.sql +// postgres/000076_upgrade_lastrootpostat.up.sql package migrations import ( @@ -3376,6 +3380,46 @@ func mysql000075_alter_upload_sessions_indexUpSql() (*asset, error) { return a, nil } +var _mysql000076_upgrade_lastrootpostatDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x04\xc0\x31\x0a\x02\x31\x10\x05\xd0\xde\x53\xfc\xce\x6a\xc3\xd8\xe8\x71\x42\x48\x3e\xd9\x01\xf9\xa3\x9b\x71\xc5\xdb\xfb\xb6\x0d\x0a\x88\x1c\xc8\x40\xdf\x9b\x26\xd1\xf4\xcb\xdd\x35\xb1\x5c\x9d\xf0\xbc\x2e\xf4\x38\x79\x70\xc0\x05\x33\xb3\x87\xd5\xcf\x6b\x1e\x6d\xb0\xf6\x64\x3d\xef\xe5\x56\x46\x7c\x55\xd6\xfb\x79\xf9\x07\x00\x00\xff\xff\xa9\xaa\xb3\x07\x55\x00\x00\x00") + +func mysql000076_upgrade_lastrootpostatDownSqlBytes() ([]byte, error) { + return bindataRead( + _mysql000076_upgrade_lastrootpostatDownSql, + "mysql/000076_upgrade_lastrootpostat.down.sql", + ) +} + +func mysql000076_upgrade_lastrootpostatDownSql() (*asset, error) { + bytes, err := mysql000076_upgrade_lastrootpostatDownSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "mysql/000076_upgrade_lastrootpostat.down.sql", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +var _mysql000076_upgrade_lastrootpostatUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x9c\x53\xc1\x6e\x9b\x40\x10\x3d\xef\x7e\xc5\xeb\xc9\x50\x25\x96\x7b\x46\x54\x5a\xc3\x90\x50\xc1\x62\xc1\xa2\xa6\x27\x84\x02\xa9\x91\x52\x93\x98\x4d\xeb\xcf\xaf\x96\x05\xd7\x76\xaa\xa6\xed\x0d\x76\x66\xde\xbc\x79\x6f\x26\xc8\x49\x28\xc2\x26\xcf\x02\x0a\xcb\x9c\x90\x76\x5f\xf7\xb5\x6e\xab\xa4\x1e\x74\xde\xf7\x7a\xd3\x0f\x5a\xe8\x2a\x6c\x1f\xea\x97\x47\x0d\xc7\xe5\x6b\xba\x89\x25\x67\x71\x04\x87\x33\xc6\x0a\x4a\x28\x50\x08\xb2\x52\x2a\xe7\xbd\x8b\x28\xcf\x52\xc4\x32\xca\xf2\x54\xa8\x38\x93\x55\x11\xdc\x52\x2a\x96\x41\x96\x94\xa9\x2c\x4c\xc9\xe7\x5b\xca\x09\x4a\xac\x13\xaa\xa4\x48\x09\x3e\x16\xc1\xb6\xde\xed\xda\xc7\x61\x61\x12\x84\x0c\xa7\xb0\x2d\x86\x8f\x50\x28\xb1\x16\x05\x39\xee\x9c\x60\x01\x8f\x00\xe7\x84\x8f\x30\xce\x94\x16\x52\x24\xca\x44\x21\x2e\x20\xcb\x24\x41\x96\xe3\x22\xf2\xce\xc7\xca\x80\xbb\xf0\xf1\x01\xea\x96\x24\x67\x4c\x24\x8a\x72\xcb\x05\x33\x45\xd8\x47\x5b\x8e\xf3\xbe\x28\x48\x61\x46\x5c\x79\x9c\x91\x0c\x11\x47\x1e\x27\x19\x7a\x9c\x05\x22\x49\xde\x96\xd8\xe3\x2c\xcc\xb3\xcd\x89\x2b\x71\x04\xba\x8b\x0b\x55\xbc\x51\xec\x71\xfe\xb7\x8e\x46\xdd\xe1\xb5\x9b\x17\x66\x72\xc6\x46\x3b\xe7\xc9\xf9\xec\xdd\xc5\xd0\x93\xa8\x9c\xb9\xf8\x88\xd5\xa4\xdd\xf5\x35\x1e\xba\x43\x3b\xe0\x9b\x25\x80\x7b\xdd\xa2\xde\x35\x18\x5a\x3d\x40\x6f\xdb\x4b\x94\x87\x7e\x8f\xfb\x59\x63\xbd\xad\x35\x9a\x7e\xb7\xd0\xd8\xd6\xdf\x5b\x74\xda\xd4\x71\xc6\xca\x4d\x28\x14\x19\x7b\x4f\x58\xb1\x58\x4a\xca\xf1\x29\x8b\xa5\xdd\xca\x69\x92\xf1\xf3\x98\xb8\x8c\x9b\xb9\x41\xd7\x5c\x4d\xb1\x4c\x24\x54\x04\xe4\xa4\xe2\xce\x31\x44\x86\x65\xb0\x6f\x6b\xdd\x0a\xed\x5e\x61\xe5\x42\x14\x78\xac\x07\xbd\xef\x7b\xfd\xd4\x0f\x7a\xac\x32\xa2\x9c\x43\xdb\xbf\x84\x22\x65\x49\x8c\x48\x88\xb2\x3c\x20\xc4\x32\xa4\x3b\x38\x5d\x73\xa8\x0c\xc2\x50\x4d\x1c\xaa\xae\xa9\x5e\x9e\x1a\xe3\x4d\xad\x5d\x64\x12\xa7\x44\x7d\x4c\x6c\xec\x5b\xdc\x8c\x2d\x46\xf9\x6d\x33\x1b\x36\xfa\x8d\xd9\x8b\x71\xe5\xd9\x4d\x9e\x95\x1b\xac\xbf\xbc\x9a\x7c\x9c\xe4\xd9\x74\x79\x5e\x1e\x45\x80\x7f\xda\x73\x12\x6e\x52\xed\xc2\x1d\xff\xb5\x0c\x27\x64\x7e\xbf\x10\x1e\xe7\xcc\xec\xc1\xe8\xf8\x45\x8a\xee\xb1\xba\x3a\xf7\xfc\x47\xa7\xb7\xd8\xf5\x18\x55\x3a\x5a\xfd\xeb\xee\xcc\x6d\x9d\xa3\xf8\x2b\xfc\x69\x21\xff\xe9\xfe\xec\x41\xfc\xc7\xed\x45\xdd\xc1\xe3\x3f\x03\x00\x00\xff\xff\x8e\x6b\x3e\x5f\x49\x05\x00\x00") + +func mysql000076_upgrade_lastrootpostatUpSqlBytes() ([]byte, error) { + return bindataRead( + _mysql000076_upgrade_lastrootpostatUpSql, + "mysql/000076_upgrade_lastrootpostat.up.sql", + ) +} + +func mysql000076_upgrade_lastrootpostatUpSql() (*asset, error) { + bytes, err := mysql000076_upgrade_lastrootpostatUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "mysql/000076_upgrade_lastrootpostat.up.sql", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + var _postgres000001_create_teamsDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x90\xc1\x6a\x04\x21\x10\x44\xef\x7e\x45\x1f\x93\x6f\xf0\x34\xd9\x35\x30\x30\x19\xc3\xac\x81\xbd\x89\x68\x93\x34\x38\x2a\xea\x26\xf9\xfc\x30\xe3\x92\x1c\x72\x58\xf7\xd8\xd4\xab\xa2\xab\x0e\x8b\x18\x94\x80\x71\x3e\x8a\x33\x8c\xcf\x30\x4b\x05\xe2\x3c\x9e\xd4\x09\xc8\x7d\xeb\x8a\x66\x2d\x3a\x98\x15\x41\xce\xb0\x5f\xf0\xb0\x9d\x8f\x9c\xb1\x61\x52\x62\x01\x35\x3c\x4d\xe2\x2a\x1d\x17\xf9\x0a\x07\x39\xbd\xbd\xcc\x5b\xd8\x35\xc8\x78\x1f\xbf\x62\xc2\x40\xe1\x93\x2a\xf2\x6e\xa3\x37\xa5\x6e\x3a\xd9\x18\x2e\xc9\x99\x7b\xbc\x0e\x8b\xcd\x94\x2a\xc5\xd0\x6f\x7a\xcf\xf1\x92\x6c\x0c\xa5\x66\x43\x01\x1d\x67\x6c\x07\x7f\xe7\xf9\x37\x4d\xab\xa4\xc9\xf1\x5b\x64\x2b\xa0\x4d\xbd\x49\xda\x8c\x9d\xa4\x43\x8f\x7d\x64\xb1\x1f\xb8\xb6\x3f\x1b\xda\xc6\xf8\x43\x77\x8c\xb3\x9f\x00\x00\x00\xff\xff\x9c\xf4\x8f\xc0\x10\x02\x00\x00") func postgres000001_create_teamsDownSqlBytes() ([]byte, error) { @@ -6376,6 +6420,46 @@ func postgres000075_alter_upload_sessions_indexUpSql() (*asset, error) { return a, nil } +var _postgres000076_upgrade_lastrootpostatDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x04\xc0\x31\x0a\x02\x31\x10\x05\xd0\xde\x53\xfc\xce\x6a\xc3\xd8\xe8\x71\x42\x48\x3e\xd9\x01\xf9\xa3\x9b\x71\xc5\xdb\xfb\xb6\x0d\x0a\x88\x1c\xc8\x40\xdf\x9b\x26\xd1\xf4\xcb\xdd\x35\xb1\x5c\x9d\xf0\xbc\x2e\xf4\x38\x79\x70\xc0\x05\x33\xb3\x87\xd5\xcf\x6b\x1e\x6d\xb0\xf6\x64\x3d\xef\xe5\x56\x46\x7c\x55\xd6\xfb\x79\xf9\x07\x00\x00\xff\xff\xa9\xaa\xb3\x07\x55\x00\x00\x00") + +func postgres000076_upgrade_lastrootpostatDownSqlBytes() ([]byte, error) { + return bindataRead( + _postgres000076_upgrade_lastrootpostatDownSql, + "postgres/000076_upgrade_lastrootpostat.down.sql", + ) +} + +func postgres000076_upgrade_lastrootpostatDownSql() (*asset, error) { + bytes, err := postgres000076_upgrade_lastrootpostatDownSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "postgres/000076_upgrade_lastrootpostat.down.sql", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +var _postgres000076_upgrade_lastrootpostatUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x52\x5d\xaf\x9a\x40\x10\x7d\xde\xfd\x15\xd3\xc4\x64\xb5\xf1\x12\xfa\x7a\x0d\x4d\x50\xd6\x2b\x0d\x82\x01\x4c\xdb\xa7\x66\x85\xf5\x42\x82\xac\xba\x63\xdb\x9f\xdf\x2c\x5f\x57\x69\xd3\xe4\xbe\xc1\xce\xcc\x99\x73\xce\x1c\x2f\x82\xc9\x84\x2e\xf9\x8b\x1f\x52\xe2\xaf\x61\x4a\x09\x49\x78\xc0\x57\x29\x64\xea\x56\xe3\xf4\xe3\x8c\x12\xb2\x8e\xa3\x2d\x94\xf5\x51\x5d\x4f\x02\x4b\x55\xff\xd0\x59\x21\x4f\xc2\xca\x54\x75\x3b\xd5\x9a\x12\xf2\x75\xc3\x63\x0e\x28\x0e\x95\xec\x8a\x0e\x3b\xdf\x0e\x55\x99\x31\x4a\x88\x1b\x7a\x5d\xad\x16\x27\xe9\xb0\xac\x10\x75\x2d\x2b\xdd\xd7\x5a\x9c\xae\x58\x09\x8d\x57\xa5\xf0\xac\x34\x0a\xec\x5b\xa6\x5d\x4f\x2e\x8f\xe2\x56\x21\xf8\x09\x84\xfb\x20\x80\x28\x86\x51\xe5\x83\x03\x8c\x31\x9b\xb1\xe7\xe7\x43\xf9\x5a\xd6\xc8\x66\x94\xcc\xc0\x81\x4f\x90\x6e\x78\x68\xf0\x82\x94\xc7\x90\xba\xcb\x80\x43\x4f\x05\xda\xc7\x55\x14\xec\xb7\x21\x3c\x72\x80\x84\xa7\xe0\xf1\xb5\xbb\x0f\x52\x60\xf6\x00\xbc\xa0\x84\x87\x1e\xf8\xeb\x05\xe5\xa1\x37\x99\x2c\x28\x7d\x87\x9f\xab\x6e\xf3\xe0\x5e\x20\x34\xc6\x4a\xe1\x4e\x69\x74\x07\x85\x86\xfb\x67\xb0\x7b\xee\x4f\x4f\x70\x2c\x7f\x4b\x0d\xa7\xf2\xf5\x2a\x50\x42\x86\x12\x44\x9d\x83\x96\xa8\x01\x0b\x39\x86\x39\xaa\xeb\x9b\x48\x2c\x04\x42\xae\x6a\x86\x50\x88\x9f\x12\x4a\x34\x73\x86\x81\x9f\x6e\xe0\x02\x6e\xd2\x30\xee\x28\x9b\x2f\xd2\xd3\xb4\xfc\xbc\xc7\x29\xf3\x79\x5b\x8a\xdc\x80\x27\x2b\x3e\xdd\xba\xdf\xa6\x66\x9d\xb6\x56\x57\x29\x50\xba\x38\x9b\x83\x3d\x33\x70\xf7\x4e\x9a\x21\x23\xfd\x01\xd7\xfc\x04\x7c\x9d\xc2\x97\xc8\x78\x46\x08\x69\x90\xcc\x57\x14\xfe\xc5\xc0\x81\x6e\x4f\xfb\xe6\xe7\xa6\xa3\xb1\xef\x6d\xd4\x32\xea\x9b\x5e\x66\xd2\x43\x5e\xe2\x68\xbf\x83\xe5\xf7\x31\x18\x25\xc4\x1c\x63\xbf\xf3\xdc\xb4\x19\xbf\xa3\x94\xf0\x46\xfd\xc8\x4b\x07\x2e\xd6\x48\x50\xaf\xe7\x42\xef\x78\x5c\xac\xc1\x29\x70\xe0\x9e\xbf\x89\xf2\xf0\xff\xef\x83\x2f\x68\x7b\xe6\xe6\xa0\xa3\x16\x54\x60\xcf\x1f\x4f\xfa\xab\xc4\x02\x6a\x05\xe7\xce\xb5\x56\xce\xb0\xa4\xc9\xee\x23\x8a\x63\xc3\xff\x02\xf7\x98\x6a\x30\xb1\xfe\x13\x00\x00\xff\xff\xa1\xa8\x3e\x66\x26\x04\x00\x00") + +func postgres000076_upgrade_lastrootpostatUpSqlBytes() ([]byte, error) { + return bindataRead( + _postgres000076_upgrade_lastrootpostatUpSql, + "postgres/000076_upgrade_lastrootpostat.up.sql", + ) +} + +func postgres000076_upgrade_lastrootpostatUpSql() (*asset, error) { + bytes, err := postgres000076_upgrade_lastrootpostatUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "postgres/000076_upgrade_lastrootpostat.up.sql", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -6578,6 +6662,8 @@ var _bindata = map[string]func() (*asset, error){ "mysql/000074_upgrade_users_v6.3.up.sql": mysql000074_upgrade_users_v63UpSql, "mysql/000075_alter_upload_sessions_index.down.sql": mysql000075_alter_upload_sessions_indexDownSql, "mysql/000075_alter_upload_sessions_index.up.sql": mysql000075_alter_upload_sessions_indexUpSql, + "mysql/000076_upgrade_lastrootpostat.down.sql": mysql000076_upgrade_lastrootpostatDownSql, + "mysql/000076_upgrade_lastrootpostat.up.sql": mysql000076_upgrade_lastrootpostatUpSql, "postgres/000001_create_teams.down.sql": postgres000001_create_teamsDownSql, "postgres/000001_create_teams.up.sql": postgres000001_create_teamsUpSql, "postgres/000002_create_team_members.down.sql": postgres000002_create_team_membersDownSql, @@ -6728,6 +6814,8 @@ var _bindata = map[string]func() (*asset, error){ "postgres/000074_upgrade_users_v6.3.up.sql": postgres000074_upgrade_users_v63UpSql, "postgres/000075_alter_upload_sessions_index.down.sql": postgres000075_alter_upload_sessions_indexDownSql, "postgres/000075_alter_upload_sessions_index.up.sql": postgres000075_alter_upload_sessions_indexUpSql, + "postgres/000076_upgrade_lastrootpostat.down.sql": postgres000076_upgrade_lastrootpostatDownSql, + "postgres/000076_upgrade_lastrootpostat.up.sql": postgres000076_upgrade_lastrootpostatUpSql, } // AssetDir returns the file names below a certain @@ -6922,6 +7010,8 @@ var _bintree = &bintree{nil, map[string]*bintree{ "000074_upgrade_users_v6.3.up.sql": &bintree{mysql000074_upgrade_users_v63UpSql, map[string]*bintree{}}, "000075_alter_upload_sessions_index.down.sql": &bintree{mysql000075_alter_upload_sessions_indexDownSql, map[string]*bintree{}}, "000075_alter_upload_sessions_index.up.sql": &bintree{mysql000075_alter_upload_sessions_indexUpSql, map[string]*bintree{}}, + "000076_upgrade_lastrootpostat.down.sql": &bintree{mysql000076_upgrade_lastrootpostatDownSql, map[string]*bintree{}}, + "000076_upgrade_lastrootpostat.up.sql": &bintree{mysql000076_upgrade_lastrootpostatUpSql, map[string]*bintree{}}, }}, "postgres": &bintree{nil, map[string]*bintree{ "000001_create_teams.down.sql": &bintree{postgres000001_create_teamsDownSql, map[string]*bintree{}}, @@ -7074,6 +7164,8 @@ var _bintree = &bintree{nil, map[string]*bintree{ "000074_upgrade_users_v6.3.up.sql": &bintree{postgres000074_upgrade_users_v63UpSql, map[string]*bintree{}}, "000075_alter_upload_sessions_index.down.sql": &bintree{postgres000075_alter_upload_sessions_indexDownSql, map[string]*bintree{}}, "000075_alter_upload_sessions_index.up.sql": &bintree{postgres000075_alter_upload_sessions_indexUpSql, map[string]*bintree{}}, + "000076_upgrade_lastrootpostat.down.sql": &bintree{postgres000076_upgrade_lastrootpostatDownSql, map[string]*bintree{}}, + "000076_upgrade_lastrootpostat.up.sql": &bintree{postgres000076_upgrade_lastrootpostatUpSql, map[string]*bintree{}}, }}, }} diff --git a/db/migrations/mysql/000076_upgrade_lastrootpostat.down.sql b/db/migrations/mysql/000076_upgrade_lastrootpostat.down.sql new file mode 100644 index 0000000000..5d99db8cec --- /dev/null +++ b/db/migrations/mysql/000076_upgrade_lastrootpostat.down.sql @@ -0,0 +1 @@ +-- no need to change anything since it's covered in 000070_upgrade_cte_v6.1.down.sql diff --git a/db/migrations/mysql/000076_upgrade_lastrootpostat.up.sql b/db/migrations/mysql/000076_upgrade_lastrootpostat.up.sql new file mode 100644 index 0000000000..f52c950a73 --- /dev/null +++ b/db/migrations/mysql/000076_upgrade_lastrootpostat.up.sql @@ -0,0 +1,47 @@ +CREATE PROCEDURE Migrate_LastRootPostAt_Default () +BEGIN + IF ( + SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_NAME = 'Channels' + AND TABLE_SCHEMA = DATABASE() + AND COLUMN_NAME = 'LastRootPostAt' + AND (COLUMN_DEFAULT IS NULL OR COLUMN_DEFAULT != 0) + ) = 1 THEN + ALTER TABLE Channels ALTER COLUMN LastRootPostAt SET DEFAULT 0; + END IF; +END; + CALL Migrate_LastRootPostAt_Default (); + DROP PROCEDURE IF EXISTS Migrate_LastRootPostAt_Default; + +CREATE PROCEDURE Migrate_LastRootPostAt_Fix () +BEGIN + IF ( + SELECT COUNT(*) + FROM Channels + WHERE LastRootPostAt IS NULL + ) > 0 THEN + -- fixes migrate cte and sets the LastRootPostAt for channels that don't have it set + UPDATE + Channels + INNER JOIN ( + SELECT + Channels.Id channelid, + COALESCE(MAX(Posts.CreateAt), 0) AS lastrootpost + FROM + Channels + LEFT JOIN Posts FORCE INDEX (idx_posts_channel_id_update_at) ON Channels.Id = Posts.ChannelId + WHERE + Posts.RootId = '' + GROUP BY + Channels.Id) AS q ON q.channelid = Channels.Id + SET + LastRootPostAt = lastrootpost + WHERE + LastRootPostAt IS NULL; + + -- sets LastRootPostAt to 0, for channels with no posts + UPDATE Channels SET LastRootPostAt=0 WHERE LastRootPostAt IS NULL; + END IF; +END; + CALL Migrate_LastRootPostAt_Fix (); + DROP PROCEDURE IF EXISTS Migrate_LastRootPostAt_Fix; diff --git a/db/migrations/postgres/000076_upgrade_lastrootpostat.down.sql b/db/migrations/postgres/000076_upgrade_lastrootpostat.down.sql new file mode 100644 index 0000000000..5d99db8cec --- /dev/null +++ b/db/migrations/postgres/000076_upgrade_lastrootpostat.down.sql @@ -0,0 +1 @@ +-- no need to change anything since it's covered in 000070_upgrade_cte_v6.1.down.sql diff --git a/db/migrations/postgres/000076_upgrade_lastrootpostat.up.sql b/db/migrations/postgres/000076_upgrade_lastrootpostat.up.sql new file mode 100644 index 0000000000..4d8a542f52 --- /dev/null +++ b/db/migrations/postgres/000076_upgrade_lastrootpostat.up.sql @@ -0,0 +1,50 @@ +DO $$ +BEGIN + IF ( + SELECT count(*) + FROM information_schema.columns + WHERE table_schema='public' + AND table_name='channels' + AND column_name='lastrootpostat' + AND (column_default IS NULL OR column_default != '''0''::bigint') + ) = 1 THEN + ALTER TABLE channels ALTER COLUMN lastrootpostat SET DEFAULT '0'::bigint; + END IF; +END$$; + +DO $$ +BEGIN + IF ( + SELECT count(*) + FROM Channels + WHERE LastRootPostAt IS NULL + ) > 0 THEN + -- fixes migrate cte and sets the LastRootPostAt for channels that don't have it set + WITH q AS ( + SELECT + Channels.Id channelid, + COALESCE(MAX(Posts.CreateAt), 0) AS lastrootpost + FROM + Channels + LEFT JOIN + Posts + ON + Channels.Id = Posts.ChannelId + WHERE + Posts.RootId = '' + GROUP BY + Channels.Id + ) + UPDATE + Channels + SET + LastRootPostAt = q.lastrootpost + FROM + q + WHERE + q.channelid = Channels.Id AND Channels.LastRootPostAt IS NULL; + + -- sets LastRootPostAt to 0, for channels with no posts + UPDATE Channels SET LastRootPostAt=0 WHERE LastRootPostAt IS NULL; + END IF; +END $$;