Skip to content

Commit ad42aae

Browse files
committed
Fixed automatic maintaining test schema to properly handle sql structure schema format.
Additionally: * It changes `purge` task on `sqlite3` adapter to recreate database file, to be consistent with other adapters. * Adds `purge` step when loading from `schema.rb`
1 parent df6dc1b commit ad42aae

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fixed automatic maintaining test schema to properly handle sql structure
2+
schema format. Fixes #15394.
3+
4+
*Wojciech Wnętrzak*
5+
16
* PostgreSQL support default values for enum types. Fixes #7814.
27

38
*Yves Senn*

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ def load_schema(format = ActiveRecord::Base.schema_format, file = nil)
161161
when :ruby
162162
file ||= File.join(db_dir, "schema.rb")
163163
check_schema_file(file)
164+
purge(current_config)
164165
load(file)
165166
when :sql
166167
file ||= File.join(db_dir, "structure.sql")
167168
check_schema_file(file)
169+
purge(current_config)
168170
structure_load(current_config, file)
169171
else
170172
raise ArgumentError, "unknown format #{format.inspect}"

activerecord/lib/active_record/tasks/sqlite_database_tasks.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def drop
2121

2222
FileUtils.rm(file) if File.exist?(file)
2323
end
24-
alias :purge :drop
24+
25+
def purge
26+
drop
27+
create
28+
end
2529

2630
def charset
2731
connection.encoding

railties/test/application/test_test.rb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ class UserTest < ActiveSupport::TestCase
125125

126126
assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"
127127

128-
app_file 'db/structure.sql', <<-RUBY
128+
app_file 'db/structure.sql', <<-SQL
129129
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
130130
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
131131
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
132132
INSERT INTO schema_migrations (version) VALUES ('#{version}');
133-
RUBY
133+
SQL
134134

135135
app_file 'config/initializers/disable_maintain_test_schema.rb', <<-RUBY
136136
Rails.application.config.active_record.maintain_test_schema = false
@@ -143,6 +143,56 @@ class UserTest < ActiveSupport::TestCase
143143
assert_successful_test_run('models/user_test.rb')
144144
end
145145

146+
test "sql structure migrations when adding column to existing table" do
147+
output_1 = script('generate model user name:string')
148+
version_1 = output_1.match(/(\d+)_create_users\.rb/)[1]
149+
150+
app_file 'test/models/user_test.rb', <<-RUBY
151+
require 'test_helper'
152+
class UserTest < ActiveSupport::TestCase
153+
test "user" do
154+
User.create! name: "Jon"
155+
end
156+
end
157+
RUBY
158+
159+
app_file 'config/initializers/enable_sql_schema_format.rb', <<-RUBY
160+
Rails.application.config.active_record.schema_format = :sql
161+
RUBY
162+
163+
app_file 'db/structure.sql', <<-SQL
164+
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
165+
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
166+
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
167+
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
168+
SQL
169+
170+
assert_successful_test_run('models/user_test.rb')
171+
172+
output_2 = script('generate migration add_email_to_users')
173+
version_2 = output_2.match(/(\d+)_add_email_to_users\.rb/)[1]
174+
175+
app_file 'test/models/user_test.rb', <<-RUBY
176+
require 'test_helper'
177+
178+
class UserTest < ActiveSupport::TestCase
179+
test "user" do
180+
User.create! name: "Jon", email: "jon@doe.com"
181+
end
182+
end
183+
RUBY
184+
185+
app_file 'db/structure.sql', <<-SQL
186+
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
187+
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
188+
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255));
189+
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
190+
INSERT INTO schema_migrations (version) VALUES ('#{version_2}');
191+
SQL
192+
193+
assert_successful_test_run('models/user_test.rb')
194+
end
195+
146196
private
147197
def assert_unsuccessful_run(name, message)
148198
result = run_test_file(name)

0 commit comments

Comments
 (0)