Module: DataMigrate::Tasks::DataMigrateTasks

Extended by:
DataMigrateTasks
Included in:
DataMigrateTasks
Defined in:
lib/data_migrate/tasks/data_migrate_tasks.rb

Instance Method Summary collapse

Instance Method Details

#abort_if_pending_migrations(migrations, message) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 29

def abort_if_pending_migrations(migrations, message)
  if migrations.any?
    puts "You have #{migrations.size} pending #{'migration'.pluralize(migrations.size)}:"
    migrations.each do |pending_migration|
      puts "  %4d %s" % [pending_migration[:version], pending_migration[:name]]
    end
    abort message
  end
end

#dumpObject



12
13
14
15
16
17
18
19
20
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 12

def dump
  if dump_schema_after_migration?
    filename = DataMigrate::DatabaseTasks.schema_file
    ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
    File.open(filename, "w:utf-8") do |file|
      DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
    end
  end
end

#dump_schema_after_migration?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 39

def dump_schema_after_migration?
  if ActiveRecord.respond_to?(:dump_schema_after_migration)
    ActiveRecord.dump_schema_after_migration
  else
    ActiveRecord::Base.dump_schema_after_migration
  end
end

#migrateObject



22
23
24
25
26
27
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 22

def migrate
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil

  DataMigrate::DataMigrator.create_data_schema_table
  DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
end

#migrations_pathsObject



8
9
10
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 8

def migrations_paths
  @migrations_paths ||= DataMigrate.config.data_migrations_path
end

#statusObject



47
48
49
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 47

def status
  DataMigrate::StatusService.dump
end

#status_with_schemaObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/data_migrate/tasks/data_migrate_tasks.rb', line 51

def status_with_schema
  db_list_data = ActiveRecord::Base.connection.select_values(
    "SELECT version FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}"
  )
  db_list_schema = DataMigrate::RailsHelper.schema_migration_versions
  file_list = []

  Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
    # only files matching "20091231235959_some_name.rb" pattern
    if match_data = /(\d{14})_(.+)\.rb/.match(file)
      status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
      file_list << [status, match_data[1], match_data[2], 'data']
    end
  end

  DataMigrate::SchemaMigration.migrations_paths.map do |path|
    Dir.children(path) if Dir.exist?(path)
  end.flatten.compact.each do |file|
    # only files matching "20091231235959_some_name.rb" pattern
    if match_data = /(\d{14})_(.+)\.rb/.match(file)
      status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
      file_list << [status, match_data[1], match_data[2], 'schema']
    end
  end

  file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }

  # output
  puts "\ndatabase: #{database_name}\n\n"
  puts "#{"Status".center(8)} #{"Type".center(8)}  #{"Migration ID".ljust(14)} Migration Name"
  puts "-" * 60
  file_list.each do |file|
    puts "#{file[0].center(8)} #{file[3].center(8)} #{file[1].ljust(14)}  #{file[2].humanize}"
  end
  db_list_schema.each do |version|
    puts "#{'up'.center(8)}  #{version.ljust(14)}  *** NO SCHEMA FILE ***"
  end
  db_list_data.each do |version|
    puts "#{'up'.center(8)}  #{version.ljust(14)}  *** NO DATA FILE ***"
  end
  puts
end