2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "support/schema_dumping_helper"
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
class SchemaDumperTest < ActiveRecord::TestCase
|
2014-09-02 22:17:27 -04:00
|
|
|
include SchemaDumpingHelper
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2014-08-15 07:10:42 -04:00
|
|
|
|
2014-05-23 10:32:42 -04:00
|
|
|
setup do
|
2012-10-15 17:22:15 -04:00
|
|
|
ActiveRecord::SchemaMigration.create_table
|
2011-07-29 15:23:37 -04:00
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def standard_dump
|
2014-09-10 18:32:29 -04:00
|
|
|
@@standard_dump ||= perform_schema_dump
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_schema_dump
|
|
|
|
dump_all_table_schema []
|
2011-07-29 15:23:37 -04:00
|
|
|
end
|
|
|
|
|
2017-06-15 06:50:05 -04:00
|
|
|
def test_dump_schema_information_with_empty_versions
|
|
|
|
ActiveRecord::SchemaMigration.delete_all
|
|
|
|
schema_info = ActiveRecord::Base.connection.dump_schema_information
|
|
|
|
assert_no_match(/INSERT INTO/, schema_info)
|
|
|
|
end
|
|
|
|
|
2012-01-13 14:04:13 -05:00
|
|
|
def test_dump_schema_information_outputs_lexically_ordered_versions
|
|
|
|
versions = %w{ 20100101010101 20100201010101 20100301010101 }
|
2014-10-13 06:47:16 -04:00
|
|
|
versions.reverse_each do |v|
|
2016-08-06 13:37:57 -04:00
|
|
|
ActiveRecord::SchemaMigration.create!(version: v)
|
2012-01-13 14:04:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
schema_info = ActiveRecord::Base.connection.dump_schema_information
|
|
|
|
assert_match(/20100201010101.*20100301010101/m, schema_info)
|
2019-06-20 08:00:42 -04:00
|
|
|
assert_includes schema_info, "20100101010101"
|
2014-08-15 07:10:42 -04:00
|
|
|
ensure
|
|
|
|
ActiveRecord::SchemaMigration.delete_all
|
2012-01-13 14:04:13 -05:00
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
2016-10-10 14:55:06 -04:00
|
|
|
assert_no_match %r{(?<=, ) do \|t\|}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
2016-01-11 20:51:54 -05:00
|
|
|
assert_no_match %r{create_table "ar_internal_metadata"}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2014-12-18 04:07:23 -05:00
|
|
|
def test_schema_dump_uses_force_cascade_on_create_table
|
|
|
|
output = dump_table_schema "authors"
|
2017-08-27 09:16:05 -04:00
|
|
|
assert_match %r{create_table "authors",.* force: :cascade}, output
|
2014-12-18 04:07:23 -05:00
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_excludes_sqlite_sequence
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{create_table "sqlite_sequence"}, output
|
|
|
|
end
|
2006-11-13 22:32:16 -05:00
|
|
|
|
2009-04-20 16:48:02 -04:00
|
|
|
def test_schema_dump_includes_camelcase_table_name
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{create_table "CamelCase"}, output
|
|
|
|
end
|
|
|
|
|
2016-08-22 14:46:04 -04:00
|
|
|
def assert_no_line_up(lines, pattern)
|
2008-05-06 19:08:23 -04:00
|
|
|
return assert(true) if lines.empty?
|
|
|
|
matches = lines.map { |line| line.match(pattern) }
|
|
|
|
matches.compact!
|
|
|
|
return assert(true) if matches.empty?
|
2016-08-22 14:46:04 -04:00
|
|
|
line_matches = lines.map { |line| [line, line.match(pattern)] }.select { |line, match| match }
|
|
|
|
assert line_matches.all? { |line, match|
|
|
|
|
start = match.offset(0).first
|
|
|
|
line[start - 2..start - 1] == ", "
|
|
|
|
}
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def column_definition_lines(output = standard_dump)
|
2016-08-16 03:30:11 -04:00
|
|
|
output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) }
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2016-08-22 14:46:04 -04:00
|
|
|
def test_types_no_line_up
|
2008-05-06 19:08:23 -04:00
|
|
|
column_definition_lines.each do |column_set|
|
|
|
|
next if column_set.empty?
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2016-08-22 14:46:04 -04:00
|
|
|
assert column_set.all? { |column| !column.match(/\bt\.\w+\s{2,}/) }
|
2006-04-26 02:15:51 -04:00
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2016-08-22 14:46:04 -04:00
|
|
|
def test_arguments_no_line_up
|
2008-05-06 19:08:23 -04:00
|
|
|
column_definition_lines.each do |column_set|
|
2016-08-22 14:46:04 -04:00
|
|
|
assert_no_line_up(column_set, /default: /)
|
|
|
|
assert_no_line_up(column_set, /limit: /)
|
|
|
|
assert_no_line_up(column_set, /null: /)
|
2006-04-26 02:15:51 -04:00
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_no_dump_errors
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{\# Could not dump table}, output
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_not_null_columns
|
2014-09-02 22:17:27 -04:00
|
|
|
output = dump_all_table_schema([/^[^r]/])
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{null: false}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-06-14 22:55:56 -04:00
|
|
|
def test_schema_dump_includes_limit_constraint_for_integer_columns
|
2014-09-02 22:17:27 -04:00
|
|
|
output = dump_all_table_schema([/^(?!integer_limits)/])
|
2008-06-14 22:55:56 -04:00
|
|
|
|
2016-12-20 01:20:38 -05:00
|
|
|
assert_match %r{"c_int_without_limit"(?!.*limit)}, output
|
2014-09-10 17:22:41 -04:00
|
|
|
|
2008-06-14 22:55:56 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{c_int_1.*limit: 2}, output
|
|
|
|
assert_match %r{c_int_2.*limit: 2}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
|
|
|
|
# int 3 is 4 bytes in postgresql
|
2016-12-20 01:20:38 -05:00
|
|
|
assert_match %r{"c_int_3"(?!.*limit)}, output
|
|
|
|
assert_match %r{"c_int_4"(?!.*limit)}, output
|
2015-12-15 17:01:30 -05:00
|
|
|
elsif current_adapter?(:Mysql2Adapter)
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{c_int_1.*limit: 1}, output
|
|
|
|
assert_match %r{c_int_2.*limit: 2}, output
|
|
|
|
assert_match %r{c_int_3.*limit: 3}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
|
2016-12-20 01:20:38 -05:00
|
|
|
assert_match %r{"c_int_4"(?!.*limit)}, output
|
2010-10-19 13:18:54 -04:00
|
|
|
elsif current_adapter?(:SQLite3Adapter)
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{c_int_1.*limit: 1}, output
|
|
|
|
assert_match %r{c_int_2.*limit: 2}, output
|
|
|
|
assert_match %r{c_int_3.*limit: 3}, output
|
|
|
|
assert_match %r{c_int_4.*limit: 4}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
end
|
|
|
|
|
2016-12-18 23:26:04 -05:00
|
|
|
if current_adapter?(:SQLite3Adapter, :OracleAdapter)
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{c_int_5.*limit: 5}, output
|
|
|
|
assert_match %r{c_int_6.*limit: 6}, output
|
|
|
|
assert_match %r{c_int_7.*limit: 7}, output
|
|
|
|
assert_match %r{c_int_8.*limit: 8}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
else
|
2016-03-06 16:25:54 -05:00
|
|
|
assert_match %r{t\.bigint\s+"c_int_5"$}, output
|
|
|
|
assert_match %r{t\.bigint\s+"c_int_6"$}, output
|
|
|
|
assert_match %r{t\.bigint\s+"c_int_7"$}, output
|
|
|
|
assert_match %r{t\.bigint\s+"c_int_8"$}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_with_string_ignored_table
|
2016-08-06 12:26:20 -04:00
|
|
|
output = dump_all_table_schema(["accounts"])
|
2008-05-06 19:08:23 -04:00
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
2016-01-11 20:51:54 -05:00
|
|
|
assert_no_match %r{create_table "ar_internal_metadata"}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_with_regexp_ignored_table
|
2014-09-02 22:17:27 -04:00
|
|
|
output = dump_all_table_schema([/^account/])
|
2008-05-06 19:08:23 -04:00
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
2016-01-11 20:51:54 -05:00
|
|
|
assert_no_match %r{create_table "ar_internal_metadata"}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2009-04-20 11:47:31 -04:00
|
|
|
def test_schema_dumps_index_columns_in_right_order
|
2017-02-12 13:51:44 -05:00
|
|
|
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_index/).first.strip
|
2017-12-02 14:57:04 -05:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
2017-04-15 16:17:14 -04:00
|
|
|
if ActiveRecord::Base.connection.supports_index_sort_order?
|
|
|
|
assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index", length: { type: 10 }, order: { rating: :desc }', index_definition
|
|
|
|
else
|
|
|
|
assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index", length: { type: 10 }', index_definition
|
|
|
|
end
|
2017-12-02 14:57:04 -05:00
|
|
|
elsif ActiveRecord::Base.connection.supports_index_sort_order?
|
|
|
|
assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index", order: { rating: :desc }', index_definition
|
2013-03-30 14:53:49 -04:00
|
|
|
else
|
2017-10-25 14:19:37 -04:00
|
|
|
assert_equal 't.index ["firm_id", "type", "rating"], name: "company_index"', index_definition
|
2013-03-27 22:38:53 -04:00
|
|
|
end
|
2009-04-20 11:47:31 -04:00
|
|
|
end
|
2009-08-09 04:56:25 -04:00
|
|
|
|
2012-02-09 00:28:11 -05:00
|
|
|
def test_schema_dumps_partial_indices
|
2017-02-12 13:51:44 -05:00
|
|
|
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_partial_index/).first.strip
|
2018-05-17 07:10:00 -04:00
|
|
|
if ActiveRecord::Base.connection.supports_partial_index?
|
2017-02-12 13:51:44 -05:00
|
|
|
assert_equal 't.index ["firm_id", "type"], name: "company_partial_index", where: "(rating > 10)"', index_definition
|
2012-02-09 00:28:11 -05:00
|
|
|
else
|
2015-01-18 13:36:09 -05:00
|
|
|
assert_equal 't.index ["firm_id", "type"], name: "company_partial_index"', index_definition
|
2012-02-09 00:28:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-02 14:57:04 -05:00
|
|
|
def test_schema_dumps_index_sort_order
|
|
|
|
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*_name_and_rating/).first.strip
|
|
|
|
if ActiveRecord::Base.connection.supports_index_sort_order?
|
|
|
|
assert_equal 't.index ["name", "rating"], name: "index_companies_on_name_and_rating", order: :desc', index_definition
|
|
|
|
else
|
|
|
|
assert_equal 't.index ["name", "rating"], name: "index_companies_on_name_and_rating"', index_definition
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dumps_index_length
|
|
|
|
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*_name_and_description/).first.strip
|
|
|
|
if current_adapter?(:Mysql2Adapter)
|
|
|
|
assert_equal 't.index ["name", "description"], name: "index_companies_on_name_and_description", length: 10', index_definition
|
|
|
|
else
|
|
|
|
assert_equal 't.index ["name", "description"], name: "index_companies_on_name_and_description"', index_definition
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-05 03:59:25 -04:00
|
|
|
def test_schema_dump_should_honor_nonstandard_primary_keys
|
|
|
|
output = standard_dump
|
|
|
|
match = output.match(%r{create_table "movies"(.*)do})
|
|
|
|
assert_not_nil(match, "nonstandardpk table not found")
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r(primary_key: "movieid"), match[1], "non-standard primary key not preserved"
|
2009-07-05 03:59:25 -04:00
|
|
|
end
|
2009-04-20 11:47:31 -04:00
|
|
|
|
2013-10-21 14:20:28 -04:00
|
|
|
def test_schema_dump_should_use_false_as_default
|
2017-08-27 09:18:23 -04:00
|
|
|
output = dump_table_schema "booleans"
|
2013-10-21 14:20:28 -04:00
|
|
|
assert_match %r{t\.boolean\s+"has_fun",.+default: false}, output
|
|
|
|
end
|
|
|
|
|
2016-05-31 10:15:46 -04:00
|
|
|
def test_schema_dump_does_not_include_limit_for_text_field
|
2017-08-27 09:18:23 -04:00
|
|
|
output = dump_table_schema "admin_users"
|
2016-05-31 10:15:46 -04:00
|
|
|
assert_match %r{t\.text\s+"params"$}, output
|
|
|
|
end
|
2006-07-08 16:35:56 -04:00
|
|
|
|
2016-05-31 10:15:46 -04:00
|
|
|
def test_schema_dump_does_not_include_limit_for_binary_field
|
2017-08-27 09:18:23 -04:00
|
|
|
output = dump_table_schema "binaries"
|
2016-05-31 10:15:46 -04:00
|
|
|
assert_match %r{t\.binary\s+"data"$}, output
|
|
|
|
end
|
|
|
|
|
2017-08-27 09:18:23 -04:00
|
|
|
def test_schema_dump_does_not_include_limit_for_float_field
|
|
|
|
output = dump_table_schema "numeric_data"
|
|
|
|
assert_match %r{t\.float\s+"temperature"$}, output
|
|
|
|
end
|
|
|
|
|
2018-09-13 16:00:51 -04:00
|
|
|
if ActiveRecord::Base.connection.supports_expression_index?
|
|
|
|
def test_schema_dump_expression_indices
|
|
|
|
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_expression_index/).first.strip
|
2018-10-24 18:29:51 -04:00
|
|
|
index_definition.sub!(/, name: "company_expression_index"\z/, "")
|
2018-09-13 16:00:51 -04:00
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2018-10-24 18:29:51 -04:00
|
|
|
assert_match %r{CASE.+lower\(\(name\)::text\).+END\) DESC"\z}i, index_definition
|
|
|
|
elsif current_adapter?(:Mysql2Adapter)
|
|
|
|
assert_match %r{CASE.+lower\(`name`\).+END\) DESC"\z}i, index_definition
|
2018-09-13 16:00:51 -04:00
|
|
|
elsif current_adapter?(:SQLite3Adapter)
|
2018-10-24 18:29:51 -04:00
|
|
|
assert_match %r{CASE.+lower\(name\).+END\) DESC"\z}i, index_definition
|
2018-09-13 16:00:51 -04:00
|
|
|
else
|
|
|
|
assert false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-31 10:15:46 -04:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
2012-10-16 05:53:18 -04:00
|
|
|
def test_schema_dump_includes_length_for_mysql_binary_fields
|
2019-01-25 08:01:07 -05:00
|
|
|
output = dump_table_schema "binary_fields"
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.binary\s+"var_binary",\s+limit: 255$}, output
|
|
|
|
assert_match %r{t\.binary\s+"var_binary_large",\s+limit: 4095$}, output
|
2012-10-16 05:53:18 -04:00
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
|
2019-01-25 08:01:07 -05:00
|
|
|
output = dump_table_schema "binary_fields"
|
|
|
|
assert_match %r{t\.binary\s+"tiny_blob",\s+size: :tiny$}, output
|
2016-05-31 10:15:46 -04:00
|
|
|
assert_match %r{t\.binary\s+"normal_blob"$}, output
|
2019-01-25 08:01:07 -05:00
|
|
|
assert_match %r{t\.binary\s+"medium_blob",\s+size: :medium$}, output
|
|
|
|
assert_match %r{t\.binary\s+"long_blob",\s+size: :long$}, output
|
|
|
|
assert_match %r{t\.text\s+"tiny_text",\s+size: :tiny$}, output
|
2016-05-31 10:15:46 -04:00
|
|
|
assert_match %r{t\.text\s+"normal_text"$}, output
|
2019-01-25 08:01:07 -05:00
|
|
|
assert_match %r{t\.text\s+"medium_text",\s+size: :medium$}, output
|
|
|
|
assert_match %r{t\.text\s+"long_text",\s+size: :long$}, output
|
|
|
|
assert_match %r{t\.binary\s+"tiny_blob_2",\s+size: :tiny$}, output
|
|
|
|
assert_match %r{t\.binary\s+"medium_blob_2",\s+size: :medium$}, output
|
|
|
|
assert_match %r{t\.binary\s+"long_blob_2",\s+size: :long$}, output
|
|
|
|
assert_match %r{t\.text\s+"tiny_text_2",\s+size: :tiny$}, output
|
|
|
|
assert_match %r{t\.text\s+"medium_text_2",\s+size: :medium$}, output
|
|
|
|
assert_match %r{t\.text\s+"long_text_2",\s+size: :long$}, output
|
2006-07-08 16:35:56 -04:00
|
|
|
end
|
2013-03-27 00:30:11 -04:00
|
|
|
|
2015-02-24 13:59:00 -05:00
|
|
|
def test_schema_does_not_include_limit_for_emulated_mysql_boolean_fields
|
2019-01-25 08:01:07 -05:00
|
|
|
output = dump_table_schema "booleans"
|
2015-02-24 13:59:00 -05:00
|
|
|
assert_no_match %r{t\.boolean\s+"has_fun",.+limit: 1}, output
|
|
|
|
end
|
|
|
|
|
2013-03-27 00:30:11 -04:00
|
|
|
def test_schema_dumps_index_type
|
2017-02-12 13:51:44 -05:00
|
|
|
output = dump_table_schema "key_tests"
|
|
|
|
assert_match %r{t\.index \["awesome"\], name: "index_key_tests_on_awesome", type: :fulltext$}, output
|
|
|
|
assert_match %r{t\.index \["pizza"\], name: "index_key_tests_on_pizza"$}, output
|
2013-03-27 00:30:11 -04:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2005-10-29 14:40:49 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_decimal_options
|
2014-09-02 22:17:27 -04:00
|
|
|
output = dump_all_table_schema([/^[^n]/])
|
2015-06-11 16:31:01 -04:00
|
|
|
assert_match %r{precision: 3,[[:space:]]+scale: 2,[[:space:]]+default: "2\.78"}, output
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2009-05-30 03:44:50 -04:00
|
|
|
|
2009-08-09 04:56:25 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2013-04-04 21:06:52 -04:00
|
|
|
def test_schema_dump_includes_bigint_default
|
2017-02-12 12:34:52 -05:00
|
|
|
output = dump_table_schema "defaults"
|
2016-03-06 16:25:54 -05:00
|
|
|
assert_match %r{t\.bigint\s+"bigint_default",\s+default: 0}, output
|
2013-04-04 21:06:52 -04:00
|
|
|
end
|
|
|
|
|
2015-03-21 18:08:55 -04:00
|
|
|
def test_schema_dump_includes_limit_on_array_type
|
2017-02-12 12:34:52 -05:00
|
|
|
output = dump_table_schema "bigint_array"
|
2016-03-06 16:25:54 -05:00
|
|
|
assert_match %r{t\.bigint\s+"big_int_data_points\",\s+array: true}, output
|
2015-03-21 18:08:55 -04:00
|
|
|
end
|
|
|
|
|
2015-06-11 18:48:23 -04:00
|
|
|
def test_schema_dump_allows_array_of_decimal_defaults
|
2017-02-12 12:34:52 -05:00
|
|
|
output = dump_table_schema "bigint_array"
|
2015-06-11 16:31:01 -04:00
|
|
|
assert_match %r{t\.decimal\s+"decimal_array_default",\s+default: \["1.23", "3.45"\],\s+array: true}, output
|
2015-06-11 18:48:23 -04:00
|
|
|
end
|
|
|
|
|
2017-02-12 12:34:52 -05:00
|
|
|
def test_schema_dump_interval_type
|
|
|
|
output = dump_table_schema "postgresql_times"
|
|
|
|
assert_match %r{t\.interval\s+"time_interval"$}, output
|
|
|
|
assert_match %r{t\.interval\s+"scaled_time_interval",\s+precision: 6$}, output
|
|
|
|
end
|
|
|
|
|
2017-02-12 13:00:48 -05:00
|
|
|
def test_schema_dump_oid_type
|
|
|
|
output = dump_table_schema "postgresql_oids"
|
|
|
|
assert_match %r{t\.oid\s+"obj_id"$}, output
|
|
|
|
end
|
|
|
|
|
2017-10-24 17:45:05 -04:00
|
|
|
def test_schema_dump_includes_extensions
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
|
2018-07-10 09:21:08 -04:00
|
|
|
connection.stub(:extensions, ["hstore"]) do
|
|
|
|
output = perform_schema_dump
|
|
|
|
assert_match "# These are extensions that must be enabled", output
|
|
|
|
assert_match %r{enable_extension "hstore"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
connection.stub(:extensions, []) do
|
|
|
|
output = perform_schema_dump
|
|
|
|
assert_no_match "# These are extensions that must be enabled", output
|
|
|
|
assert_no_match %r{enable_extension}, output
|
|
|
|
end
|
2017-10-24 17:45:05 -04:00
|
|
|
end
|
2017-07-12 13:00:49 -04:00
|
|
|
|
2017-10-24 17:45:05 -04:00
|
|
|
def test_schema_dump_includes_extensions_in_alphabetic_order
|
|
|
|
connection = ActiveRecord::Base.connection
|
2017-07-12 13:00:49 -04:00
|
|
|
|
2018-07-10 09:21:08 -04:00
|
|
|
connection.stub(:extensions, ["hstore", "uuid-ossp", "xml2"]) do
|
|
|
|
output = perform_schema_dump
|
|
|
|
enabled_extensions = output.scan(%r{enable_extension "(.+)"}).flatten
|
|
|
|
assert_equal ["hstore", "uuid-ossp", "xml2"], enabled_extensions
|
|
|
|
end
|
2017-07-12 13:00:49 -04:00
|
|
|
|
2018-07-10 09:21:08 -04:00
|
|
|
connection.stub(:extensions, ["uuid-ossp", "xml2", "hstore"]) do
|
|
|
|
output = perform_schema_dump
|
|
|
|
enabled_extensions = output.scan(%r{enable_extension "(.+)"}).flatten
|
|
|
|
assert_equal ["hstore", "uuid-ossp", "xml2"], enabled_extensions
|
|
|
|
end
|
2013-02-06 18:48:08 -05:00
|
|
|
end
|
2009-08-09 04:56:25 -04:00
|
|
|
end
|
|
|
|
|
2009-05-30 03:44:50 -04:00
|
|
|
def test_schema_dump_keeps_large_precision_integer_columns_as_decimal
|
|
|
|
output = standard_dump
|
2009-06-05 11:43:11 -04:00
|
|
|
# Oracle supports precision up to 38 and it identifies decimals with scale 0 as integers
|
|
|
|
if current_adapter?(:OracleAdapter)
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.integer\s+"atoms_in_universe",\s+precision: 38}, output
|
2014-11-22 22:02:15 -05:00
|
|
|
elsif current_adapter?(:FbAdapter)
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.integer\s+"atoms_in_universe",\s+precision: 18}, output
|
2009-06-05 11:43:11 -04:00
|
|
|
else
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.decimal\s+"atoms_in_universe",\s+precision: 55}, output
|
2009-06-05 11:43:11 -04:00
|
|
|
end
|
2009-05-30 03:44:50 -04:00
|
|
|
end
|
2009-08-08 18:29:20 -04:00
|
|
|
|
|
|
|
def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
|
|
|
|
output = standard_dump
|
|
|
|
match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n})
|
|
|
|
assert_not_nil(match, "goofy_string_id table not found")
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r(id: false), match[1], "no table id not preserved"
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.string\s+"id",.*?null: false$}, match[2], "non-primary key id column not preserved"
|
2009-08-08 18:29:20 -04:00
|
|
|
end
|
2011-10-31 09:53:47 -04:00
|
|
|
|
|
|
|
def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
|
|
|
|
output = standard_dump
|
2017-06-05 22:56:12 -04:00
|
|
|
assert_match %r{create_table "string_key_objects", id: false}, output
|
2011-10-31 09:53:47 -04:00
|
|
|
end
|
2012-01-09 13:09:45 -05:00
|
|
|
|
2014-06-11 05:35:26 -04:00
|
|
|
if ActiveRecord::Base.connection.supports_foreign_keys?
|
|
|
|
def test_foreign_keys_are_dumped_at_the_bottom_to_circumvent_dependency_issues
|
|
|
|
output = standard_dump
|
|
|
|
assert_match(/^\s+add_foreign_key "fk_test_has_fk"[^\n]+\n\s+add_foreign_key "lessons_students"/, output)
|
|
|
|
end
|
2014-09-17 05:43:42 -04:00
|
|
|
|
|
|
|
def test_do_not_dump_foreign_keys_for_ignored_tables
|
|
|
|
output = dump_table_schema "authors"
|
|
|
|
assert_equal ["authors"], output.scan(/^\s*add_foreign_key "([^"]+)".+$/).flatten
|
|
|
|
end
|
2014-06-11 05:35:26 -04:00
|
|
|
end
|
|
|
|
|
2015-12-05 15:14:22 -05:00
|
|
|
class CreateDogMigration < ActiveRecord::Migration::Current
|
2012-01-09 13:09:45 -05:00
|
|
|
def up
|
2014-06-10 07:28:38 -04:00
|
|
|
create_table("dog_owners") do |t|
|
|
|
|
end
|
|
|
|
|
2012-01-09 13:09:45 -05:00
|
|
|
create_table("dogs") do |t|
|
|
|
|
t.column :name, :string
|
2016-12-11 17:42:46 -05:00
|
|
|
t.references :owner
|
2016-04-18 18:32:03 -04:00
|
|
|
t.index [:name]
|
2016-05-11 18:47:28 -04:00
|
|
|
t.foreign_key :dog_owners, column: "owner_id"
|
2012-01-09 13:09:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
def down
|
|
|
|
drop_table("dogs")
|
2014-06-10 07:28:38 -04:00
|
|
|
drop_table("dog_owners")
|
2012-01-09 13:09:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_with_table_name_prefix_and_suffix
|
|
|
|
original, $stdout = $stdout, StringIO.new
|
2016-08-06 12:26:20 -04:00
|
|
|
ActiveRecord::Base.table_name_prefix = "foo_"
|
|
|
|
ActiveRecord::Base.table_name_suffix = "_bar"
|
2012-01-09 13:09:45 -05:00
|
|
|
|
|
|
|
migration = CreateDogMigration.new
|
|
|
|
migration.migrate(:up)
|
|
|
|
|
2014-09-10 18:32:29 -04:00
|
|
|
output = perform_schema_dump
|
2012-01-09 13:09:45 -05:00
|
|
|
assert_no_match %r{create_table "foo_.+_bar"}, output
|
2014-06-10 07:36:17 -04:00
|
|
|
assert_no_match %r{add_index "foo_.+_bar"}, output
|
2012-01-09 13:09:45 -05:00
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
2016-01-11 20:51:54 -05:00
|
|
|
assert_no_match %r{create_table "ar_internal_metadata"}, output
|
2014-06-10 07:28:38 -04:00
|
|
|
|
|
|
|
if ActiveRecord::Base.connection.supports_foreign_keys?
|
|
|
|
assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
|
|
|
|
assert_no_match %r{add_foreign_key "[^"]+", "foo_.+_bar"}, output
|
|
|
|
end
|
2012-01-09 13:09:45 -05:00
|
|
|
ensure
|
|
|
|
migration.migrate(:down)
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ""
|
Allow `table_name_prefix` and `table_name_suffix` have `$`
MySQL 5.7 and PostgreSQL 9.6 allow table identifiers have the dollar sign.
* MySQL 5.7
https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
> Permitted characters in unquoted identifiers:
> ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
* PostgreSQL 9.6
https://www.postgresql.org/docs/9.6/static/sql-syntax-lexical.html
> SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($). Note that dollar signs are not allowed in identifiers according to the letter of the SQL standard, so their use might render applications less portable. The SQL standard will not define a key word that contains digits or starts or ends with an underscore, so identifiers of this form are safe against possible conflict with future extensions of the standard.
Address #30044
[Yasuo Honda & Ryuta Kamizono]
2017-08-02 11:19:15 -04:00
|
|
|
$stdout = original
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
|
|
|
|
original, $stdout = $stdout, StringIO.new
|
|
|
|
ActiveRecord::Base.table_name_prefix = "foo$"
|
|
|
|
ActiveRecord::Base.table_name_suffix = "$bar"
|
|
|
|
|
|
|
|
migration = CreateDogMigration.new
|
|
|
|
migration.migrate(:up)
|
|
|
|
|
|
|
|
output = perform_schema_dump
|
|
|
|
assert_no_match %r{create_table "foo\$.+\$bar"}, output
|
|
|
|
assert_no_match %r{add_index "foo\$.+\$bar"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
|
|
|
assert_no_match %r{create_table "ar_internal_metadata"}, output
|
|
|
|
|
|
|
|
if ActiveRecord::Base.connection.supports_foreign_keys?
|
|
|
|
assert_no_match %r{add_foreign_key "foo\$.+\$bar"}, output
|
|
|
|
assert_no_match %r{add_foreign_key "[^"]+", "foo\$.+\$bar"}, output
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
migration.migrate(:down)
|
|
|
|
|
|
|
|
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ""
|
2012-01-09 13:09:45 -05:00
|
|
|
$stdout = original
|
|
|
|
end
|
2015-11-11 19:18:42 -05:00
|
|
|
|
|
|
|
def test_schema_dump_with_table_name_prefix_and_ignoring_tables
|
|
|
|
original, $stdout = $stdout, StringIO.new
|
|
|
|
|
2015-12-05 15:14:22 -05:00
|
|
|
create_cat_migration = Class.new(ActiveRecord::Migration::Current) do
|
2015-11-17 06:17:22 -05:00
|
|
|
def change
|
|
|
|
create_table("cats") do |t|
|
|
|
|
end
|
|
|
|
create_table("omg_cats") do |t|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-11 19:18:42 -05:00
|
|
|
original_table_name_prefix = ActiveRecord::Base.table_name_prefix
|
|
|
|
original_schema_dumper_ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
|
2016-08-06 12:26:20 -04:00
|
|
|
ActiveRecord::Base.table_name_prefix = "omg_"
|
2015-11-11 19:18:42 -05:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = ["cats"]
|
2015-11-17 06:17:22 -05:00
|
|
|
migration = create_cat_migration.new
|
2015-11-11 19:18:42 -05:00
|
|
|
migration.migrate(:up)
|
|
|
|
|
|
|
|
stream = StringIO.new
|
|
|
|
output = ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream).string
|
|
|
|
|
2015-11-17 06:17:22 -05:00
|
|
|
assert_match %r{create_table "omg_cats"}, output
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_no_match %r{create_table "cats"}, output
|
2015-11-11 19:18:42 -05:00
|
|
|
ensure
|
|
|
|
migration.migrate(:down)
|
|
|
|
ActiveRecord::Base.table_name_prefix = original_table_name_prefix
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = original_schema_dumper_ignore_tables
|
|
|
|
|
|
|
|
$stdout = original
|
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2014-06-05 09:06:09 -04:00
|
|
|
|
|
|
|
class SchemaDumperDefaultsTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
Use `inspect` in `type_cast_for_schema` for date/time and decimal values
Currently dumping defaults on schema is inconsistent.
Before:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: '2014-06-05'
t.datetime "datetime_with_default", default: '2014-06-05 07:17:04'
t.time "time_with_default", default: '2000-01-01 07:17:04'
t.decimal "decimal_with_default", default: 1234567890
end
```
After:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: "2014-06-05"
t.datetime "datetime_with_default", default: "2014-06-05 07:17:04"
t.time "time_with_default", default: "2000-01-01 07:17:04"
t.decimal "decimal_with_default", default: "1234567890"
end
```
2016-06-06 02:51:57 -04:00
|
|
|
@connection.create_table :dump_defaults, force: true do |t|
|
2014-06-05 09:06:09 -04:00
|
|
|
t.string :string_with_default, default: "Hello!"
|
2016-08-06 12:26:20 -04:00
|
|
|
t.date :date_with_default, default: "2014-06-05"
|
2014-06-05 09:06:09 -04:00
|
|
|
t.datetime :datetime_with_default, default: "2014-06-05 07:17:04"
|
|
|
|
t.time :time_with_default, default: "07:17:04"
|
Use `inspect` in `type_cast_for_schema` for date/time and decimal values
Currently dumping defaults on schema is inconsistent.
Before:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: '2014-06-05'
t.datetime "datetime_with_default", default: '2014-06-05 07:17:04'
t.time "time_with_default", default: '2000-01-01 07:17:04'
t.decimal "decimal_with_default", default: 1234567890
end
```
After:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: "2014-06-05"
t.datetime "datetime_with_default", default: "2014-06-05 07:17:04"
t.time "time_with_default", default: "2000-01-01 07:17:04"
t.decimal "decimal_with_default", default: "1234567890"
end
```
2016-06-06 02:51:57 -04:00
|
|
|
t.decimal :decimal_with_default, default: "1234567890.0123456789", precision: 20, scale: 10
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|
2016-04-15 18:27:45 -04:00
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
@connection.create_table :infinity_defaults, force: true do |t|
|
|
|
|
t.float :float_with_inf_default, default: Float::INFINITY
|
|
|
|
t.float :float_with_nan_default, default: Float::NAN
|
|
|
|
end
|
|
|
|
end
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
Use `inspect` in `type_cast_for_schema` for date/time and decimal values
Currently dumping defaults on schema is inconsistent.
Before:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: '2014-06-05'
t.datetime "datetime_with_default", default: '2014-06-05 07:17:04'
t.time "time_with_default", default: '2000-01-01 07:17:04'
t.decimal "decimal_with_default", default: 1234567890
end
```
After:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: "2014-06-05"
t.datetime "datetime_with_default", default: "2014-06-05 07:17:04"
t.time "time_with_default", default: "2000-01-01 07:17:04"
t.decimal "decimal_with_default", default: "1234567890"
end
```
2016-06-06 02:51:57 -04:00
|
|
|
@connection.drop_table "dump_defaults", if_exists: true
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_defaults_with_universally_supported_types
|
Use `inspect` in `type_cast_for_schema` for date/time and decimal values
Currently dumping defaults on schema is inconsistent.
Before:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: '2014-06-05'
t.datetime "datetime_with_default", default: '2014-06-05 07:17:04'
t.time "time_with_default", default: '2000-01-01 07:17:04'
t.decimal "decimal_with_default", default: 1234567890
end
```
After:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: "2014-06-05"
t.datetime "datetime_with_default", default: "2014-06-05 07:17:04"
t.time "time_with_default", default: "2000-01-01 07:17:04"
t.decimal "decimal_with_default", default: "1234567890"
end
```
2016-06-06 02:51:57 -04:00
|
|
|
output = dump_table_schema("dump_defaults")
|
2014-06-05 09:06:09 -04:00
|
|
|
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_match %r{t\.string\s+"string_with_default",.*?default: "Hello!"}, output
|
Use `inspect` in `type_cast_for_schema` for date/time and decimal values
Currently dumping defaults on schema is inconsistent.
Before:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: '2014-06-05'
t.datetime "datetime_with_default", default: '2014-06-05 07:17:04'
t.time "time_with_default", default: '2000-01-01 07:17:04'
t.decimal "decimal_with_default", default: 1234567890
end
```
After:
```ruby
create_table "defaults", force: :cascade do |t|
t.string "string_with_default", default: "Hello!"
t.date "date_with_default", default: "2014-06-05"
t.datetime "datetime_with_default", default: "2014-06-05 07:17:04"
t.time "time_with_default", default: "2000-01-01 07:17:04"
t.decimal "decimal_with_default", default: "1234567890"
end
```
2016-06-06 02:51:57 -04:00
|
|
|
assert_match %r{t\.date\s+"date_with_default",\s+default: "2014-06-05"}, output
|
|
|
|
assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: "2014-06-05 07:17:04"}, output
|
|
|
|
assert_match %r{t\.time\s+"time_with_default",\s+default: "2000-01-01 07:17:04"}, output
|
|
|
|
assert_match %r{t\.decimal\s+"decimal_with_default",\s+precision: 20,\s+scale: 10,\s+default: "1234567890.0123456789"}, output
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|
2016-04-15 18:27:45 -04:00
|
|
|
|
|
|
|
def test_schema_dump_with_float_column_infinity_default
|
|
|
|
skip unless current_adapter?(:PostgreSQLAdapter)
|
2016-10-28 23:05:58 -04:00
|
|
|
output = dump_table_schema("infinity_defaults")
|
2016-04-15 18:27:45 -04:00
|
|
|
assert_match %r{t\.float\s+"float_with_inf_default",\s+default: ::Float::INFINITY}, output
|
|
|
|
assert_match %r{t\.float\s+"float_with_nan_default",\s+default: ::Float::NAN}, output
|
|
|
|
end
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|