Skip to content

Commit 86085aa

Browse files
authored
Merge pull request #1418 from jeffcarbs/pending-migrations
Only run pending migrations check if necessary
2 parents be04e90 + 9139ce6 commit 86085aa

File tree

3 files changed

+121
-55
lines changed

3 files changed

+121
-55
lines changed

lib/tapioca/dsl/pipeline.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def run(&blk)
6868
ERROR
6969
end
7070

71+
if defined?(::ActiveRecord::Base) && constants_to_process.any? { |c| ::ActiveRecord::Base > c }
72+
abort_if_pending_migrations!
73+
end
74+
7175
result = Executor.new(
7276
constants_to_process,
7377
number_of_workers: @number_of_workers,
@@ -184,6 +188,16 @@ def report_error(error)
184188
handler.call(error)
185189
exit(1)
186190
end
191+
192+
sig { void }
193+
def abort_if_pending_migrations!
194+
return unless defined?(::Rake)
195+
196+
Rails.application.load_tasks
197+
if Rake::Task.task_defined?("db:abort_if_pending_migrations")
198+
Rake::Task["db:abort_if_pending_migrations"].invoke
199+
end
200+
end
187201
end
188202
end
189203
end

lib/tapioca/loaders/dsl.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def load_application(tapioca_path:, eager_load: true, app_root: ".")
2020
def load
2121
load_dsl_extensions
2222
load_application
23-
abort_if_pending_migrations!
2423
load_dsl_compilers
2524
end
2625

@@ -70,17 +69,6 @@ def load_application
7069

7170
say("Done", :green)
7271
end
73-
74-
sig { void }
75-
def abort_if_pending_migrations!
76-
return unless File.exist?("#{@app_root}/config/application.rb")
77-
return unless defined?(::Rake)
78-
79-
Rails.application.load_tasks
80-
if Rake::Task.task_defined?("db:abort_if_pending_migrations")
81-
Rake::Task["db:abort_if_pending_migrations"].invoke
82-
end
83-
end
8472
end
8573
end
8674
end

spec/tapioca/cli/dsl_spec.rb

Lines changed: 107 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,64 +1212,128 @@ def body=(body); end
12121212
RBI
12131213
end
12141214

1215-
it "aborts if there are pending migrations" do
1216-
@project.require_real_gem("rake", "13.0.6")
1217-
@project.bundle_install
1215+
describe "pending migrations" do
1216+
before do
1217+
@project.write("db/migrate/202001010000_create_articles.rb", <<~RB)
1218+
class CreateArticles < ActiveRecord::Migration[6.1]
1219+
def change
1220+
create_table(:articles) do |t|
1221+
t.timestamps
1222+
end
1223+
end
1224+
end
1225+
RB
12181226

1219-
@project.write("lib/post.rb", <<~RB)
1220-
require "smart_properties"
1227+
@project.write("lib/database.rb", <<~RB)
1228+
require "rake"
12211229
1222-
class Post
1223-
include SmartProperties
1224-
property :title, accepts: String
1225-
end
1226-
RB
1230+
namespace :db do
1231+
task :abort_if_pending_migrations do
1232+
pending_migrations = Dir["\#{Kernel.__dir__}/../db/migrate/*.rb"]
12271233
1228-
@project.write("db/migrate/202001010000_create_articles.rb", <<~RB)
1229-
class CreateArticles < ActiveRecord::Migration[6.1]
1230-
def change
1231-
create_table(:articles) do |t|
1232-
t.timestamps
1234+
if pending_migrations.any?
1235+
Kernel.puts "You have \#{pending_migrations.size} pending migration:"
1236+
1237+
pending_migrations.each do |pending_migration|
1238+
name = pending_migration.split("/").last
1239+
Kernel.puts name
1240+
end
1241+
1242+
Kernel.abort(%{Run `bin/rails db:migrate` to update your database then try again.})
1243+
end
12331244
end
12341245
end
1235-
end
1236-
RB
1246+
RB
12371247

1238-
@project.write("lib/database.rb", <<~RB)
1239-
require "rake"
1248+
@project.require_real_gem("rake", "13.0.6")
1249+
@project.require_real_gem("activerecord")
1250+
@project.bundle_install
1251+
end
12401252

1241-
namespace :db do
1242-
task :abort_if_pending_migrations do
1243-
pending_migrations = Dir["\#{Kernel.__dir__}/../db/migrate/*.rb"]
1253+
it "aborts if there are pending migrations" do
1254+
@project.write("lib/post.rb", <<~RB)
1255+
class Post < ActiveRecord::Base
1256+
end
1257+
RB
12441258

1245-
if pending_migrations.any?
1246-
Kernel.puts "You have \#{pending_migrations.size} pending migration:"
1259+
result = @project.tapioca("dsl Post")
12471260

1248-
pending_migrations.each do |pending_migration|
1249-
name = pending_migration.split("/").last
1250-
Kernel.puts name
1251-
end
1261+
# FIXME: print the error to the correct stream
1262+
assert_equal(<<~OUT, result.out)
1263+
Loading Rails application... Done
1264+
Loading DSL compiler classes... Done
1265+
Compiling DSL RBI files...
12521266
1253-
Kernel.abort(%{Run `bin/rails db:migrate` to update your database then try again.})
1254-
end
1267+
You have 1 pending migration:
1268+
202001010000_create_articles.rb
1269+
OUT
1270+
1271+
assert_equal(<<~ERR, result.err)
1272+
Run `bin/rails db:migrate` to update your database then try again.
1273+
ERR
1274+
1275+
refute_success_status(result)
1276+
end
1277+
1278+
it "aborts if there are pending migrations and no arg was passed" do
1279+
@project.write("lib/post.rb", <<~RB)
1280+
class Post < ActiveRecord::Base
12551281
end
1256-
end
1257-
RB
1282+
RB
12581283

1259-
result = @project.tapioca("dsl")
1284+
result = @project.tapioca("dsl")
12601285

1261-
# FIXME: print the error to the correct stream
1262-
assert_equal(<<~OUT, result.out)
1263-
Loading Rails application... Done
1264-
You have 1 pending migration:
1265-
202001010000_create_articles.rb
1266-
OUT
1286+
# FIXME: print the error to the correct stream
1287+
assert_equal(<<~OUT, result.out)
1288+
Loading Rails application... Done
1289+
Loading DSL compiler classes... Done
1290+
Compiling DSL RBI files...
12671291
1268-
assert_equal(<<~ERR, result.err)
1269-
Run `bin/rails db:migrate` to update your database then try again.
1270-
ERR
1292+
You have 1 pending migration:
1293+
202001010000_create_articles.rb
1294+
OUT
12711295

1272-
refute_success_status(result)
1296+
assert_equal(<<~ERR, result.err)
1297+
Run `bin/rails db:migrate` to update your database then try again.
1298+
ERR
1299+
1300+
refute_success_status(result)
1301+
end
1302+
1303+
it "does not abort if there are pending migrations but no active record models" do
1304+
@project.write("lib/post.rb", <<~RB)
1305+
require "smart_properties"
1306+
1307+
class Post
1308+
include SmartProperties
1309+
property :title, accepts: String
1310+
end
1311+
RB
1312+
1313+
result = @project.tapioca("dsl Post")
1314+
1315+
assert_equal(<<~OUT, result.out)
1316+
Loading Rails application... Done
1317+
Loading DSL compiler classes... Done
1318+
Compiling DSL RBI files...
1319+
1320+
create sorbet/rbi/dsl/post.rbi
1321+
1322+
Done
1323+
1324+
Checking generated RBI files... Done
1325+
No errors found
1326+
1327+
All operations performed in working directory.
1328+
Please review changes and commit them.
1329+
OUT
1330+
1331+
assert_empty_stderr(result)
1332+
1333+
assert_project_file_exist("sorbet/rbi/dsl/post.rbi")
1334+
1335+
assert_success_status(result)
1336+
end
12731337
end
12741338

12751339
it "overwrites existing RBIs without user input" do

0 commit comments

Comments
 (0)