@@ -106,3 +106,55 @@ func TestGetTableSchemaCaseSensitiveIdentifiers(t *testing.T) {
106106 assertSchema (schemas [ulTable ], []string {"id" }, "id" , "ul" )
107107 assertSchema (schemas [uuTable ], []string {"id" }, "id" , "uu" )
108108}
109+
110+ func TestGetTableSchemaPrimaryKeyVariants (t * testing.T ) {
111+ t .Parallel ()
112+ ctx := t .Context ()
113+ connector := newTestConnector (t , ctx )
114+
115+ dbName := "pk_variants_test"
116+ createTestDB (t , ctx , connector , dbName )
117+
118+ exec := func (sql string ) {
119+ t .Helper ()
120+ _ , err := connector .Execute (ctx , sql )
121+ require .NoError (t , err )
122+ }
123+
124+ assertSchema := func (schema * protos.TableSchema , pk []string , cols ... string ) {
125+ t .Helper ()
126+ require .NotNil (t , schema )
127+ if len (pk ) == 0 {
128+ require .Empty (t , schema .PrimaryKeyColumns )
129+ } else {
130+ require .Equal (t , pk , schema .PrimaryKeyColumns )
131+ }
132+ require .Len (t , schema .Columns , len (cols ))
133+ for i , name := range cols {
134+ require .Equal (t , name , schema .Columns [i ].Name )
135+ }
136+ }
137+
138+ compositeTable := dbName + ".composite_pk"
139+ noPKTable := dbName + ".no_pk"
140+ mixedCaseTable := dbName + ".mixed_case_pk"
141+
142+ // Composite PK whose key order (b, a) differs from column definition order (a, b, c),
143+ // exercising the seq_in_index sort.
144+ exec (fmt .Sprintf ("CREATE TABLE %s (a INT, b INT, c INT, PRIMARY KEY (b, a))" , compositeTable ))
145+ // Table without a primary key, exercising the LEFT JOIN's NULL seq_in_index path.
146+ exec (fmt .Sprintf ("CREATE TABLE %s (a INT, b TEXT)" , noPKTable ))
147+ // PK on a mixed-case column name, exercising the case-preserving column_name join.
148+ exec (fmt .Sprintf ("CREATE TABLE %s (`MyId` INT PRIMARY KEY, val TEXT)" , mixedCaseTable ))
149+
150+ schemas , err := connector .GetTableSchema (ctx , nil , shared .InternalVersion_Latest , protos .TypeSystem_Q ,
151+ []* protos.TableMapping {
152+ {SourceTableIdentifier : compositeTable },
153+ {SourceTableIdentifier : noPKTable },
154+ {SourceTableIdentifier : mixedCaseTable },
155+ })
156+ require .NoError (t , err )
157+ assertSchema (schemas [compositeTable ], []string {"b" , "a" }, "a" , "b" , "c" )
158+ assertSchema (schemas [noPKTable ], nil , "a" , "b" )
159+ assertSchema (schemas [mixedCaseTable ], []string {"MyId" }, "MyId" , "val" )
160+ }
0 commit comments