Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task db:prepare:with_data #274

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/data_migrate/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ def migration_connection # :nodoc:
ensure
migration_class.connection_handler.establish_connection(original_db_config)
end

def with_temporary_pool(db_config)
original_db_config = migration_class.connection_db_config
pool = migration_class.establish_connection(db_config)
yield pool
ensure
migration_class.establish_connection(original_db_config)
end

def migration_class # :nodoc:
ActiveRecord::Base
end

def migration_connection # :nodoc:
migration_class.connection
end
end

def db_configs_with_versions
Expand Down Expand Up @@ -173,5 +189,73 @@ def past_migrations(sort = nil)

sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
end

def self.migrate_with_data
DataMigrate::DataMigrator.create_data_schema_table

ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true

db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)

schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions

mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
schema_db_configs + data_db_configs
end

mapped_versions.sort.each do |version, db_configs|
db_configs.each do |db_config|
if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
db_config = db_config.db_config
end

DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
if is_data_migration
DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
else
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
end
end
end
end
end

def self.prepare_all_with_data
seed = false

each_current_configuration(env) do |db_config|
Floppy marked this conversation as resolved.
Show resolved Hide resolved
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
36 changes: 8 additions & 28 deletions tasks/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,7 @@ namespace :db do
namespace :migrate do
desc "Migrate the database data and schema (options: VERSION=x, VERBOSE=false)."
task :with_data => :load_config do
DataMigrate::DataMigrator.create_data_schema_table
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true

db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)

schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions

mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
schema_db_configs + data_db_configs
end

mapped_versions.sort.each do |version, db_configs|
db_configs.each do |db_config|
if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
db_config = db_config.db_config
end

DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
if is_data_migration
DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
else
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
end
end
end
end

DataMigrate::DatabaseTasks.migrate_with_data
Rake::Task["db:_dump"].invoke
Rake::Task["data:dump"].invoke
end
Expand Down Expand Up @@ -173,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