22require 'stringio'
33
44describe "Schema dump" do
5+ let ( :model ) { stub_model ( 'Post' ) }
56
6- before ( :all ) do
7- ActiveRecord ::Migration . suppress_messages do
8- ActiveRecord ::Schema . define do
9- connection . tables . each do |table | drop_table table , force : :cascade end
10-
7+ context "with date default" , :postgresql => :only do
8+ before do
9+ apply_migration do
1110 create_table :posts , :force => true do |t |
12- t . text :body
13- t . integer :user_id
14- t . integer :first_comment_id
15- t . string :string_no_default
16- t . integer :short_id
17- t . string :str_short
18- t . integer :integer_col
19- t . float :float_col
20- t . decimal :decimal_col
21- t . datetime :datetime_col
22- t . timestamp :timestamp_col
23- t . time :time_col
24- t . date :date_col
25- t . binary :binary_col
26- t . boolean :boolean_col
2711 end
28-
2912 end
3013 end
31- class ::Post < ActiveRecord ::Base ; end
32- end
3314
34- context "with date default" , :postgresql => :only do
3515 it "should dump the default hash expr as now()" do
36- with_additional_column Post , :posted_at , :datetime , :default => :now do
16+ with_additional_column model , :posted_at , :datetime , :default => :now do
3717 expect ( dump_posts ) . to match ( %r{t\. datetime\s +"posted_at",\s *(?:default:|:default\s *=>)\s *\{ \s *(?:expr:|:expr\s *=>)\s *"now\( \) "\s *\} \s *$} )
3818 end
3919 end
4020
4121 it "should dump the default hash expr as CURRENT_TIMESTAMP" do
42- with_additional_column Post , :posted_at , :datetime , :default => { :expr => 'date \'2001-09-28\'' } do
22+ with_additional_column model , :posted_at , :datetime , :default => { :expr => 'date \'2001-09-28\'' } do
4323 expect ( dump_posts ) . to match ( %r{t\. datetime\s +"posted_at",\s *(?:default:|:default\s *=>).*2001-09-28.*} )
4424 end
4525 end
26+ end
27+
28+ context 'with a complex expression' , postgresql : :only do
29+ before do
30+ apply_migration do
31+ create_table :posts , :force => true do |t |
32+ end
33+ end
34+ end
4635
4736 it "can dump a complex default expression" do
48- with_additional_column Post , :name , :string , :default => { :expr => 'substring(random()::text from 3 for 6)' } do
37+ with_additional_column model , :name , :string , :default => { :expr => 'substring(random()::text from 3 for 6)' } do
4938 expect ( dump_posts ) . to match ( %r{t\. string\s +"name",\s *(?:default:|:default\s *=>)\s *{\s *(?:expr:|:expr\s *=>)\s *"\\ "substring\\ "\( \( random\( \) \) ::text, 3, 6\) "\s *}} )
5039 end
5140 end
5241 end
5342
5443 context "with date default" , :sqlite3 => :only do
44+ before do
45+ apply_migration do
46+ create_table :posts , :force => true do |t |
47+ end
48+ end
49+ end
50+
5551 it "should dump the default hash expr as now" do
56- with_additional_column Post , :posted_at , :datetime , :default => :now do
52+ with_additional_column model , :posted_at , :datetime , :default => :now do
5753 expect ( dump_posts ) . to match ( %r{t\. datetime\s +"posted_at",\s *(default:|:default\s *=>)\s *\{ \s *(?:expr:|:expr\s *=>)\s *"\( DATETIME\( 'now'\) \) "\s *\} } )
5854 end
5955 end
6056
6157 it "should dump the default hash expr string as now" do
62- with_additional_column Post , :posted_at , :datetime , :default => { :expr => "(DATETIME('now'))" } do
58+ with_additional_column model , :posted_at , :datetime , :default => { :expr => "(DATETIME('now'))" } do
6359 expect ( dump_posts ) . to match ( %r{t\. datetime\s +"posted_at",\s *(default:|:default\s *=>)\s *\{ \s *(?:expr:|:expr\s *=>)\s *"\( DATETIME\( 'now'\) \) "\s *\} } )
6460 end
6561 end
6662
6763 it "should dump the default value normally" do
68- with_additional_column Post , :posted_at , :string , :default => "now" do
64+ with_additional_column model , :posted_at , :string , :default => "now" do
6965 expect ( dump_posts ) . to match ( %r{t\. string\s *"posted_at",\s *(?:default:|:default\s *=>)\s *"now"} )
7066 end
7167 end
7268 end
7369
7470 it "should leave out :default when default was changed to null" do
75- ActiveRecord ::Migration . suppress_messages do
76- ActiveRecord ::Migration . change_column_default :posts , :string_no_default , nil
71+ apply_migration do
72+ create_table :posts , :force => true do |t |
73+ t . datetime :date_column , default : { expr : :now }
74+ end
75+
76+ change_column_default :posts , :date_column , nil
7777 end
78- # mysql2 includes 'limit: 255' in the output. that's OK, just want to
79- # make sure the full line doesn't have 'default' in it.
80- expect ( dump_posts ) . to match ( %r{t\. string\s +"string_no_default"\s *(,\s *limit:\s *\d +)?$} )
78+ expect ( dump_posts ) . to match ( %r{t\. datetime\s +"date_column"$} )
8179 end
8280
8381 protected
82+
8483 def to_regexp ( string )
8584 Regexp . new ( Regexp . escape ( string ) )
8685 end
8786
8887 def with_additional_column ( model , column_name , column_type , options )
89- table_columns = model . columns . reject { |column | column . name == 'id' }
90- ActiveRecord :: Migration . suppress_messages do
91- ActiveRecord :: Migration . create_table model . table_name , :force => true do |t |
88+ table_columns = model . columns . reject { |column | column . name == 'id' }
89+ apply_migration do
90+ create_table model . table_name , :force => true do |t |
9291 table_columns . each do |column |
9392 t . column column . name , column . type , :default => column . default
9493 end
@@ -98,8 +97,8 @@ def with_additional_column(model, column_name, column_type, options)
9897 yield
9998 end
10099
101- def dump_schema ( opts = { } )
102- stream = StringIO . new
100+ def dump_schema ( opts = { } )
101+ stream = StringIO . new
103102 ActiveRecord ::SchemaDumper . ignore_tables = Array . wrap ( opts [ :ignore ] ) || [ ]
104103 ActiveRecord ::SchemaDumper . dump ( ActiveRecord ::Base . connection , stream )
105104 stream . string
@@ -110,4 +109,3 @@ def dump_posts
110109 end
111110
112111end
113-
0 commit comments