@@ -331,6 +331,45 @@ func assignColumnIndexesInSchema(schema sql.Schema) sql.Schema {
331331 return newSch
332332}
333333
334+ func (b * Builder ) getIndexDefs (table sql.Table ) sql.IndexDefs {
335+ idxTbl , isIdxTbl := table .(sql.IndexAddressableTable )
336+ if ! isIdxTbl {
337+ return nil
338+ }
339+ var idxDefs sql.IndexDefs
340+ idxs , err := idxTbl .GetIndexes (b .ctx )
341+ if err != nil {
342+ b .handleErr (err )
343+ }
344+ for _ , idx := range idxs {
345+ if idx .IsGenerated () {
346+ continue
347+ }
348+ constraint := sql .IndexConstraint_None
349+ if idx .IsUnique () {
350+ if idx .ID () == "PRIMARY" {
351+ constraint = sql .IndexConstraint_Primary
352+ } else {
353+ constraint = sql .IndexConstraint_Unique
354+ }
355+ }
356+ columns := make ([]sql.IndexColumn , len (idx .Expressions ()))
357+ for i , col := range idx .Expressions () {
358+ // TODO: find a better way to get only the column name if the table is present
359+ col = strings .TrimPrefix (col , idxTbl .Name ()+ "." )
360+ columns [i ] = sql.IndexColumn {Name : col }
361+ }
362+ idxDefs = append (idxDefs , & sql.IndexDef {
363+ Name : idx .ID (),
364+ Storage : sql .IndexUsing_Default ,
365+ Constraint : constraint ,
366+ Columns : columns ,
367+ Comment : idx .Comment (),
368+ })
369+ }
370+ return idxDefs
371+ }
372+
334373func (b * Builder ) buildCreateTableLike (inScope * scope , ct * ast.DDL ) * scope {
335374 database := b .resolveDbForTable (ct .Table )
336375 newTableName := strings .ToLower (ct .Table .Name .String ())
@@ -397,59 +436,16 @@ func (b *Builder) buildCreateTableLike(inScope *scope, ct *ast.DDL) *scope {
397436 }
398437
399438 // Load index definitions
400- if idxTbl , isIdxTbl := lTable .Table .(sql.IndexAddressableTable ); isIdxTbl {
401- idxs , err := idxTbl .GetIndexes (b .ctx )
402- if err != nil {
403- b .handleErr (err )
404- }
405- for _ , idx := range idxs {
406- if idx .IsGenerated () {
407- continue
408- }
409- constraint := sql .IndexConstraint_None
410- if idx .IsUnique () {
411- if idx .ID () == "PRIMARY" {
412- // TODO: deal with multiple primary key constraints?
413- constraint = sql .IndexConstraint_Primary
414- } else {
415- constraint = sql .IndexConstraint_Unique
416- }
417- }
418-
419- columns := make ([]sql.IndexColumn , len (idx .Expressions ()))
420- for i , col := range idx .Expressions () {
421- // TODO: find a better way to get only the column name if the table is present
422- col = strings .TrimPrefix (col , idxTbl .Name ()+ "." )
423- columns [i ] = sql.IndexColumn {Name : col }
424- }
425- idxDefs = append (idxDefs , & sql.IndexDef {
426- Name : idx .ID (),
427- Storage : sql .IndexUsing_Default ,
428- Constraint : constraint ,
429- Columns : columns ,
430- Comment : idx .Comment (),
431- })
432- }
433- }
439+ idxDefs = append (idxDefs , b .getIndexDefs (lTable .Table )... )
434440
435441 // Load check constraints
436- if chkTable , isChkTable := lTable .Table .(sql.CheckTable ); isChkTable {
437- checks , err := chkTable .GetChecks (b .ctx )
438- if err != nil {
439- b .handleErr (err )
440- }
441- for _ , check := range checks {
442- checkConstraint := b .buildCheckConstraint (outScope , & check )
443- if err != nil {
444- b .handleErr (err )
445- }
446-
447- // Prevent a name collision between old and new checks.
448- // New check name will be assigned a name during building.
449- checkConstraint .Name = ""
450- checkDefs = append (checkDefs , checkConstraint )
451- }
442+ newCheckDefs := b .loadChecksFromTable (outScope , lTable .Table )
443+ for _ , check := range newCheckDefs {
444+ // Prevent a name collision between old and new checks.
445+ // New check name will be assigned a name during building.
446+ check .Name = ""
452447 }
448+ checkDefs = append (checkDefs , newCheckDefs ... )
453449 }
454450
455451 var hasSkippedCols bool
0 commit comments