Module: HairTrigger::Adapter
- Defined in:
- lib/hair_trigger/adapter.rb
Instance Method Summary collapse
- #create_trigger(name = nil, options = {}) ⇒ Object
- #drop_trigger(name, table, options = {}) ⇒ Object
- #implicit_mysql_definer ⇒ Object
- #normalize_mysql_definer(definer) ⇒ Object
-
#quote_table_name_in_trigger(definition) ⇒ Object
a bit hacky, but we need to ensure the table name is always quoted on the way out, not just for reserved words.
- #triggers(options = {}) ⇒ Object
Instance Method Details
#create_trigger(name = nil, options = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/hair_trigger/adapter.rb', line 3 def create_trigger(name = nil, = {}) if name.is_a?(Hash) = name name = nil end ::HairTrigger::Builder.new(name, .merge(:execute => true, :adapter => self)) end |
#drop_trigger(name, table, options = {}) ⇒ Object
11 12 13 |
# File 'lib/hair_trigger/adapter.rb', line 11 def drop_trigger(name, table, = {}) ::HairTrigger::Builder.new(name, .merge(:execute => true, :drop => true, :table => table, :adapter => self)).all{} end |
#implicit_mysql_definer ⇒ Object
21 22 23 |
# File 'lib/hair_trigger/adapter.rb', line 21 def implicit_mysql_definer "'#{@config[:username] || 'root'}'@'#{@config[:host] || 'localhost'}'" end |
#normalize_mysql_definer(definer) ⇒ Object
15 16 17 18 19 |
# File 'lib/hair_trigger/adapter.rb', line 15 def normalize_mysql_definer(definer) user, host = definer.split('@') host = @config[:host] || 'localhost' if host == '%' "'#{user}'@'#{host}'" # SHOW TRIGGERS doesn't quote them, but we need quotes for creating a trigger end |
#quote_table_name_in_trigger(definition) ⇒ Object
a bit hacky, but we need to ensure the table name is always quoted on the way out, not just for reserved words. this is because we always quote on the way in, so we need them to match exactly when diffing
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hair_trigger/adapter.rb', line 83 def quote_table_name_in_trigger(definition) pattern = / ( CREATE\sTRIGGER\s+ \S+\s+ (BEFORE|AFTER|INSTEAD\s+OF)\s+ (INSERT|UPDATE|DELETE|TRUNCATE)\s+ (OR\s+(INSERT|UPDATE|DELETE|TRUNCATE)\s+)* (ON\s+) ) (\w+) # quote if not already quoted /ixm definition.sub(pattern, '\\1"\\7"') end |
#triggers(options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
# File 'lib/hair_trigger/adapter.rb', line 25 def triggers( = {}) triggers = {} name_clause = [:only] ? "IN ('" + [:only].join("', '") + "')" : nil adapter_name = HairTrigger.adapter_name_for(self) case adapter_name when *HairTrigger::SQLITE_ADAPTERS select_rows("SELECT name, sql FROM sqlite_master WHERE type = 'trigger' #{name_clause ? " AND name " + name_clause : ""}").each do |(name, definition)| triggers[name] = quote_table_name_in_trigger(definition) + ";\n" end when *HairTrigger::MYSQL_ADAPTERS select_rows("SHOW TRIGGERS").each do |(name, event, table, actions, timing, created, sql_mode, definer)| definer = normalize_mysql_definer(definer) next if [:only] && ![:only].include?(name) triggers[name.strip] = <<-SQL CREATE #{definer != implicit_mysql_definer ? "DEFINER = #{definer} " : ""}TRIGGER #{name} #{timing} #{event} ON `#{table}` FOR EACH ROW #{actions} SQL end when *POSTGRESQL_ADAPTERS function_conditions = "(SELECT typname FROM pg_type WHERE oid = prorettype) = 'trigger'" function_conditions << <<-SQL unless [:simple_check] AND oid IN ( SELECT tgfoid FROM pg_trigger WHERE NOT tgisinternal AND tgconstrrelid = 0 AND tgrelid IN ( SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '#{HairTrigger.pg_schema}') ) ) SQL sql = <<-SQL SELECT tgname::varchar, pg_get_triggerdef(oid, true) FROM pg_trigger WHERE NOT tgisinternal AND tgconstrrelid = 0 AND tgrelid IN ( SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '#{HairTrigger.pg_schema}') ) #{name_clause ? " AND tgname::varchar " + name_clause : ""} UNION SELECT proname || '()', pg_get_functiondef(oid) FROM pg_proc WHERE #{function_conditions} #{name_clause ? " AND (proname || '()')::varchar " + name_clause : ""} SQL select_rows(sql).each do |(name, definition)| triggers[name] = quote_table_name_in_trigger(definition) end else raise "don't know how to retrieve #{adapter_name} triggers yet" end triggers end |