Skip to content

Commit 9f8df61

Browse files
authored
Use lease_connection over deprecated connection for rails 8 (#353)
* Use `lease_connection` over deprecated `connection` for rails 8 Call `register_task` as extending DatabaseTasks doesn't hold onto that * Use lease_connection over connection only when available
1 parent 01e6078 commit 9f8df61

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog
22

3-
# 11.2.0
3+
# Unreleased
4+
- Use lease_connection over deprecated connection for rails 8 https://github.com/ilyakatz/data-migrate/pull/353
45

6+
# 11.2.0
57
- Remove committed Gemfile.lock, reduce bundled file list when running `gem install` https://github.com/ilyakatz/data-migrate/pull/351
68
- [Bump actionpack from 7.1.3.4 to 7.1.4.1](https://github.com/ilyakatz/data-migrate/pull/348)
79
- [Bump rexml from 3.3.6 to 3.3.9](https://github.com/ilyakatz/data-migrate/pull/349)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ From now on capistrano will run `rake db:migrate:with_data` in every deploy.
144144
## Rails Engines support
145145

146146
This gem also has a initial support for adding data migrations inside Rails engines.
147-
Just add your engines directory pattern to data_migrations initializer, for example
147+
Just add your engines directory pattern to data_migrations initializer, for example
148148
in the case your engines are located in `engines` folder you can set it up like this:
149149

150150
```ruby
@@ -166,6 +166,7 @@ bundle exec appraisal rails-6.1 rspec
166166
bundle exec appraisal rails-7.0 rspec
167167
bundle exec appraisal rails-7.1 rspec
168168
bundle exec appraisal rails-7.2 rspec
169+
bundle exec appraisal rails-8.0 rspec
169170
```
170171

171172
## Releasing new version

lib/data_migrate/data_migrator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def migrations_status
4848

4949
# TODO: this was added to be backward compatible, need to re-evaluate
5050
def migrations(_migrations_paths)
51-
#DataMigrate::MigrationContext.new(migrations_paths).migrations
5251
DataMigrate::MigrationContext.new(_migrations_paths).migrations
5352
end
5453

lib/data_migrate/database_tasks.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ module DatabaseTasks
99
extend ActiveRecord::Tasks::DatabaseTasks
1010
extend self
1111

12+
if respond_to?(:register_task)
13+
register_task(/mysql/, "ActiveRecord::Tasks::MySQLDatabaseTasks")
14+
register_task(/trilogy/, "ActiveRecord::Tasks::MySQLDatabaseTasks")
15+
register_task(/postgresql/, "ActiveRecord::Tasks::PostgreSQLDatabaseTasks")
16+
register_task(/sqlite/, "ActiveRecord::Tasks::SQLiteDatabaseTasks")
17+
end
18+
1219
# These method are only introduced in Rails 7.1
1320
unless respond_to?(:with_temporary_pool_for_each)
1421
def with_temporary_pool_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc:
@@ -99,7 +106,7 @@ def pending_migrations
99106
end
100107

101108
def sort_migrations(*migrations)
102-
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
109+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
103110
end
104111

105112
def sort_string migration
@@ -159,7 +166,7 @@ def pending_data_migrations
159166
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
160167
sort_migrations(
161168
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
162-
)
169+
)
163170
end
164171

165172
def pending_schema_migrations
@@ -219,7 +226,8 @@ def self.prepare_all_with_data
219226
next unless primary?(db_config)
220227

221228
with_temporary_pool(db_config) do |pool|
222-
unless database_exists?(pool.connection)
229+
connection = pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection
230+
unless database_exists?(connection)
223231
create(db_config)
224232
if File.exist?(schema_dump_path(db_config))
225233
load_schema(db_config, schema_format, nil)

spec/data_migrate/database_tasks_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
before do
120120
allow(subject).to receive(:each_current_configuration).and_yield(db_config)
121121
allow(subject).to receive(:with_temporary_pool).with(db_config).and_yield(pool)
122-
allow(pool).to receive(:connection).and_return(connection)
122+
allow(pool).to receive(:lease_connection).and_return(connection)
123123
allow(subject).to receive(:schema_dump_path).and_return("db/data_schema.rb")
124124
allow(File).to receive(:exist?).and_return(true)
125125
allow(subject).to receive(:load_schema)

0 commit comments

Comments
 (0)