2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2014-06-05 09:06:09 -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
|
|
|
|
|
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|
|
2012-12-09 17:52:28 -05: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)
|
2014-08-15 07:10:42 -04:00
|
|
|
ensure
|
|
|
|
ActiveRecord::SchemaMigration.delete_all
|
2012-01-13 14:04:13 -05:00
|
|
|
end
|
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_magic_comment
|
2014-09-10 18:32:29 -04:00
|
|
|
assert_match "# encoding: #{Encoding.default_external.name}", standard_dump
|
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_schema_dump
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
|
|
|
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"
|
|
|
|
assert_match %r{create_table "authors", force: :cascade}, output
|
|
|
|
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
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def assert_line_up(lines, pattern, required = false)
|
|
|
|
return assert(true) if lines.empty?
|
|
|
|
matches = lines.map { |line| line.match(pattern) }
|
|
|
|
assert matches.all? if required
|
|
|
|
matches.compact!
|
|
|
|
return assert(true) if matches.empty?
|
|
|
|
assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
|
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def column_definition_lines(output = standard_dump)
|
|
|
|
output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
|
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_types_line_up
|
|
|
|
column_definition_lines.each do |column_set|
|
|
|
|
next if column_set.empty?
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
lengths = column_set.map do |column|
|
2015-01-18 17:29:32 -05:00
|
|
|
if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean|xml|uuid|point)\s+"/)
|
2008-05-06 19:08:23 -04:00
|
|
|
match[0].length
|
2007-05-11 17:26:53 -04:00
|
|
|
end
|
2015-01-18 17:29:32 -05:00
|
|
|
end.compact
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
assert_equal 1, lengths.uniq.length
|
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_arguments_line_up
|
|
|
|
column_definition_lines.each do |column_set|
|
2012-08-19 18:02:34 -04:00
|
|
|
assert_line_up(column_set, /default: /)
|
|
|
|
assert_line_up(column_set, /limit: /)
|
|
|
|
assert_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
|
|
|
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_match %r{c_int_without_limit}, output
|
|
|
|
|
2008-06-14 22:55:56 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_no_match %r{c_int_without_limit.*limit:}, output
|
|
|
|
|
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
|
|
|
|
assert_match %r{c_int_3.*}, output
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_no_match %r{c_int_3.*limit:}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
|
|
|
|
assert_match %r{c_int_4.*}, output
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_no_match %r{c_int_4.*limit:}, output
|
2013-01-01 17:17:37 -05:00
|
|
|
elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter)
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_match %r{c_int_without_limit.*limit: 4}, output
|
|
|
|
|
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
|
|
|
|
|
|
|
assert_match %r{c_int_4.*}, output
|
|
|
|
assert_no_match %r{c_int_4.*:limit}, output
|
2010-10-19 13:18:54 -04:00
|
|
|
elsif current_adapter?(:SQLite3Adapter)
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_no_match %r{c_int_without_limit.*limit:}, output
|
|
|
|
|
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
|
|
|
|
|
2010-10-19 13:18:54 -04:00
|
|
|
if current_adapter?(:SQLite3Adapter)
|
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
|
2009-03-22 18:20:42 -04:00
|
|
|
elsif current_adapter?(: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
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{c_int_5.*limit: 8}, output
|
|
|
|
assert_match %r{c_int_6.*limit: 8}, output
|
|
|
|
assert_match %r{c_int_7.*limit: 8}, output
|
|
|
|
assert_match %r{c_int_8.*limit: 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
|
2014-09-02 22:17:27 -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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
index_definition = standard_dump.split(/\n/).grep(/add_index.*companies/).first.strip
|
2014-03-10 10:09:39 -04:00
|
|
|
if current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)
|
2013-03-30 14:53:49 -04:00
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type", "rating"], name: "company_index", using: :btree', index_definition
|
|
|
|
else
|
|
|
|
assert_equal 'add_index "companies", ["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
|
|
|
|
index_definition = standard_dump.split(/\n/).grep(/add_index.*company_partial_index/).first.strip
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
2013-03-27 22:38:53 -04:00
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "(rating > 10)", using: :btree', index_definition
|
2014-03-10 10:09:39 -04:00
|
|
|
elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter)
|
2013-03-27 22:38:53 -04:00
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", using: :btree', index_definition
|
2013-12-17 12:10:23 -05:00
|
|
|
elsif current_adapter?(:SQLite3Adapter) && ActiveRecord::Base.connection.supports_partial_index?
|
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "rating > 10"', index_definition
|
2012-02-09 00:28:11 -05:00
|
|
|
else
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index"', index_definition
|
2012-02-09 00:28:11 -05:00
|
|
|
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
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{t\.boolean\s+"has_fun",.+default: false}, output
|
|
|
|
end
|
|
|
|
|
2013-01-01 17:17:37 -05:00
|
|
|
if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
|
2014-09-10 17:03:34 -04:00
|
|
|
def test_schema_dump_should_add_default_value_for_mysql_text_field
|
2008-05-06 19:08:23 -04:00
|
|
|
output = standard_dump
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.text\s+"body",\s+limit: 65535,\s+null: false$}, output
|
2005-12-24 11:55:55 -05:00
|
|
|
end
|
2006-07-08 16:35:56 -04:00
|
|
|
|
2012-10-16 05:53:18 -04:00
|
|
|
def test_schema_dump_includes_length_for_mysql_binary_fields
|
|
|
|
output = standard_dump
|
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
|
|
|
|
output = standard_dump
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.binary\s+"tiny_blob",\s+limit: 255$}, output
|
|
|
|
assert_match %r{t\.binary\s+"normal_blob",\s+limit: 65535$}, output
|
|
|
|
assert_match %r{t\.binary\s+"medium_blob",\s+limit: 16777215$}, output
|
|
|
|
assert_match %r{t\.binary\s+"long_blob",\s+limit: 4294967295$}, output
|
|
|
|
assert_match %r{t\.text\s+"tiny_text",\s+limit: 255$}, output
|
|
|
|
assert_match %r{t\.text\s+"normal_text",\s+limit: 65535$}, output
|
|
|
|
assert_match %r{t\.text\s+"medium_text",\s+limit: 16777215$}, output
|
|
|
|
assert_match %r{t\.text\s+"long_text",\s+limit: 4294967295$}, 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
|
|
|
|
output = standard_dump
|
|
|
|
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
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{add_index "key_tests", \["awesome"\], name: "index_key_tests_on_awesome", type: :fulltext}, output
|
|
|
|
assert_match %r{add_index "key_tests", \["pizza"\], name: "index_key_tests_on_pizza", using: :btree}, output
|
|
|
|
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-01-18 15:59:56 -05: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
|
|
|
|
output = standard_dump
|
2015-01-18 15:59:56 -05:00
|
|
|
assert_match %r{t\.integer\s+"bigint_default",\s+limit: 8,\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
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{t\.integer\s+"big_int_data_points\",\s+limit: 8,\s+array: true}, output
|
|
|
|
end
|
|
|
|
|
2013-11-08 10:57:51 -05:00
|
|
|
if ActiveRecord::Base.connection.supports_extensions?
|
|
|
|
def test_schema_dump_includes_extensions
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
connection.stubs(:extensions).returns(['hstore'])
|
2014-09-10 18:32:29 -04:00
|
|
|
output = perform_schema_dump
|
2013-11-08 10:57:51 -05:00
|
|
|
assert_match "# These are extensions that must be enabled", output
|
|
|
|
assert_match %r{enable_extension "hstore"}, output
|
|
|
|
|
|
|
|
connection.stubs(:extensions).returns([])
|
2014-09-10 18:32:29 -04:00
|
|
|
output = perform_schema_dump
|
2013-11-08 10:57:51 -05:00
|
|
|
assert_no_match "# These are extensions that must be enabled", output
|
|
|
|
assert_no_match %r{enable_extension}, output
|
|
|
|
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
|
2012-09-07 14:27:08 -04:00
|
|
|
assert_match %r{create_table "subscribers", 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
|
|
|
|
|
2012-01-09 13:09:45 -05:00
|
|
|
class CreateDogMigration < ActiveRecord::Migration
|
|
|
|
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
|
2014-06-10 07:28:38 -04:00
|
|
|
t.column :owner_id, :integer
|
2012-01-09 13:09:45 -05:00
|
|
|
end
|
|
|
|
add_index "dogs", [:name]
|
2014-06-10 07:28:38 -04:00
|
|
|
add_foreign_key :dogs, :dog_owners, column: "owner_id" if supports_foreign_keys?
|
2012-01-09 13:09:45 -05:00
|
|
|
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
|
|
|
|
ActiveRecord::Base.table_name_prefix = 'foo_'
|
|
|
|
ActiveRecord::Base.table_name_suffix = '_bar'
|
|
|
|
|
|
|
|
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
|
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)
|
|
|
|
|
|
|
|
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
|
|
|
|
$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
|
|
|
|
@connection.create_table :defaults, force: true 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: "07:17:04"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
return unless @connection
|
2015-02-18 06:47:01 -05:00
|
|
|
@connection.drop_table 'defaults', if_exists: true
|
2014-06-05 09:06:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_defaults_with_universally_supported_types
|
|
|
|
output = dump_table_schema('defaults')
|
|
|
|
|
2014-09-10 17:22:41 -04:00
|
|
|
assert_match %r{t\.string\s+"string_with_default",.*?default: "Hello!"}, output
|
2014-06-05 09:06:09 -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
|
|
|
|
end
|
|
|
|
end
|