Skip to content

Commit a54d7c2

Browse files
committed
Pass deseriallized defaults when copying tables in sqlite3
1 parent b55e834 commit a54d7c2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,13 @@ def copy_table(from, to, options = {})
497497
options[:rename][column.name.to_sym] ||
498498
column.name) : column.name
499499

500+
if column.has_default?
501+
type = lookup_cast_type_from_column(column)
502+
default = type.deserialize(column.default)
503+
end
504+
500505
@definition.column(column_name, column.type,
501-
limit: column.limit, default: column.default,
506+
limit: column.limit, default: default,
502507
precision: column.precision, scale: column.scale,
503508
null: column.null, collation: column.collation,
504509
primary_key: column_name == from_primary_key

activerecord/test/cases/adapters/sqlite3/copy_table_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def test_copy_table(from = "customers", to = "customers2", options = {})
2525
@connection.drop_table(to) rescue nil
2626
end
2727

28+
def test_copy_table_with_column_with_default
29+
test_copy_table("comments", "comments_with_default") do
30+
@connection.add_column("comments_with_default", "options", "json", default: {})
31+
test_copy_table("comments_with_default", "comments_with_default2") do
32+
column = @connection.columns("comments_with_default2").find { |col| col.name == "options" }
33+
assert_equal "{}", column.default
34+
end
35+
end
36+
end
37+
2838
def test_copy_table_renaming_column
2939
test_copy_table("customers", "customers2",
3040
rename: { "name" => "person_name" }) do |from, to, options|

0 commit comments

Comments
 (0)