forgejo/models/migrations/v1_23/v303_test.go
forgejo-backport-action 2d1e163913 [v10.0/forgejo] fix: reduce noise for the v303 migration (#6594)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/6591

Using SELECT `%s` FROM `%s` WHERE 0 = 1 to assert the existence of a column is simple but noisy: it shows errors in the migrations that are confusing for Forgejo admins because they are not actual errors.

Use introspection instead, which is more complicated but leads to the same result.

Add a test that ensures it works as expected, for all database types. Although the migration is run for all database types, it does not account for various scenarios and is never tested in the case a column does not exist.

Refs: https://codeberg.org/forgejo/forgejo/issues/6583

Co-authored-by: Earl Warren <contact@earl-warren.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6594
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
2025-01-17 08:15:16 +00:00

41 lines
922 B
Go

// Copyright 2025 The Forgejo Authors.
// SPDX-License-Identifier: GPL-3.0-or-later
package v1_23 //nolint
import (
"testing"
migration_tests "code.gitea.io/gitea/models/migrations/test"
"github.com/stretchr/testify/require"
"xorm.io/xorm/schemas"
)
func Test_GiteaLastDrop(t *testing.T) {
type Badge struct {
ID int64 `xorm:"pk autoincr"`
Slug string
}
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Badge))
defer deferable()
if x == nil || t.Failed() {
return
}
getColumn := func() *schemas.Column {
tables, err := x.DBMetas()
require.NoError(t, err)
require.Len(t, tables, 1)
table := tables[0]
require.Equal(t, "badge", table.Name)
return table.GetColumn("slug")
}
require.NotNil(t, getColumn(), "slug column exists")
require.NoError(t, GiteaLastDrop(x))
require.Nil(t, getColumn(), "slug column was deleted")
// idempotent
require.NoError(t, GiteaLastDrop(x))
}