Module: DataMigrate::DatabaseTasks
- Extended by:
- ActiveRecord::Tasks::DatabaseTasks, DatabaseTasks
- Included in:
- DatabaseTasks
- Defined in:
- lib/data_migrate/database_tasks.rb
Overview
This class extends DatabaseTasks to add a schema_file method.
Class Method Summary collapse
Instance Method Summary collapse
- #check_schema_file(filename) ⇒ Object
- #data_migrations_path ⇒ Object
- #db_configs_with_versions ⇒ Object
-
#dump_filename(spec_name, format = ActiveRecord::Base.schema_format) ⇒ Object
This method is removed in Rails 7.0.
- #forward(step = 1) ⇒ Object
-
#migration_class ⇒ Object
:nodoc:.
-
#migration_connection ⇒ Object
:nodoc:.
- #past_migrations(sort = nil) ⇒ Object
- #pending_data_migrations ⇒ Object
- #pending_migrations ⇒ Object
- #pending_schema_migrations ⇒ Object
- #run_migration(migration, direction) ⇒ Object
- #schema_dump_path(db_config, format = ActiveRecord.schema_format) ⇒ Object
- #schema_file(_format = nil) ⇒ Object
- #schema_file_type(_format = nil) ⇒ Object
-
#schema_sha1(file) ⇒ Object
Override this method from ‘ActiveRecord::Tasks::DatabaseTasks` to ensure that the sha saved in ar_internal_metadata table is from the original schema.rb file.
- #sort_migrations(*migrations) ⇒ Object
- #sort_string(migration) ⇒ Object
-
#with_temporary_connection(db_config) ⇒ Object
:nodoc:.
-
#with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) ⇒ Object
:nodoc:.
Class Method Details
.migrate_with_data ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/data_migrate/database_tasks.rb', line 177 def self.migrate_with_data DataMigrate::DataMigrator.create_data_schema_table ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true schema_mapped_versions = if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_1 ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions else db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env) ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs) end 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 |
.prepare_all_with_data ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/data_migrate/database_tasks.rb', line 213 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 |
Instance Method Details
#check_schema_file(filename) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/data_migrate/database_tasks.rb', line 87 def check_schema_file(filename) unless File.exist?(filename) = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.} Kernel.abort end end |
#data_migrations_path ⇒ Object
109 110 111 |
# File 'lib/data_migrate/database_tasks.rb', line 109 def data_migrations_path ::DataMigrate.config.data_migrations_path end |
#db_configs_with_versions ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/data_migrate/database_tasks.rb', line 49 def db_configs_with_versions db_configs_with_versions = Hash.new { |h, k| h[k] = [] } with_temporary_connection_for_each do |conn| db_config = conn.pool.db_config if db_config.primary? versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] } target_version = ActiveRecord::Tasks::DatabaseTasks.target_version versions_to_run.each do |version| next if target_version && target_version != version db_configs_with_versions[version] << DatabaseConfigurationWrapper.new(db_config) end end end db_configs_with_versions end |
#dump_filename(spec_name, format = ActiveRecord::Base.schema_format) ⇒ Object
This method is removed in Rails 7.0
77 78 79 80 81 82 83 84 85 |
# File 'lib/data_migrate/database_tasks.rb', line 77 def dump_filename(spec_name, format = ActiveRecord::Base.schema_format) filename = if spec_name == "primary" schema_file_type(format) else "#{spec_name}_#{schema_file_type(format)}" end ENV["DATA_SCHEMA"] || File.join(db_dir, filename) end |
#forward(step = 1) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/data_migrate/database_tasks.rb', line 143 def forward(step = 1) DataMigrate::DataMigrator.create_data_schema_table migrations = pending_migrations.reverse.pop(step).reverse migrations.each do | pending_migration | if pending_migration[:kind] == :data ActiveRecord::Migration.write("== %s %s" % ["Data", "=" * 71]) DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version]) elsif pending_migration[:kind] == :schema ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69]) DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version]) end end end |
#migration_class ⇒ Object
:nodoc:
31 32 33 |
# File 'lib/data_migrate/database_tasks.rb', line 31 def migration_class # :nodoc: ActiveRecord::Base end |
#migration_connection ⇒ Object
:nodoc:
35 36 37 |
# File 'lib/data_migrate/database_tasks.rb', line 35 def migration_connection # :nodoc: migration_class.connection end |
#past_migrations(sort = nil) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/data_migrate/database_tasks.rb', line 169 def past_migrations(sort = nil) data_versions = DataMigrate::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : [] schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_versions migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } } sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse end |
#pending_data_migrations ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/data_migrate/database_tasks.rb', line 157 def pending_data_migrations data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path) data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations) sort_migrations( data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } } ) end |
#pending_migrations ⇒ Object
94 95 96 97 98 99 |
# File 'lib/data_migrate/database_tasks.rb', line 94 def pending_migrations sort_migrations( pending_schema_migrations, pending_data_migrations ) end |
#pending_schema_migrations ⇒ Object
165 166 167 |
# File 'lib/data_migrate/database_tasks.rb', line 165 def pending_schema_migrations ::DataMigrate::SchemaMigration.pending_schema_migrations end |
#run_migration(migration, direction) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/data_migrate/database_tasks.rb', line 113 def run_migration(migration, direction) if migration[:kind] == :data ::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71]) ::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version]) else ::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69]) ::DataMigrate::SchemaMigration.run( direction, ::DataMigrate::SchemaMigration.migrations_paths, migration[:version] ) end end |
#schema_dump_path(db_config, format = ActiveRecord.schema_format) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/data_migrate/database_tasks.rb', line 127 def schema_dump_path(db_config, format = ActiveRecord.schema_format) return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"] # We only require a schema.rb file for the primary database return unless db_config.primary? File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type) end |
#schema_file(_format = nil) ⇒ Object
68 69 70 |
# File 'lib/data_migrate/database_tasks.rb', line 68 def schema_file(_format = nil) File.join(db_dir, "data_schema.rb") end |
#schema_file_type(_format = nil) ⇒ Object
72 73 74 |
# File 'lib/data_migrate/database_tasks.rb', line 72 def schema_file_type(_format = nil) "data_schema.rb" end |
#schema_sha1(file) ⇒ Object
Override this method from ‘ActiveRecord::Tasks::DatabaseTasks` to ensure that the sha saved in ar_internal_metadata table is from the original schema.rb file
139 140 141 |
# File 'lib/data_migrate/database_tasks.rb', line 139 def schema_sha1(file) ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary")) end |
#sort_migrations(*migrations) ⇒ Object
101 102 103 |
# File 'lib/data_migrate/database_tasks.rb', line 101 def sort_migrations(*migrations) migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) } end |
#sort_string(migration) ⇒ Object
105 106 107 |
# File 'lib/data_migrate/database_tasks.rb', line 105 def sort_string migration "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}" end |
#with_temporary_connection(db_config) ⇒ Object
:nodoc:
25 26 27 28 29 |
# File 'lib/data_migrate/database_tasks.rb', line 25 def with_temporary_connection(db_config) # :nodoc: with_temporary_pool(db_config) do |pool| yield pool.connection end end |
#with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) ⇒ Object
:nodoc:
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/data_migrate/database_tasks.rb', line 14 def with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc: if name db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name) with_temporary_connection(db_config, &block) else ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config| with_temporary_connection(db_config, &block) end end end |