Class: Generators::DeclareSchema::Migration::Migrator
- Inherits:
-
Object
- Object
- Generators::DeclareSchema::Migration::Migrator
- Defined in:
- lib/generators/declare_schema/migration/migrator.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT_CHARSET =
:utf8mb4- DEFAULT_COLLATION =
:utf8mb4_general
Class Attribute Summary collapse
-
.active_record_class ⇒ Object
Returns the value of attribute active_record_class.
-
.before_generating_migration_callback ⇒ Object
readonly
Returns the value of attribute before_generating_migration_callback.
-
.default_charset ⇒ Object
Returns the value of attribute default_charset.
-
.default_collation ⇒ Object
Returns the value of attribute default_collation.
-
.disable_constraints ⇒ Object
Returns the value of attribute disable_constraints.
-
.disable_indexing ⇒ Object
Returns the value of attribute disable_indexing.
-
.ignore_models ⇒ Object
Returns the value of attribute ignore_models.
-
.ignore_tables ⇒ Object
Returns the value of attribute ignore_tables.
Instance Attribute Summary collapse
-
#renames ⇒ Object
Returns the value of attribute renames.
Class Method Summary collapse
- .always_ignore_tables ⇒ Object
- .before_generating_migration(&block) ⇒ Object
- .connection ⇒ Object
- .default_migration_name ⇒ Object
- .fix_native_types(types) ⇒ Object
- .native_types ⇒ Object
- .run(renames = {}) ⇒ Object
Instance Method Summary collapse
- #change_column_back(table, col_name) ⇒ Object
- #change_foreign_key_constraints(model, old_table_name) ⇒ Object
- #change_indexes(model, old_table_name) ⇒ Object
- #change_table(model, current_table_name) ⇒ Object
- #change_table_options(model, current_table_name) ⇒ Object
- #column_options_from_reverted_table(table, col_name) ⇒ Object
- #connection ⇒ Object
- #create_constraints(model) ⇒ Object
- #create_field(field_spec, field_name_width) ⇒ Object
- #create_indexes(model) ⇒ Object
- #create_table(model) ⇒ Object
- #create_table_options(model, disable_auto_increment) ⇒ Object
- #drop_index(table, name) ⇒ Object
- #extract_column_renames!(to_add, to_remove, table_name) ⇒ Object
-
#extract_table_renames!(to_create, to_drop) ⇒ Object
return a hash of table renames and modifies the passed arrays so that renamed tables are no longer listed as to_create or to_drop.
- #fk_field_options(model, field_name) ⇒ Object
- #format_options(options, type, changing: false) ⇒ Object
- #generate ⇒ Object
-
#habtm_tables ⇒ Object
list habtm join tables.
-
#initialize(ambiguity_resolver = {}) ⇒ Migrator
constructor
A new instance of Migrator.
- #load_rails_models ⇒ Object
-
#models_and_tables ⇒ Object
Returns an array of model classes and an array of table names that generation needs to take into account.
- #native_types ⇒ Object
- #remove_foreign_key(old_table_name, fk_name) ⇒ Object
- #revert_column(table, column) ⇒ Object
- #revert_table(table) ⇒ Object
-
#table_model_classes ⇒ Object
Returns an array of model classes that directly extend ActiveRecord::Base, excluding anything in the CGI module.
- #table_options_for_model(model) ⇒ Object
Constructor Details
#initialize(ambiguity_resolver = {}) ⇒ Migrator
Returns a new instance of Migrator.
130 131 132 133 134 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 130 def initialize(ambiguity_resolver = {}) @ambiguity_resolver = ambiguity_resolver @drops = [] @renames = nil end |
Class Attribute Details
.active_record_class ⇒ Object
Returns the value of attribute active_record_class.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def active_record_class @active_record_class end |
.before_generating_migration_callback ⇒ Object (readonly)
Returns the value of attribute before_generating_migration_callback.
87 88 89 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 87 def @before_generating_migration_callback end |
.default_charset ⇒ Object
Returns the value of attribute default_charset.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def default_charset @default_charset end |
.default_collation ⇒ Object
Returns the value of attribute default_collation.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def default_collation @default_collation end |
.disable_constraints ⇒ Object
Returns the value of attribute disable_constraints.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def disable_constraints @disable_constraints end |
.disable_indexing ⇒ Object
Returns the value of attribute disable_indexing.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def disable_indexing @disable_indexing end |
.ignore_models ⇒ Object
Returns the value of attribute ignore_models.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def ignore_models @ignore_models end |
.ignore_tables ⇒ Object
Returns the value of attribute ignore_tables.
85 86 87 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 85 def ignore_tables @ignore_tables end |
Instance Attribute Details
#renames ⇒ Object
Returns the value of attribute renames.
136 137 138 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 136 def renames @renames end |
Class Method Details
.always_ignore_tables ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 239 def self.always_ignore_tables sessions_table = begin if defined?(CGI::Session::ActiveRecordStore::Session) && defined?(ActionController::Base) && ActionController::Base.session_store == CGI::Session::ActiveRecordStore CGI::Session::ActiveRecordStore::Session.table_name end rescue nil end [ 'schema_info', ActiveRecord::Base.try(:schema_migrations_table_name) || 'schema_migrations', ActiveRecord::Base.try(:internal_metadata_table_name) || 'ar_internal_metadata', sessions_table ].compact end |
.before_generating_migration(&block) ⇒ Object
124 125 126 127 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 124 def (&block) block or raise ArgumentError, 'A block is required when setting the before_generating_migration callback' @before_generating_migration_callback = block end |
.connection ⇒ Object
106 107 108 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 106 def connection ActiveRecord::Base.connection end |
.default_migration_name ⇒ Object
100 101 102 103 104 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 100 def default_migration_name existing = Dir["#{Rails.root}/db/migrate/*declare_schema_migration*"] max = existing.grep(/([0-9]+)\.rb$/) { Regexp.last_match(1).to_i }.max.to_i "declare_schema_migration_#{max + 1}" end |
.fix_native_types(types) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 110 def fix_native_types(types) case connection.class.name when /mysql/i types[:integer][:limit] ||= 11 types[:text][:limit] ||= 0xffff types[:binary][:limit] ||= 0xffff end types end |
.native_types ⇒ Object
120 121 122 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 120 def native_types @native_types ||= fix_native_types(connection.native_database_types) end |
Instance Method Details
#change_column_back(table, col_name) ⇒ Object
635 636 637 638 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 635 def change_column_back(table, col_name) type, = (table, col_name) "change_column :#{table}, :#{col_name}, #{type}#{', ' + .strip if }" end |
#change_foreign_key_constraints(model, old_table_name) ⇒ Object
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 528 def change_foreign_key_constraints(model, old_table_name) ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) and raise 'SQLite does not support foreign keys' return [[], []] if Migrator.disable_indexing new_table_name = model.table_name existing_fks = ::DeclareSchema::Model::ForeignKeyDefinition.for_model(model, old_table_name) model_fks = model.constraint_specs add_fks = model_fks - existing_fks drop_fks = existing_fks - model_fks undo_add_fks = [] undo_drop_fks = [] add_fks.map! do |fk| # next if fk.parent.constantize.abstract_class || fk.parent == fk.model.class_name undo_add_fks << remove_foreign_key(old_table_name, fk.[:constraint_name]) fk.to_add_statement end.compact drop_fks.map! do |fk| undo_drop_fks << fk.to_add_statement remove_foreign_key(new_table_name, fk.[:constraint_name]) end [drop_fks + add_fks, undo_add_fks + undo_drop_fks] end |
#change_indexes(model, old_table_name) ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 489 def change_indexes(model, old_table_name) return [[], []] if Migrator.disable_constraints new_table_name = model.table_name existing_indexes = ::DeclareSchema::Model::IndexDefinition.for_model(model, old_table_name) model_indexes_with_equivalents = model.index_definitions_with_primary_key model_indexes = model_indexes_with_equivalents.map do |i| if i.explicit_name.nil? if ex = existing_indexes.find { |e| i != e && e.equivalent?(i) } i.with_name(ex.name) end end || i end existing_has_primary_key = existing_indexes.any? { |i| i.name == ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME } model_has_primary_key = model_indexes.any? { |i| i.name == ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME } add_indexes_init = model_indexes - existing_indexes drop_indexes_init = existing_indexes - model_indexes undo_add_indexes = [] undo_drop_indexes = [] add_indexes = add_indexes_init.map do |i| undo_add_indexes << drop_index(old_table_name, i.name) unless i.name == ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME i.to_add_statement(new_table_name, existing_has_primary_key) end drop_indexes = drop_indexes_init.map do |i| undo_drop_indexes << i.to_add_statement(old_table_name, model_has_primary_key) drop_index(new_table_name, i.name) unless i.name == ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME end.compact # the order is important here - adding a :unique, for instance needs to remove then add [drop_indexes + add_indexes, undo_add_indexes + undo_drop_indexes] end |
#change_table(model, current_table_name) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 389 def change_table(model, current_table_name) new_table_name = model.table_name db_columns = model.connection.columns(current_table_name).index_by(&:name) key_missing = db_columns[model.primary_key].nil? && model.primary_key.present? if model.primary_key.present? db_columns.delete(model.primary_key) end model_column_names = model.field_specs.keys.map(&:to_s) db_column_names = db_columns.keys.map(&:to_s) to_add = model_column_names - db_column_names to_add += [model.primary_key] if key_missing && model.primary_key.present? to_remove = db_column_names - model_column_names to_remove -= [model.primary_key.to_sym] if model.primary_key.present? to_rename = extract_column_renames!(to_add, to_remove, new_table_name) db_column_names -= to_rename.keys db_column_names |= to_rename.values to_change = db_column_names & model_column_names renames = to_rename.map do |old_name, new_name| "rename_column :#{new_table_name}, :#{old_name}, :#{new_name}" end undo_renames = to_rename.map do |old_name, new_name| "rename_column :#{new_table_name}, :#{new_name}, :#{old_name}" end to_add = to_add.sort_by { |c| model.field_specs[c]&.position || 0 } adds = to_add.map do |c| args = if (spec = model.field_specs[c]) = (model, c).merge(spec.) [":#{spec.sql_type}", *(, spec.sql_type)] else [":integer"] end "add_column :#{new_table_name}, :#{c}, #{args.join(', ')}" end undo_adds = to_add.map do |c| "remove_column :#{new_table_name}, :#{c}" end removes = to_remove.map do |c| "remove_column :#{new_table_name}, :#{c}" end undo_removes = to_remove.map do |c| revert_column(current_table_name, c) end old_names = to_rename.invert changes = [] undo_changes = [] to_change.each do |c| col_name = old_names[c] || c col = db_columns[col_name] spec = model.field_specs[c] if spec.different_to?(current_table_name, col) # TODO: TECH-4814 DRY this up to a diff function that returns the differences. It's different if it has differences. -Colin change_spec = (model, c) change_spec[:limit] ||= spec.limit if (spec.sql_type != :text || ::DeclareSchema::Model::FieldSpec.mysql_text_limits?) && (spec.limit || col.limit) change_spec[:precision] = spec.precision unless spec.precision.nil? change_spec[:scale] = spec.scale unless spec.scale.nil? change_spec[:null] = spec.null unless spec.null && col.null change_spec[:default] = spec.default unless spec.default.nil? && col.default.nil? change_spec[:collation] = spec.collation unless spec.collation.nil? change_spec[:charset] = spec.charset unless spec.charset.nil? changes << "change_column :#{new_table_name}, :#{c}, " + ([":#{spec.sql_type}"] + (change_spec, spec.sql_type, changing: true)).join(", ") back = change_column_back(current_table_name, col_name) undo_changes << back unless back.blank? end end.compact index_changes, undo_index_changes = change_indexes(model, current_table_name) fk_changes, undo_fk_changes = if ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) [[], []] else change_foreign_key_constraints(model, current_table_name) end , = if ActiveRecord::Base.connection.class.name.match?(/mysql/i) (model, current_table_name) else [[], []] end [(renames + adds + removes + changes) * "\n", (undo_renames + undo_adds + undo_removes + undo_changes) * "\n", index_changes * "\n", undo_index_changes * "\n", fk_changes * "\n", undo_fk_changes * "\n", * "\n", * "\n"] end |
#change_table_options(model, current_table_name) ⇒ Object
596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 596 def (model, current_table_name) = ::DeclareSchema::Model::TableOptionsDefinition.for_model(model, current_table_name) = ::DeclareSchema::Model::TableOptionsDefinition.new(model.table_name, (model)) if .equivalent?() [[], []] else [ [.alter_table_statement], [.alter_table_statement] ] end end |
#column_options_from_reverted_table(table, col_name) ⇒ Object
622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 622 def (table, col_name) revert = revert_table(table) if (md = revert.match(/\s*t\.column\s+"#{col_name}",\s+(:[a-zA-Z0-9_]+)(?:,\s+(.*?)$)?/m)) # Ugly migration _, type, = *md elsif (md = revert.match(/\s*t\.([a-z_]+)\s+"#{col_name}"(?:,\s+(.*?)$)?/m)) # Sexy migration _, type, = *md type = ":#{type}" end [type, ] end |
#connection ⇒ Object
155 156 157 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 155 def connection self.class.connection end |
#create_constraints(model) ⇒ Object
379 380 381 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 379 def create_constraints(model) model.constraint_specs.map { |fk| fk.to_add_statement(model.table_name) } end |
#create_field(field_spec, field_name_width) ⇒ Object
383 384 385 386 387 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 383 def create_field(field_spec, field_name_width) = (field_spec.model, field_spec.name).merge(field_spec.) args = [field_spec.name.inspect] + (, field_spec.sql_type) format("t.%-*s %s", field_name_width, field_spec.sql_type, args.join(', ')) end |
#create_indexes(model) ⇒ Object
375 376 377 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 375 def create_indexes(model) model.index_definitions.map { |i| i.to_add_statement(model.table_name) } end |
#create_table(model) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 334 def create_table(model) longest_field_name = model.field_specs.values.map { |f| f.sql_type.to_s.length }.max disable_auto_increment = model.respond_to?(:disable_auto_increment) && model.disable_auto_increment = ::DeclareSchema::Model::TableOptionsDefinition.new(model.table_name, (model)) field_definitions = [ disable_auto_increment ? "t.integer :id, limit: 8, auto_increment: false, primary_key: true" : nil, *(model.field_specs.values.sort_by(&:position).map { |f| create_field(f, longest_field_name) }) ].compact <<~EOS.strip create_table :#{model.table_name}, #{(model, disable_auto_increment)} do |t| #{field_definitions.join("\n")} end #{.alter_table_statement unless ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/)} #{create_indexes(model).join("\n") unless Migrator.disable_indexing} #{create_constraints(model).join("\n") unless Migrator.disable_indexing} EOS end |
#create_table_options(model, disable_auto_increment) ⇒ Object
354 355 356 357 358 359 360 361 362 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 354 def (model, disable_auto_increment) if model.primary_key.blank? || disable_auto_increment "id: false" elsif model.primary_key == "id" "id: :bigint" else "primary_key: :#{model.primary_key}" end end |
#drop_index(table, name) ⇒ Object
522 523 524 525 526 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 522 def drop_index(table, name) # see https://hobo.lighthouseapp.com/projects/8324/tickets/566 # for why the rescue exists "remove_index :#{table}, name: :#{name} rescue ActiveRecord::StatementInvalid" end |
#extract_column_renames!(to_add, to_remove, table_name) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 215 def extract_column_renames!(to_add, to_remove, table_name) if renames to_rename = {} if (column_renames = renames&.[](table_name.to_sym)) # A hash of table renames has been provided column_renames.each_pair do |old_name, new_name| if to_add.delete(new_name.to_s) && to_remove.delete(old_name.to_s) to_rename[old_name.to_s] = new_name.to_s else raise Error, "Invalid rename specified: #{old_name} => #{new_name}" end end end to_rename elsif @ambiguity_resolver @ambiguity_resolver.call(to_add, to_remove, "column", "#{table_name}.") else raise Error, "Unable to resolve migration ambiguities in table #{table_name}" end end |
#extract_table_renames!(to_create, to_drop) ⇒ Object
return a hash of table renames and modifies the passed arrays so that renamed tables are no longer listed as to_create or to_drop
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 190 def extract_table_renames!(to_create, to_drop) if renames # A hash of table renames has been provided to_rename = {} renames.each_pair do |old_name, new_name| new_name = new_name[:table_name] if new_name.is_a?(Hash) next unless new_name if to_create.delete(new_name.to_s) && to_drop.delete(old_name.to_s) to_rename[old_name.to_s] = new_name.to_s else raise Error, "Invalid table rename specified: #{old_name} => #{new_name}" end end to_rename elsif @ambiguity_resolver @ambiguity_resolver.call(to_create, to_drop, "table", nil) else raise Error, "Unable to resolve migration ambiguities" end end |
#fk_field_options(model, field_name) ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 575 def (model, field_name) foreign_key = model.constraint_specs.find { |fk| field_name == fk.foreign_key.to_s } if foreign_key && (parent_table = foreign_key.parent_table_name) parent_columns = connection.columns(parent_table) rescue [] pk_limit = if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target if Rails::VERSION::MAJOR <= 4 pk_column.cast_type.limit else pk_column.limit end else 8 end { limit: pk_limit } else {} end end |
#format_options(options, type, changing: false) ⇒ Object
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 558 def (, type, changing: false) .map do |k, v| unless changing next if k == :limit && (type == :decimal || v == native_types[type][:limit]) next if k == :null && v == true end next if k == :limit && type == :text && !::DeclareSchema::Model::FieldSpec.mysql_text_limits? if k.is_a?(Symbol) "#{k}: #{v.inspect}" else "#{k.inspect} => #{v.inspect}" end end.compact end |
#generate ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 259 def generate models, db_tables = models_and_tables models_by_table_name = {} models.each do |m| if !models_by_table_name.has_key?(m.table_name) models_by_table_name[m.table_name] = m elsif m.superclass == models_by_table_name[m.table_name].superclass.superclass # we need to ensure that models_by_table_name contains the # base class in an STI hierarchy models_by_table_name[m.table_name] = m end end # generate shims for HABTM models habtm_tables.each do |name, refls| models_by_table_name[name] = HabtmModelShim.from_reflection(refls.first) end model_table_names = models_by_table_name.keys to_create = model_table_names - db_tables to_drop = db_tables - model_table_names - self.class.always_ignore_tables to_change = model_table_names to_rename = extract_table_renames!(to_create, to_drop) renames = to_rename.map do |old_name, new_name| "rename_table :#{old_name}, :#{new_name}" end * "\n" undo_renames = to_rename.map do |old_name, new_name| "rename_table :#{new_name}, :#{old_name}" end * "\n" drops = to_drop.map do |t| "drop_table :#{t}" end * "\n" undo_drops = to_drop.map do |t| revert_table(t) end * "\n\n" creates = to_create.map do |t| create_table(models_by_table_name[t]) end * "\n\n" undo_creates = to_create.map do |t| "drop_table :#{t}" end * "\n" changes = [] undo_changes = [] index_changes = [] undo_index_changes = [] fk_changes = [] undo_fk_changes = [] = [] = [] to_change.each do |t| model = models_by_table_name[t] table = to_rename.key(t) || model.table_name if table.in?(db_tables) change, undo, index_change, undo_index, fk_change, undo_fk, , = change_table(model, table) changes << change undo_changes << undo index_changes << index_change undo_index_changes << undo_index fk_changes << fk_change undo_fk_changes << undo_fk << << end end up = [renames, drops, creates, changes, index_changes, fk_changes, ].flatten.reject(&:blank?) * "\n\n" down = [undo_changes, undo_renames, undo_drops, undo_creates, undo_index_changes, undo_fk_changes, ].flatten.reject(&:blank?) * "\n\n" [up, down] end |
#habtm_tables ⇒ Object
list habtm join tables
164 165 166 167 168 169 170 171 172 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 164 def habtm_tables reflections = Hash.new { |h, k| h[k] = [] } ActiveRecord::Base.send(:descendants).map do |c| c.reflect_on_all_associations(:has_and_belongs_to_many).each do |a| reflections[a.join_table] << a end end reflections end |
#load_rails_models ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 138 def load_rails_models ActiveRecord::Migration.verbose = false Rails.application.eager_load! Rails::Engine.subclasses.each(&:eager_load!) self.class.&.call end |
#models_and_tables ⇒ Object
Returns an array of model classes and an array of table names that generation needs to take into account
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 176 def models_and_tables ignore_model_names = Migrator.ignore_models.map { |model| model.to_s.underscore } all_models = table_model_classes declare_schema_models = all_models.select do |m| (m.name['HABTM_'] || (m.include_in_migration if m.respond_to?(:include_in_migration))) && !m.name.underscore.in?(ignore_model_names) end non_declare_schema_models = all_models - declare_schema_models db_tables = connection.tables - Migrator.ignore_tables.map(&:to_s) - non_declare_schema_models.map(&:table_name) [declare_schema_models, db_tables] end |
#native_types ⇒ Object
159 160 161 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 159 def native_types self.class.native_types end |
#remove_foreign_key(old_table_name, fk_name) ⇒ Object
554 555 556 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 554 def remove_foreign_key(old_table_name, fk_name) "remove_foreign_key('#{old_table_name}', name: '#{fk_name}')" end |
#revert_column(table, column) ⇒ Object
640 641 642 643 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 640 def revert_column(table, column) type, = (table, column) "add_column :#{table}, :#{column}, #{type}#{', ' + .strip if }" end |
#revert_table(table) ⇒ Object
610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 610 def revert_table(table) res = StringIO.new schema_dumper_klass = case Rails::VERSION::MAJOR when 4 ActiveRecord::SchemaDumper else ActiveRecord::ConnectionAdapters::SchemaDumper end schema_dumper_klass.send(:new, ActiveRecord::Base.connection).send(:table, table, res) res.string.strip.gsub("\n ", "\n") end |
#table_model_classes ⇒ Object
Returns an array of model classes that directly extend ActiveRecord::Base, excluding anything in the CGI module
148 149 150 151 152 153 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 148 def table_model_classes load_rails_models ActiveRecord::Base.send(:descendants).select do |klass| klass.base_class == klass && !klass.name.starts_with?("CGI::") end end |
#table_options_for_model(model) ⇒ Object
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/generators/declare_schema/migration/migrator.rb', line 364 def (model) if ActiveRecord::Base.connection.class.name.match?(/SQLite3Adapter/) {} else { charset: model.[:charset] || Migrator.default_charset, collation: model.[:collation] || Migrator.default_collation } end end |