trackless_triggers 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/CHANGELOG +32 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README +34 -0
  4. data/Rakefile +15 -0
  5. data/VERSION +1 -0
  6. data/init.rb +3 -0
  7. data/lib/trackless_triggers.rb +113 -0
  8. metadata +54 -0
data/CHANGELOG ADDED
@@ -0,0 +1,32 @@
1
+ = CHANGELOG
2
+
3
+ == 0.1.1
4
+
5
+ * Add support for DROP TRIGGER IF EXISTS to update_trigger and drop_trigger
6
+
7
+ == 0.1.0
8
+
9
+ * Add update_trigger
10
+
11
+ == 0.0.8
12
+
13
+ * Fix homepage
14
+
15
+ == 0.0.6
16
+
17
+ * Fix runtime & require issues
18
+
19
+
20
+ == 0.0.5
21
+
22
+ * Cleanup pkg/
23
+
24
+
25
+ == 0.0.2
26
+
27
+ * Convert to gem on github
28
+
29
+ == 0.0.1
30
+
31
+ * Birthday!
32
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2007 Aaron Patterson (aaronp@rubyforge.org)
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,34 @@
1
+ = Trigger Happy
2
+
3
+ Add triggers to your active record migrations.
4
+
5
+ == INSTALL
6
+
7
+ script/plugin install svn://rubyforge.org/var/svn/artriggers/trunk/trigger_happy
8
+
9
+ == EXAMPLE
10
+
11
+ Add a trigger:
12
+
13
+ add_trigger "ai_people",
14
+ :on => 'people',
15
+ :timing => 'after',
16
+ :event => 'insert',
17
+ :statement => 'INSERT INTO log (id, timestamp) VALUES (NEW.id, NOW())'
18
+
19
+ Update an existing trigger (drops then adds):
20
+
21
+ update_trigger "ai_people",
22
+ :on => 'people',
23
+ :timing => 'after',
24
+ :event => 'insert',
25
+ :statement => 'INSERT INTO log (id, timestamp) VALUES (NEW.id, NOW())'
26
+
27
+ Remove a trigger:
28
+
29
+ drop_trigger 'ai_people'
30
+
31
+ == LIMITATIONS
32
+
33
+ Only works with mysql for now.
34
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "trackless_triggers"
7
+ gemspec.summary = "Adds support for MySQL triggers in ActiveRecord"
8
+ gemspec.description = "Adds support for MySQL triggers in ActiveRecord"
9
+ gemspec.email = "apresber@jovoto.com"
10
+ gemspec.homepage = "https://github.com/jovoto-team/trackless_triggers"
11
+ gemspec.authors = ["Aaron Patterson","Christian Eager",'Alexander Presber']
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # require 'active_support'
2
+ require 'active_record'
3
+ require 'trackless_triggers'
@@ -0,0 +1,113 @@
1
+ require 'active_record/connection_adapters/abstract_mysql_adapter'
2
+
3
+ module ActiveRecord
4
+ class SchemaDumper
5
+
6
+ private
7
+
8
+ alias trigger_old_tables tables
9
+
10
+ def tables(stream)
11
+ trigger_old_tables(stream)
12
+
13
+ # dump triggers
14
+ @connection.tables.sort.each do |tbl|
15
+ next if tbl == 'schema_info'
16
+ dump_table_triggers(tbl, stream)
17
+ end
18
+
19
+ # dump functions
20
+ dump_functions(stream)
21
+ end
22
+
23
+ def dump_table_triggers(table, stream)
24
+ triggers = @connection.triggers(table)
25
+ triggers.each do |trigger|
26
+ stream.print " add_trigger \"#{trigger.name}\", :on => \"#{trigger.reference_table}\", :timing => \"#{trigger.timing}\", :event => \"#{trigger.event}\", :statement => \"#{trigger.statement}\""
27
+ stream.puts
28
+ end
29
+ end
30
+
31
+ def dump_functions(stream)
32
+ functions = @connection.functions
33
+ functions.each do |function|
34
+ stream.print " add_function \"#{function.definition.gsub(/DEFINER=`\w*`@`\w*` /i,'')}\"" if function.definition.present?
35
+ stream.puts
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ module ConnectionAdapters
42
+ class TriggerDefinition < Struct.new(:name, :event, :reference_table, :statement, :timing, :created, :sql_mode, :definer, :character_set_client, :collation_connection, :database_collation)
43
+ end
44
+
45
+ class FunctionInfoDefinition < Struct.new(:db, :name, :type, :definer, :modified, :created, :security_type, :comment, :charset, :collation, :db_collation)
46
+ end
47
+
48
+ class FunctionDefinition < Struct.new(:name, :sql_mode, :definition, :charset, :collation, :db_collation)
49
+ end
50
+
51
+ module TriggerFunc
52
+ def triggers(table, name = nil)
53
+ triggers = []
54
+ execute("SHOW TRIGGERS LIKE '#{table}'", name).each do |row|
55
+ triggers << TriggerDefinition.new(*row)
56
+ end
57
+
58
+ triggers
59
+ end
60
+
61
+ def functions(name = nil)
62
+ function_names = []
63
+ functions = []
64
+
65
+ #config = Rails::Application.config
66
+ #config.database_configuration[RAILS_ENV]["database"]
67
+ dbname = ActiveRecord::Base.configurations[Rails.env]['database']
68
+
69
+ execute("SHOW FUNCTION STATUS WHERE DB='#{dbname}'").each do |row|
70
+ func_info = FunctionInfoDefinition.new(*row)
71
+ function_names << func_info.name
72
+ end
73
+
74
+ function_names.each do |name|
75
+ execute("SHOW CREATE FUNCTION #{name}").each do |row|
76
+ functions << FunctionDefinition.new(*row)
77
+ end
78
+ end
79
+
80
+ functions
81
+ end
82
+
83
+ end
84
+
85
+ class MysqlAdapter < AbstractMysqlAdapter
86
+ include TriggerFunc
87
+ end
88
+
89
+ class Mysql2Adapter < AbstractMysqlAdapter
90
+ include TriggerFunc
91
+ end
92
+
93
+ module SchemaStatements
94
+ def add_trigger(name, opts = {})
95
+ sql = "CREATE TRIGGER #{name} #{opts[:timing]} #{opts[:event]} ON #{opts[:on]} FOR EACH ROW #{opts[:statement]}"
96
+ execute sql
97
+ end
98
+
99
+ def drop_trigger(name)
100
+ execute("DROP TRIGGER #{name}")
101
+ end
102
+
103
+ def add_function(sql)
104
+ execute sql
105
+ end
106
+
107
+ def drop_function(name)
108
+ execute("DROP FUNCTION #{name}")
109
+ end
110
+ end
111
+
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trackless_triggers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Patterson
9
+ - Christian Eager
10
+ - Alexander Presber
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2013-03-05 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: Adds support for MySQL triggers in ActiveRecord
17
+ email: apresber@jovoto.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - README
22
+ files:
23
+ - CHANGELOG
24
+ - MIT-LICENSE
25
+ - README
26
+ - Rakefile
27
+ - VERSION
28
+ - init.rb
29
+ - lib/trackless_triggers.rb
30
+ homepage: https://github.com/jovoto-team/trackless_triggers
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.25
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Adds support for MySQL triggers in ActiveRecord
54
+ test_files: []