Skip to content

Commit

Permalink
Merge pull request #321 from jgmontoya/fix/db-prepare-withdata-task
Browse files Browse the repository at this point in the history
fix(db:prepare:with_data): add missing prepare_all_with_data and task definition
  • Loading branch information
ilyakatz committed Aug 14, 2024
2 parents 2bf9a52 + a0c4978 commit 81886e1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 9.4.2
- Fix db:prepare:with_data task

## 9.4.1
- Add db:prepare task

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
data_migrate (9.4.1)
data_migrate (9.4.2)
activerecord (>= 6.1)
railties (>= 6.1)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
data_migrate (9.4.1)
data_migrate (9.4.2)
activerecord (>= 6.1)
railties (>= 6.1)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
data_migrate (9.4.1)
data_migrate (9.4.2)
activerecord (>= 6.1)
railties (>= 6.1)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
data_migrate (9.4.1)
data_migrate (9.4.2)
activerecord (>= 6.1)
railties (>= 6.1)

Expand Down
37 changes: 37 additions & 0 deletions lib/data_migrate/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,42 @@ def self.migrate_with_data
end
end
end

def self.prepare_all_with_data
seed = false

each_current_configuration(env) do |db_config|
next unless db_config.primary?

with_temporary_pool(db_config) do
begin
database_initialized = migration_connection.schema_migration.table_exists?
rescue ActiveRecord::NoDatabaseError
create(db_config)
retry
end

unless database_initialized
if File.exist?(schema_dump_path(db_config))
load_schema(db_config, ActiveRecord.schema_format, nil)
load_schema_current(
:ruby,
ENV["DATA_SCHEMA"]
)
end

seed = true
end

migrate_with_data
if ActiveRecord.dump_schema_after_migration
dump_schema(db_config)
DataMigrate::Tasks::DataMigrateTasks.dump
end
end
end

load_seed if seed
end
end
end
2 changes: 1 addition & 1 deletion lib/data_migrate/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DataMigrate
VERSION = "9.4.1".freeze
VERSION = "9.4.2".freeze
end
7 changes: 7 additions & 0 deletions tasks/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ namespace :db do
end
end
end

namespace :prepare do
desc "Runs setup if database does not exist, or runs data and schema migrations if it does"
task with_data: :environment do
DataMigrate::DatabaseTasks.prepare_all_with_data
end
end
end

namespace :data do
Expand Down

0 comments on commit 81886e1

Please sign in to comment.