hairtrigger 0.2.25 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/hair_trigger/adapter.rb +4 -4
- data/lib/hair_trigger/builder.rb +3 -3
- data/lib/hair_trigger/version.rb +1 -1
- data/lib/hair_trigger.rb +21 -14
- data/lib/tasks/hair_trigger.rake +37 -4
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adffb212907d4617f9ed63b3bbed32f0f4c8e7113e50284eb560a2e9ac0f24cb
|
4
|
+
data.tar.gz: a437870c199e11d4264e496ac45ff6d6f61593e80b9082bc8bf320de33bd0b84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf089ae92b9427a39243f6c096c44eab7ea4d41e7e6bec757781ba2d9b5ebef09683b2528e8ce39c2c28b15919149b82461bd2edcef4d662161a28a52c24671f
|
7
|
+
data.tar.gz: 83c64e0d188cfc45a6ffb701e6f5ee56350d4b636eaaa106405d545731563cc3751e792eecd6eefc1f4d534fdfd31369099e8a9875cec63082ce91d6d753118a
|
data/README.md
CHANGED
@@ -202,6 +202,15 @@ you need to actually implement `up`/`down` methods in your migration
|
|
202
202
|
(rather than `change`) so that it does the right thing when
|
203
203
|
rolling back.
|
204
204
|
|
205
|
+
The `drop_trigger` currently only supports the `drop_trigger(name, table, options = {})`
|
206
|
+
format. You will need to determine what the resulting trigger name is (e.g. `SHOW TRIGGERS`
|
207
|
+
query) and use that name in the `drop_triggers` call. Your `down` migration method
|
208
|
+
might contain something like:
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
drop_trigger(:users_after_insert_row_tr, :transactions)
|
212
|
+
```
|
213
|
+
|
205
214
|
#### Manual triggers and :compatibility
|
206
215
|
|
207
216
|
As bugs are fixed and features are implemented in HairTrigger, it's possible
|
data/lib/hair_trigger/adapter.rb
CHANGED
@@ -31,7 +31,7 @@ module HairTrigger
|
|
31
31
|
select_rows("SELECT name, sql FROM sqlite_master WHERE type = 'trigger' #{name_clause ? " AND name " + name_clause : ""}").each do |(name, definition)|
|
32
32
|
triggers[name] = quote_table_name_in_trigger(definition) + ";\n"
|
33
33
|
end
|
34
|
-
when :mysql
|
34
|
+
when :mysql, :trilogy
|
35
35
|
select_rows("SHOW TRIGGERS").each do |(name, event, table, actions, timing, created, sql_mode, definer)|
|
36
36
|
definer = normalize_mysql_definer(definer)
|
37
37
|
next if options[:only] && !options[:only].include?(name)
|
@@ -48,7 +48,7 @@ FOR EACH ROW
|
|
48
48
|
SELECT tgfoid
|
49
49
|
FROM pg_trigger
|
50
50
|
WHERE NOT tgisinternal AND tgconstrrelid = 0 AND tgrelid IN (
|
51
|
-
SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '
|
51
|
+
SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '#{HairTrigger.pg_schema}')
|
52
52
|
)
|
53
53
|
)
|
54
54
|
SQL
|
@@ -57,9 +57,9 @@ FOR EACH ROW
|
|
57
57
|
SELECT tgname::varchar, pg_get_triggerdef(oid, true)
|
58
58
|
FROM pg_trigger
|
59
59
|
WHERE NOT tgisinternal AND tgconstrrelid = 0 AND tgrelid IN (
|
60
|
-
SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '
|
60
|
+
SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '#{HairTrigger.pg_schema}')
|
61
61
|
)
|
62
|
-
|
62
|
+
|
63
63
|
#{name_clause ? " AND tgname::varchar " + name_clause : ""}
|
64
64
|
UNION
|
65
65
|
SELECT proname || '()', pg_get_functiondef(oid)
|
data/lib/hair_trigger/builder.rb
CHANGED
@@ -162,7 +162,7 @@ module HairTrigger
|
|
162
162
|
chainable_methods :name, :on, :for_each, :before, :after, :where, :security, :timing, :events, :all, :nowrap, :of, :declare
|
163
163
|
|
164
164
|
def create_grouped_trigger?
|
165
|
-
adapter_name == :mysql
|
165
|
+
adapter_name == :mysql || adapter_name == :trilogy
|
166
166
|
end
|
167
167
|
|
168
168
|
def prepare!
|
@@ -224,7 +224,7 @@ module HairTrigger
|
|
224
224
|
[case adapter_name
|
225
225
|
when :sqlite
|
226
226
|
generate_trigger_sqlite
|
227
|
-
when :mysql
|
227
|
+
when :mysql, :trilogy
|
228
228
|
generate_trigger_mysql
|
229
229
|
when :postgresql, :postgis
|
230
230
|
generate_trigger_postgresql
|
@@ -407,7 +407,7 @@ module HairTrigger
|
|
407
407
|
|
408
408
|
def generate_drop_trigger
|
409
409
|
case adapter_name
|
410
|
-
when :sqlite, :mysql
|
410
|
+
when :sqlite, :mysql, :trilogy
|
411
411
|
"DROP TRIGGER IF EXISTS #{prepared_name};\n"
|
412
412
|
when :postgresql, :postgis
|
413
413
|
"DROP TRIGGER IF EXISTS #{prepared_name} ON #{adapter.quote_table_name(options[:table])};\nDROP FUNCTION IF EXISTS #{adapter.quote_table_name(prepared_name)}();\n"
|
data/lib/hair_trigger/version.rb
CHANGED
data/lib/hair_trigger.rb
CHANGED
@@ -13,7 +13,7 @@ module HairTrigger
|
|
13
13
|
autoload :MigrationReader, 'hair_trigger/migration_reader'
|
14
14
|
|
15
15
|
class << self
|
16
|
-
attr_writer :model_path, :schema_rb_path, :migration_path
|
16
|
+
attr_writer :model_path, :schema_rb_path, :migration_path, :pg_schema
|
17
17
|
|
18
18
|
def current_triggers
|
19
19
|
# see what the models say there should be
|
@@ -39,19 +39,14 @@ module HairTrigger
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def migrator
|
42
|
-
|
43
|
-
|
42
|
+
if ActiveRecord::VERSION::STRING >= "7.1."
|
43
|
+
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
|
44
|
+
schema_migration = connection.schema_migration
|
45
|
+
migrations = ActiveRecord::MigrationContext.new(migration_path, schema_migration).migrations
|
46
|
+
ActiveRecord::Migrator.new(:up, migrations, schema_migration, ActiveRecord::InternalMetadata.new(connection))
|
47
|
+
else
|
44
48
|
migrations = ActiveRecord::MigrationContext.new(migration_path, ActiveRecord::SchemaMigration).migrations
|
45
|
-
elsif version >= "5.2."
|
46
|
-
migrations = ActiveRecord::MigrationContext.new(migration_path).migrations
|
47
|
-
else # version >= "4.0."
|
48
|
-
migrations = ActiveRecord::Migrator.migrations(migration_path)
|
49
|
-
end
|
50
|
-
|
51
|
-
if version >= "6.0."
|
52
49
|
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration)
|
53
|
-
else
|
54
|
-
ActiveRecord::Migrator.new(:up, migrations)
|
55
50
|
end
|
56
51
|
end
|
57
52
|
|
@@ -82,7 +77,7 @@ module HairTrigger
|
|
82
77
|
if previous_schema = (options.has_key?(:previous_schema) ? options[:previous_schema] : File.exist?(schema_rb_path) && File.read(schema_rb_path))
|
83
78
|
base_triggers = MigrationReader.get_triggers(previous_schema, options)
|
84
79
|
unless base_triggers.empty?
|
85
|
-
version = (previous_schema =~ /ActiveRecord::Schema
|
80
|
+
version = (previous_schema =~ /ActiveRecord::Schema(\[\d\.\d\])?\.define\(version\: (.*)\)/) && $2.to_i
|
86
81
|
migrations.unshift [OpenStruct.new({:version => version}), base_triggers]
|
87
82
|
end
|
88
83
|
end
|
@@ -200,8 +195,16 @@ end
|
|
200
195
|
"#{migration_base_name}#{name_version}"
|
201
196
|
end
|
202
197
|
|
198
|
+
def timestamped_migrations
|
199
|
+
if ActiveRecord::VERSION::STRING >= "7.0."
|
200
|
+
ActiveRecord.timestamped_migrations
|
201
|
+
else
|
202
|
+
ActiveRecord::Base.timestamped_migrations
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
203
206
|
def infer_migration_version(migration_name)
|
204
|
-
|
207
|
+
timestamped_migrations ?
|
205
208
|
Time.now.getutc.strftime("%Y%m%d%H%M%S") :
|
206
209
|
Dir.glob(migration_path + '/*rb').
|
207
210
|
map{ |f| f.gsub(/.*\/(\d+)_.*/, '\1').to_i}.
|
@@ -220,6 +223,10 @@ end
|
|
220
223
|
@migration_path ||= 'db/migrate'
|
221
224
|
end
|
222
225
|
|
226
|
+
def pg_schema
|
227
|
+
@pg_schema ||= 'public'
|
228
|
+
end
|
229
|
+
|
223
230
|
def adapter_name_for(adapter)
|
224
231
|
adapter.adapter_name.downcase.sub(/\d$/, '').to_sym
|
225
232
|
end
|
data/lib/tasks/hair_trigger.rake
CHANGED
@@ -11,13 +11,46 @@ namespace :db do
|
|
11
11
|
namespace :schema do
|
12
12
|
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
|
13
13
|
task :dump => :environment do
|
14
|
+
next unless ActiveRecord::Base.schema_format == :ruby
|
15
|
+
|
14
16
|
require 'active_record/schema_dumper'
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
|
18
|
+
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
|
19
|
+
|
20
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
21
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
|
22
|
+
connection_pool = ActiveRecord::Base.establish_connection(db_config)
|
23
|
+
|
24
|
+
filename = dump_filename(db_config.name)
|
25
|
+
ActiveRecord::SchemaDumper.previous_schema = File.exist?(filename) ? File.read(filename) : nil
|
26
|
+
|
27
|
+
File.open(filename, "w") do |file|
|
28
|
+
ActiveRecord::SchemaDumper.dump(connection_pool.connection, file)
|
29
|
+
end
|
19
30
|
end
|
31
|
+
|
20
32
|
Rake::Task["db:schema:dump"].reenable
|
21
33
|
end
|
34
|
+
|
35
|
+
def schema_file_type(format)
|
36
|
+
case format
|
37
|
+
when :ruby
|
38
|
+
"schema.rb"
|
39
|
+
when :sql
|
40
|
+
"structure.sql"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# code adopted from activerecord/lib/active_record/tasks/database_tasks.rb#L441
|
45
|
+
def dump_filename(db_config_name)
|
46
|
+
format = ActiveRecord::Base.schema_format
|
47
|
+
filename = if ActiveRecord::Base.configurations.primary?(db_config_name)
|
48
|
+
schema_file_type(format)
|
49
|
+
else
|
50
|
+
"#{db_config_name}_#{schema_file_type(format)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
54
|
+
end
|
22
55
|
end
|
23
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hairtrigger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Jensen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,7 +16,7 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '8'
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '6.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '8'
|
@@ -83,7 +83,7 @@ homepage: http://github.com/jenseng/hair_trigger
|
|
83
83
|
licenses:
|
84
84
|
- MIT
|
85
85
|
metadata: {}
|
86
|
-
post_install_message:
|
86
|
+
post_install_message:
|
87
87
|
rdoc_options: []
|
88
88
|
require_paths:
|
89
89
|
- lib
|
@@ -91,15 +91,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
91
|
requirements:
|
92
92
|
- - ">="
|
93
93
|
- !ruby/object:Gem::Version
|
94
|
-
version:
|
94
|
+
version: '3.0'
|
95
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
97
|
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
99
|
version: '0'
|
100
100
|
requirements: []
|
101
|
-
rubygems_version: 3.
|
102
|
-
signing_key:
|
101
|
+
rubygems_version: 3.5.3
|
102
|
+
signing_key:
|
103
103
|
specification_version: 4
|
104
104
|
summary: easy database triggers for active record
|
105
105
|
test_files: []
|