aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/rake/dbs_test.rb
blob: 9a36d1ca01652e8d1ae5c54ad57e5ea04b42675b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require "isolation/abstract_unit"

module ApplicationTests
  module RakeTests
    class RakeDbsTest < ActiveSupport::TestCase
      include ActiveSupport::Testing::Isolation

      def setup
        build_app
        boot_rails
        FileUtils.rm_rf("#{app_path}/config/environments")
      end

      def teardown
        teardown_app
      end

      def database_url_db_name
        File.join(app_path, "db/database_url_db.sqlite3")
      end

      def set_database_url
        ENV['DATABASE_URL'] = File.join("sqlite3://:@localhost", database_url_db_name)
        # ensure it's using the DATABASE_URL
        FileUtils.rm_rf("#{app_path}/config/database.yml")
      end

      def expected
        @expected ||= {}
      end

      def db_create_and_drop
        Dir.chdir(app_path) do
          output = `bundle exec rake db:create`
          assert_equal output, ""
          assert File.exist?(expected[:database])
          assert_equal expected[:database],
                        ActiveRecord::Base.connection_config[:database]
          output = `bundle exec rake db:drop`
          assert_equal output, ""
          assert !File.exist?(expected[:database])
        end
      end

      test 'db:create and db:drop without database url' do
        require "#{app_path}/config/environment"
        expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
        db_create_and_drop
       end

      test 'db:create and db:drop with database url' do
        require "#{app_path}/config/environment"
        set_database_url
        expected[:database] = database_url_db_name
        db_create_and_drop
      end

      def db_migrate_and_status
        Dir.chdir(app_path) do
          `rails generate model book title:string;
           bundle exec rake db:migrate`
          output = `bundle exec rake db:migrate:status`
          assert_match(%r{database:\s+\S*#{Regexp.escape(expected[:database])}}, output)
          assert_match(/up\s+\d{14}\s+Create books/, output)
        end
      end

      test 'db:migrate and db:migrate:status without database_url' do
        require "#{app_path}/config/environment"
        expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
        db_migrate_and_status
      end

      test 'db:migrate and db:migrate:status with database_url' do
        require "#{app_path}/config/environment"
        set_database_url
        expected[:database] = database_url_db_name
        db_migrate_and_status
      end

      def db_schema_dump
        Dir.chdir(app_path) do
          `rails generate model book title:string;
           rake db:migrate db:schema:dump`
          schema_dump = File.read("db/schema.rb")
          assert_match(/create_table \"books\"/, schema_dump)
        end
      end

      test 'db:schema:dump without database_url' do
        db_schema_dump
      end

      test 'db:schema:dump with database_url' do
        set_database_url
        db_schema_dump
      end

      def db_fixtures_load
        Dir.chdir(app_path) do
          `rails generate model book title:string;
           bundle exec rake db:migrate db:fixtures:load`
          assert_match(/#{expected[:database]}/,
                    ActiveRecord::Base.connection_config[:database])
          require "#{app_path}/app/models/book"
          assert_equal 2, Book.count
        end
      end

      test 'db:fixtures:load without database_url' do
        require "#{app_path}/config/environment"
        expected[:database] =  ActiveRecord::Base.configurations[Rails.env]['database']
        db_fixtures_load
      end

      test 'db:fixtures:load with database_url' do
        require "#{app_path}/config/environment"
        set_database_url
        expected[:database] = database_url_db_name
        db_fixtures_load
      end

      def db_structure_dump_and_load
        Dir.chdir(app_path) do
          `rails generate model book title:string;
           bundle exec rake db:migrate db:structure:dump`
          structure_dump = File.read("db/structure.sql")
          assert_match(/CREATE TABLE \"books\"/, structure_dump)
          `bundle exec rake db:drop db:structure:load`
          assert_match(/#{expected[:database]}/,
                        ActiveRecord::Base.connection_config[:database])
          require "#{app_path}/app/models/book"
          #if structure is not loaded correctly, exception would be raised
          assert Book.count, 0
        end
      end

      test 'db:structure:dump and db:structure:load without database_url' do
        require "#{app_path}/config/environment"
        expected[:database] =  ActiveRecord::Base.configurations[Rails.env]['database']
        db_structure_dump_and_load
      end

      test 'db:structure:dump and db:structure:load with database_url' do
        require "#{app_path}/config/environment"
        set_database_url
        expected[:database] = database_url_db_name
        db_structure_dump_and_load
      end

      def db_test_load_structure
        Dir.chdir(app_path) do
          `rails generate model book title:string;
           bundle exec rake db:migrate db:structure:dump db:test:load_structure`
          ActiveRecord::Base.configurations = Rails.application.config.database_configuration
          ActiveRecord::Base.establish_connection :test
          require "#{app_path}/app/models/book"
          #if structure is not loaded correctly, exception would be raised
          assert Book.count, 0
          assert_match(/#{ActiveRecord::Base.configurations['test']['database']}/,
                        ActiveRecord::Base.connection_config[:database])
        end
      end

      test 'db:test:load_structure without database_url' do
        require "#{app_path}/config/environment"
        db_test_load_structure
      end
    end
  end
end