jojo/models/forgejo_migrations/index_utils_test.go
Gusted d1cef852ee feat: rework notification table (#9926)
This change is motivated by 5e300a2a87

- Drop the `updated_by` and `commit_id` column, they are unused and have a index for no reason.
- Drop the index on `status` and `created_unix` and make a index on `(user_id, status)`.

## Test
1. Run migration.
2. Confirm the migration succeeds.
3. Check that `notification` table has the correct indexes.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9926
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-11-29 23:03:56 +01:00

61 lines
1.6 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package forgejo_migrations
import (
"testing"
migration_tests "forgejo.org/models/gitea_migrations/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDropIndexIfExists(t *testing.T) {
type Table struct {
ID int64 `xorm:"pk"`
DoerID int64 `xorm:"INDEX INDEX(s)"`
OwnerID int64 `xorm:"INDEX"`
RepoID int64 `xorm:"INDEX(s)"`
}
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Table))
defer deferable()
if x == nil || t.Failed() {
return
}
exists, err := indexExists(x, "table", "IDX_table_doer_id")
require.NoError(t, err)
assert.True(t, exists)
exists, err = indexExists(x, "table", "IDX_table_owner_id")
require.NoError(t, err)
assert.True(t, exists)
exists, err = indexExists(x, "table", "IDX_table_repo_id")
require.NoError(t, err)
assert.False(t, exists)
exists, err = indexExists(x, "table", "IDX_table_s")
require.NoError(t, err)
assert.True(t, exists)
require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_repo_id"))
require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_doer_id"))
exists, err = indexExists(x, "table", "IDX_table_doer_id")
require.NoError(t, err)
assert.False(t, exists)
require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_s"))
exists, err = indexExists(x, "table", "IDX_table_s")
require.NoError(t, err)
assert.False(t, exists)
require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_owner_id"))
exists, err = indexExists(x, "table", "IDX_table_owner_id")
require.NoError(t, err)
assert.False(t, exists)
}