// Copyright 2023 The Xorm Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package tests import ( "testing" "github.com/stretchr/testify/assert" "xorm.io/xorm/schemas" ) type TestSync1 struct { Id int64 ClassId int64 `xorm:"index"` } func (TestSync1) TableName() string { return "test_sync" } type TestSync2 struct { Id int64 ClassId int64 `xorm:"unique"` } func (TestSync2) TableName() string { return "test_sync" } func TestSync(t *testing.T) { assert.NoError(t, testEngine.Sync(new(TestSync1))) assert.NoError(t, testEngine.Sync(new(TestSync2))) } // Test Sync with varchar size changed type TestSync3 struct { Id int64 Name string `xorm:"varchar(100)"` } func (TestSync3) TableName() string { return "test_sync_2" } type TestSync4 struct { Id int64 Name string `xorm:"varchar(200)"` } func (TestSync4) TableName() string { return "test_sync_2" } func Test_SyncVarcharSizeChange(t *testing.T) { if testEngine.Dialect().URI().DBType == schemas.SQLITE { t.Skip("SQLite does not support column change") } assert.NoError(t, testEngine.Sync(new(TestSync3))) tables, err := testEngine.DBMetas() assert.NoError(t, err) var testTable *schemas.Table for _, table := range tables { if table.Name == "test_sync_2" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.Equal(t, "varchar", testTable.GetColumn("name").SQLType.Name) assert.Equal(t, 100, testTable.GetColumn("name").Length) assert.NoError(t, testEngine.Sync(new(TestSync4))) tables, err = testEngine.DBMetas() assert.NoError(t, err) testTable = nil for _, table := range tables { if table.Name == "test_sync_2" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.Equal(t, "varchar", testTable.GetColumn("name").SQLType.Name) assert.Equal(t, 200, testTable.GetColumn("name").Length) } // Test Sync with varchar size changed type TestSync5 struct { Id int64 Name string `xorm:"NOT NULL"` } func (TestSync5) TableName() string { return "test_sync_3" } type TestSync6 struct { Id int64 Name string `xorm:"NULL"` } func (TestSync6) TableName() string { return "test_sync_3" } func Test_SyncVarcharNullableChanged(t *testing.T) { if testEngine.Dialect().URI().DBType == schemas.SQLITE { t.Skip("SQLite does not support column change") } assert.NoError(t, testEngine.Sync(new(TestSync5))) tables, err := testEngine.DBMetas() assert.NoError(t, err) var testTable *schemas.Table for _, table := range tables { if table.Name == "test_sync_3" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.False(t, testTable.GetColumn("name").Nullable) assert.NoError(t, testEngine.Sync(new(TestSync6))) tables, err = testEngine.DBMetas() assert.NoError(t, err) testTable = nil for _, table := range tables { if table.Name == "test_sync_3" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.True(t, testTable.GetColumn("name").Nullable) } // Test Sync with varchar size changed type TestSync7 struct { Id int64 Name string `xorm:"DEFAULT '1'"` } func (TestSync7) TableName() string { return "test_sync_4" } type TestSync8 struct { Id int64 Name string `xorm:"DEFAULT '2'"` } func (TestSync8) TableName() string { return "test_sync_4" } func Test_SyncVarcharDefaultChange(t *testing.T) { if testEngine.Dialect().URI().DBType == schemas.SQLITE { t.Skip("SQLite does not support column change") } assert.NoError(t, testEngine.Sync(new(TestSync7))) tables, err := testEngine.DBMetas() assert.NoError(t, err) var testTable *schemas.Table for _, table := range tables { if table.Name == "test_sync_4" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.Equal(t, "1", testTable.GetColumn("name").Default) assert.NoError(t, testEngine.Sync(new(TestSync8))) tables, err = testEngine.DBMetas() assert.NoError(t, err) testTable = nil for _, table := range tables { if table.Name == "test_sync_4" { testTable = table break } } assert.NotNil(t, testTable) assert.Len(t, testTable.Columns(), 2) assert.Equal(t, "2", testTable.GetColumn("name").Default) }