class Schemaless::MigrationsGenerator
Generates migrations for schemaless safe mode
Attributes
fields[R]
indexes[R]
join_tables[R]
migration_action[R]
table_name[R]
Private Class Methods
next_migration_number(dirname)
click to toggle source
def validate_file_name!
unless file_name =~ /^[_a-z0-9]+$/ fail IllegalMigrationNameError, file_name end
end
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 103 def self.next_migration_number(dirname) next_migration_number = current_migration_number(dirname) + 1 ActiveRecord::Migration.next_migration_number(next_migration_number) end
Public Instance Methods
create_migration_files()
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 12 def create_migration_files Rails.application.eager_load! all_tables = Schemaless::Worker.all_tables.select(&:migrate?) if models.empty? tables = all_tables else tables = all_tables.select { |t| models.include?(t.model.to_s.downcase) } end tables.each do |table| puts "Generating migrations for #{table}" create_migration_for table end end
Protected Instance Methods
build_file_name(name = [])
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 30 def build_file_name(name = []) if @table.fields.add.any? name << :add name << (@table.fields.add.size > 3 ? :many : @table.fields.add) name << (@table.fields.remove.any? ? :and : :to) end if @table.fields.remove.any? name << :remove name << (@table.fields.remove.size > 3 ? :many : @table.fields.remove) name << :from end name << table_name end
create_migration_for(table)
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 50 def create_migration_for(table) @table = table @fields = @table.fields @indexes = @table.indexes @table_name = @table.name template = file_name =~ /^create_(.+)/ ? 'create' : 'change' migration_template "#{template}_table.rb", "db/migrate/#{file_name}.rb" # case file_name # when /join_table/ # if attributes.length == 2 # @migration_action = 'join' # @join_tables = if pluralize_table_names? # attributes.map(&:plural_name) # else # attributes.map(&:singular_name) # end # set_index_names # end end
file_name()
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 44 def file_name return @file_name if @file_name return "create_#{@table_name}" unless @table.exists? @file_name = build_file_name.flatten.join('_') end
index_name_for(attribute)
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 79 def index_name_for(attribute) if attribute.foreign_key? attribute.name else attribute.name.singularize.foreign_key end.to_sym end
set_index_names()
click to toggle source
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 73 def set_index_names attributes.each_with_index do |att, i| att.index_name = [att, attributes[i - 1]].map { |a| index_name_for(a) } end end
Private Instance Methods
old_migrations()
click to toggle source
def attributes_with_index
attributes.select { |a| !a.reference? && a.has_index? }
end
# File lib/generators/schemaless/migrations/migrations_generator.rb, line 93 def old_migrations ActiveRecord::Migrator.get_all_versions end