main

mattermost/focalboard

Last updated at: 29/12/2023 09:49

migration36_test.go

TLDR

This file contains a Go test function that verifies the addition of a unique constraint to a table named focalboard_category_boards. The test function includes two sub-tests, one to check if the constraint doesn't already exist, and another to check if the constraint already exists.

Methods

There are no methods defined in this file.

Classes

There are no classes defined in this file.

package migrationstests

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func Test36AddUniqueConstraintToCategoryBoards(t *testing.T) {
	t.Run("constraint doesn't alreadt exists", func(t *testing.T) {
		th, tearDown := SetupTestHelper(t)
		defer tearDown()

		th.f.MigrateToStep(36)

		// verifying if constraint has been added

		//can't verify in sqlite, so skipping it
		if th.IsSQLite() {
			return
		}

		var count int
		query := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS " +
			"WHERE constraint_name = 'unique_user_category_board' " +
			"AND constraint_type = 'UNIQUE' " +
			"AND table_name = 'focalboard_category_boards'"
		th.f.DB().Get(&count, query)

		require.Equal(t, 1, count)
	})

	t.Run("constraint already exists", func(t *testing.T) {
		th, tearDown := SetupTestHelper(t)
		defer tearDown()

		// SQLIte doesn't support adding constraint to existing table
		// and neither do we, so skipping for sqlite
		if th.IsSQLite() {
			return
		}

		th.f.MigrateToStep(35)

		if th.IsMySQL() {
			th.f.DB().Exec("alter table focalboard_category_boards add constraint unique_user_category_board UNIQUE(user_id, board_id);")
		} else if th.IsPostgres() {
			th.f.DB().Exec("ALTER TABLE focalboard_category_boards ADD CONSTRAINT unique_user_category_board UNIQUE(user_id, board_id);")
		}

		th.f.MigrateToStep(36)

		var schema string
		if th.IsMySQL() {
			schema = "DATABASE()"
		} else if th.IsPostgres() {
			schema = "'public'"
		}

		var count int
		query := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS " +
			"WHERE constraint_schema =  " + schema + " " +
			"AND constraint_name = 'unique_user_category_board' " +
			"AND constraint_type = 'UNIQUE' " +
			"AND table_name = 'focalboard_category_boards'"
		th.f.DB().Get(&count, query)
		require.Equal(t, 1, count)
	})
}