From c811f2d92825dcb00cf61a6f79fca699e9c9acfb Mon Sep 17 00:00:00 2001 From: Jon Jensen Date: Mon, 27 Apr 2015 22:47:55 -0600 Subject: [PATCH] slightly more sensible migration names, fixes #42 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) --- lib/hair_trigger.rb | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/hair_trigger.rb b/lib/hair_trigger.rb index 88b1b5f..405b358 100644 --- a/lib/hair_trigger.rb +++ b/lib/hair_trigger.rb @@ -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)