github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdcv2/metadata/sql/tables_test.go (about) 1 // Copyright 2023 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package sql 15 16 import ( 17 "database/sql" 18 "testing" 19 20 "github.com/DATA-DOG/go-sqlmock" 21 ormUtil "github.com/pingcap/tiflow/engine/pkg/orm" 22 "github.com/stretchr/testify/require" 23 "gorm.io/gorm" 24 ) 25 26 func newMockDB(t *testing.T) (*sql.DB, *gorm.DB, sqlmock.Sqlmock) { 27 backendDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) 28 require.NoError(t, err) 29 30 mock.ExpectQuery("SELECT VERSION()"). 31 WillReturnRows(sqlmock.NewRows([]string{"VERSION()"}).AddRow("5.7.35-log")) 32 db, err := ormUtil.NewGormDB(backendDB, "mysql") 33 require.NoError(t, err) 34 return backendDB, db, mock 35 } 36 37 func TestAutoMigrateTables(t *testing.T) { 38 t.Parallel() 39 t.Skip("skip this test since it's unstable") 40 41 backendDB, db, mock := newMockDB(t) 42 defer backendDB.Close() 43 44 mock.ExpectExec("CREATE TABLE `upstream` (`id` bigint(20) unsigned,`endpoints` text NOT NULL," + 45 "`config` text,`version` bigint(20) unsigned NOT NULL,`update_at` datetime(6) NOT NULL," + 46 "PRIMARY KEY (`id`))"). 47 WillReturnResult(sqlmock.NewResult(0, 0)) 48 49 mock.ExpectExec("CREATE TABLE `changefeed_info` (`uuid` bigint(20) unsigned,`namespace` varchar(128) NOT NULL," + 50 "`id` varchar(128) NOT NULL,`upstream_id` bigint(20) unsigned NOT NULL,`sink_uri` text NOT NULL," + 51 "`start_ts` bigint(20) unsigned NOT NULL,`target_ts` bigint(20) unsigned NOT NULL,`config` longtext NOT NULL," + 52 "`removed_at` datetime(6),`version` bigint(20) unsigned NOT NULL,`update_at` datetime(6) NOT NULL," + 53 "PRIMARY KEY (`uuid`),UNIQUE INDEX `namespace` (`namespace`,`id`),INDEX `upstream_id` (`upstream_id`))"). 54 WillReturnResult(sqlmock.NewResult(0, 0)) 55 56 mock.ExpectExec("CREATE TABLE `changefeed_state` (`changefeed_uuid` bigint(20) unsigned,`state` text NOT NULL," + 57 "`warning` text,`error` text,`version` bigint(20) unsigned NOT NULL,`update_at` datetime(6) NOT NULL," + 58 "PRIMARY KEY (`changefeed_uuid`))"). 59 WillReturnResult(sqlmock.NewResult(0, 0)) 60 61 mock.ExpectExec("CREATE TABLE `schedule` (`changefeed_uuid` bigint(20) unsigned,`owner` varchar(128)," + 62 "`owner_state` text NOT NULL,`processors` text,`task_position` text NOT NULL,`version` bigint(20) unsigned NOT NULL," + 63 "`update_at` datetime(6) NOT NULL,PRIMARY KEY (`changefeed_uuid`))"). 64 WillReturnResult(sqlmock.NewResult(0, 0)) 65 66 mock.ExpectExec("CREATE TABLE `progress` (`capture_id` varchar(128),`progress` longtext," + 67 "`version` bigint(20) unsigned NOT NULL,`update_at` datetime(6) NOT NULL,PRIMARY KEY (`capture_id`))"). 68 WillReturnResult(sqlmock.NewResult(0, 0)) 69 70 err := AutoMigrate(db) 71 require.NoError(t, err) 72 }