Module: HairTrigger
- Defined in:
- lib/hair_trigger.rb,
lib/hair_trigger/base.rb,
lib/hair_trigger/adapter.rb,
lib/hair_trigger/builder.rb,
lib/hair_trigger/railtie.rb,
lib/hair_trigger/version.rb,
lib/hair_trigger/migrator.rb,
lib/hair_trigger/configuration.rb,
lib/hair_trigger/schema_dumper.rb,
lib/hair_trigger/migration_reader.rb
Defined Under Namespace
Modules: Adapter, Base, MigrationReader, Migrator, SchemaDumper
Classes: Builder, Configuration, Railtie
Constant Summary
collapse
- POSTGRESQL_ADAPTERS =
i[postgresql postgis]
- MYSQL_ADAPTERS =
i[mysql mysql2rgeo trilogy]
- SQLITE_ADAPTERS =
i[sqlite litedb]
- VERSION =
"1.3.1"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.migration_path ⇒ Object
243
244
245
|
# File 'lib/hair_trigger.rb', line 243
def migration_path
@migration_path ||= 'db/migrate'
end
|
.model_path ⇒ Object
235
236
237
|
# File 'lib/hair_trigger.rb', line 235
def model_path
@model_path ||= 'app/models'
end
|
.pg_schema ⇒ Object
247
248
249
|
# File 'lib/hair_trigger.rb', line 247
def pg_schema
@pg_schema ||= 'public'
end
|
.schema_rb_path ⇒ Object
239
240
241
|
# File 'lib/hair_trigger.rb', line 239
def schema_rb_path
@schema_rb_path ||= 'db/schema.rb'
end
|
Class Method Details
.adapter_name_for(adapter) ⇒ Object
251
252
253
|
# File 'lib/hair_trigger.rb', line 251
def adapter_name_for(adapter)
adapter.adapter_name.downcase.sub(/\d$/, '').to_sym
end
|
22
23
24
|
# File 'lib/hair_trigger.rb', line 22
def configure
yield hair_trigger_config
end
|
.current_migrations(options = {}) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/hair_trigger.rb', line 74
def current_migrations(options = {})
if options[:in_rake_task]
options[:include_manual_triggers] = true
options[:schema_rb_first] = true
options[:skip_pending_migrations] = true
end
migrator = self.migrator
migrated = migrator.migrated rescue []
migrations = []
migrator.migrations.each do |migration|
next if options[:skip_pending_migrations] && !migrated.include?(migration.version)
triggers = MigrationReader.get_triggers(migration, options)
migrations << [migration, triggers] unless triggers.empty?
end
if previous_schema = (options.has_key?(:previous_schema) ? options[:previous_schema] : File.exist?(schema_rb_path) && File.read(schema_rb_path))
base_triggers = MigrationReader.get_triggers(previous_schema, options)
unless base_triggers.empty?
version = (previous_schema =~ /ActiveRecord::Schema(\[\d\.\d\])?\.define\(version\: (.*)\)/) && $2.to_i
migrations.unshift [OpenStruct.new({:version => version}), base_triggers]
end
end
migrations = migrations.sort_by{|(migration, triggers)| migration.version} unless options[:schema_rb_first]
all_builders = []
migrations.each do |(migration, triggers)|
triggers.each do |new_trigger|
new_trigger.prepare!
all_builders.delete_if{ |(n, t)| t.prepared_name == new_trigger.prepared_name }
all_builders << [migration.name, new_trigger] unless new_trigger.options[:drop]
end
end
all_builders
end
|
.current_triggers ⇒ Object
34
35
36
37
38
|
# File 'lib/hair_trigger.rb', line 34
def current_triggers
canonical_triggers = models.map(&:triggers).flatten.compact
canonical_triggers.each(&:prepare!)
end
|
.generate_migration(silent = false) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/hair_trigger.rb', line 126
def generate_migration(silent = false)
begin
canonical_triggers = current_triggers
rescue
$stderr.puts $!
exit 1
end
migrations = current_migrations
migration_names = migrations.map(&:first)
existing_triggers = migrations.map(&:last)
up_drop_triggers = []
up_create_triggers = []
down_drop_triggers = []
down_create_triggers = []
existing_triggers.each do |existing|
next if canonical_triggers.any?{ |t| t.prepared_name == existing.prepared_name }
up_drop_triggers.concat existing.drop_triggers
down_create_triggers << existing
end
(canonical_triggers - existing_triggers).each do |new_trigger|
up_create_triggers << new_trigger
down_drop_triggers.concat new_trigger.drop_triggers
if existing = existing_triggers.detect{ |t| t.prepared_name == new_trigger.prepared_name }
up_drop_triggers.concat existing.drop_triggers
down_create_triggers << existing
end
end
return if up_drop_triggers.empty? && up_create_triggers.empty?
migration_name = infer_migration_name(migration_names, up_create_triggers, up_drop_triggers)
migration_version = infer_migration_version(migration_name)
file_name = migration_path + '/' + migration_version + "_" + migration_name.underscore + ".rb"
FileUtils.mkdir_p migration_path
File.open(file_name, "w") { |f| f.write "# This migration was auto-generated via `rake db:generate_trigger_migration'.\n# While you can edit this file, any changes you make to the definitions here\n# will be undone by the next auto-generated trigger migration.\n\nclass \#{migration_name} < ActiveRecord::Migration[\#{ActiveRecord::Migration.current_version}]\n def up\n\#{(up_drop_triggers + up_create_triggers).map{ |t| t.to_ruby(' ') }.join(\"\\n\\n\").lstrip}\n end\n\n def down\n\#{(down_drop_triggers + down_create_triggers).map{ |t| t.to_ruby(' ') }.join(\"\\n\\n\").lstrip}\n end\nend\n RUBY\n file_name\nend\n" }
|
.hair_trigger_config ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/hair_trigger.rb', line 26
def hair_trigger_config
@hair_trigger_config ||= Configuration.new(
postgresql_adapters: POSTGRESQL_ADAPTERS,
mysql_adapters: MYSQL_ADAPTERS,
sqlite_adapters: SQLITE_ADAPTERS
)
end
|
.infer_migration_name(migration_names, create_triggers, drop_triggers) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/hair_trigger.rb', line 187
def infer_migration_name(migration_names, create_triggers, drop_triggers)
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
migration_base_name = "drop trigger#{drop_triggers.size > 1 ? 's' : ''} "
name_parts = drop_triggers.map { |t| t.options[:table] }
part_limit = 6
end
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_base_name}#{name_version}"
end
|
.infer_migration_version(migration_name) ⇒ Object
227
228
229
230
231
232
233
|
# File 'lib/hair_trigger.rb', line 227
def infer_migration_version(migration_name)
timestamped_migrations ?
Time.now.getutc.strftime("%Y%m%d%H%M%S") :
Dir.glob(migration_path + '/*rb').
map{ |f| f.gsub(/.*\/(\d+)_.*/, '\1').to_i}.
inject(0){ |curr, i| i > curr ? i : curr } + 1
end
|
.migrations_current? ⇒ Boolean
122
123
124
|
# File 'lib/hair_trigger.rb', line 122
def migrations_current?
current_migrations.map(&:last).sort.eql? current_triggers.sort
end
|
.migrator ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/hair_trigger.rb', line 57
def migrator
if Gem::Version.new("7.2.0") <= ActiveRecord.gem_version
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
schema_migration = connection.schema_migration
migrations = ActiveRecord::MigrationContext.new(migration_path, schema_migration).migrations
ActiveRecord::Migrator.new(:up, migrations, schema_migration, ActiveRecord::InternalMetadata.new(connection))
elsif Gem::Version.new("7.1.0") <= ActiveRecord.gem_version
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
schema_migration = connection.schema_migration
migrations = ActiveRecord::MigrationContext.new(migration_path, schema_migration).migrations
ActiveRecord::Migrator.new(:up, migrations, schema_migration, ActiveRecord::InternalMetadata.new(connection))
else
migrations = ActiveRecord::MigrationContext.new(migration_path, ActiveRecord::SchemaMigration).migrations
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration)
end
end
|
.models ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/hair_trigger.rb', line 40
def models
if defined?(Rails)
Rails.application.eager_load!
else
Dir[model_path + '/*rb'].each do |model|
class_name = model.sub(/\A.*\/(.*?)\.rb\z/, '\1').camelize
next unless File.read(model) =~ /^\s*trigger[\.\(]/
begin
require "./#{model}" unless Object.const_defined?(class_name)
rescue StandardError, LoadError
raise "unable to load #{class_name} and its trigger(s)"
end
end
end
ActiveRecord::Base.descendants
end
|
.timestamped_migrations ⇒ Object
219
220
221
222
223
224
225
|
# File 'lib/hair_trigger.rb', line 219
def timestamped_migrations
if ActiveRecord::VERSION::STRING >= "7.0."
ActiveRecord.timestamped_migrations
else
ActiveRecord::Base.timestamped_migrations
end
end
|