@@ -578,12 +578,6 @@ def copy_table(from, to, options = {})
578
578
options [ :rename ] [ column . name . to_sym ] ||
579
579
column . name ) : column . name
580
580
581
- if column . has_default?
582
- type = lookup_cast_type_from_column ( column )
583
- default = type . deserialize ( column . default )
584
- default = -> { column . default_function } if default . nil?
585
- end
586
-
587
581
column_options = {
588
582
limit : column . limit ,
589
583
precision : column . precision ,
@@ -593,19 +587,31 @@ def copy_table(from, to, options = {})
593
587
primary_key : column_name == from_primary_key
594
588
}
595
589
596
- unless column . auto_increment?
597
- column_options [ :default ] = default
590
+ if column . virtual?
591
+ column_options [ :as ] = column . default_function
592
+ column_options [ :stored ] = column . virtual_stored?
593
+ column_options [ :type ] = column . type
594
+ elsif column . has_default?
595
+ type = lookup_cast_type_from_column ( column )
596
+ default = type . deserialize ( column . default )
597
+ default = -> { column . default_function } if default . nil?
598
+
599
+ unless column . auto_increment?
600
+ column_options [ :default ] = default
601
+ end
598
602
end
599
603
600
- column_type = column . bigint? ? :bigint : column . type
604
+ column_type = column . virtual? ? :virtual : ( column . bigint? ? :bigint : column . type )
601
605
@definition . column ( column_name , column_type , **column_options )
602
606
end
603
607
604
608
yield @definition if block_given?
605
609
end
606
610
copy_table_indexes ( from , to , options [ :rename ] || { } )
611
+
612
+ columns_to_copy = @definition . columns . reject { |col | col . options . key? ( :as ) } . map ( &:name )
607
613
copy_table_contents ( from , to ,
608
- @definition . columns . map ( & :name ) ,
614
+ columns_to_copy ,
609
615
options [ :rename ] || { } )
610
616
end
611
617
0 commit comments