Skip to content

Commit

Permalink
slightly more sensible migration names, fixes #42
Browse files Browse the repository at this point in the history
migration names could get really long, resulting in Errno::ENAMETOOLONG...
so de-dup identical table+event pairs, and if we have too many just name
it CreateTriggersMultipleTables (possibly w/ numeric suffix if one already
exists)
  • Loading branch information
jenseng committed Apr 28, 2015
1 parent c12b75b commit c811f2d
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/hair_trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,35 @@ def #{prefix}down
end

def infer_migration_name(migration_names, create_triggers, drop_triggers)
migration_base_name = if create_triggers.size > 0
("create trigger#{create_triggers.size > 1 ? 's' : ''} " +
create_triggers.map{ |t| [t.options[:table], t.options[:events].join(" ")].join(" ") }.join(" and ")
).downcase.gsub(/[^a-z0-9_]/, '_').gsub(/_+/, '_').camelize
if create_triggers.size > 0
migration_base_name = "create trigger#{create_triggers.size > 1 ? 's' : ''} "
name_parts = create_triggers.map { |t| [t.options[:table], t.options[:events].join(" ")].join(" ") }.uniq
part_limit = 4
else
("drop trigger#{drop_triggers.size > 1 ? 's' : ''} " +
drop_triggers.map{ |t| t.options[:table] }.join(" and ")
).downcase.gsub(/[^a-z0-9_]/, '_').gsub(/_+/, '_').camelize
migration_base_name = "drop trigger#{drop_triggers.size > 1 ? 's' : ''} "
name_parts = drop_triggers.map { |t| t.options[:table] }
part_limit = 6
end

# don't migration names get too ridiculous
if name_parts.size > part_limit
migration_base_name << " multiple tables"
else
migration_base_name << name_parts.join(" OR ")
end

migration_base_name = migration_base_name.
downcase.
gsub(/[^a-z0-9_]/, '_').
gsub(/_+/, '_').
camelize

name_version = nil
while migration_names.include?("#{migration_base_name}#{name_version}")
name_version = name_version.to_i + 1
end
migration_name = "#{migration_base_name}#{name_version}"

"#{migration_base_name}#{name_version}"
end

def infer_migration_version(migration_name)
Expand Down

0 comments on commit c811f2d

Please sign in to comment.