Class: PgSqlTriggers::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_sql_triggers/migrator.rb,
lib/pg_sql_triggers/migrator/safety_validator.rb,
lib/pg_sql_triggers/migrator/pre_apply_comparator.rb,
lib/pg_sql_triggers/migrator/pre_apply_diff_reporter.rb

Overview

rubocop:disable Metrics/ClassLength – singleton orchestrator for migration discovery, safety validation, pre-apply comparison, application, and registry cleanup. The class-method API is the public surface; splitting into collaborators would break callers.

Defined Under Namespace

Classes: PreApplyComparator, PreApplyDiffReporter, SafetyValidator

Constant Summary collapse

MIGRATIONS_TABLE_NAME =
"trigger_migrations"

Class Method Summary collapse

Class Method Details

.apply_migration(migration_class, migration, direction) ⇒ Object

Execute the migration inside a transaction and record the version change.



254
255
256
257
258
259
260
# File 'lib/pg_sql_triggers/migrator.rb', line 254

def apply_migration(migration_class, migration, direction)
  ActiveRecord::Base.transaction do
    migration_class.new.public_send(direction)
    record_migration_version(migration.version, direction)
    cleanup_orphaned_registry_entries if direction == :down
  end
end

.cleanup_orphaned_registry_entriesObject

Clean up registry entries for triggers that no longer exist in the database This is called after rolling back migrations to keep the registry in sync



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/pg_sql_triggers/migrator.rb', line 343

def cleanup_orphaned_registry_entries
  return unless ActiveRecord::Base.connection.table_exists?("pg_sql_triggers_registry")

  introspection = PgSqlTriggers::DatabaseIntrospection.new

  # Get all triggers from registry
  registry_triggers = PgSqlTriggers::TriggerRegistry.all

  # Remove registry entries for triggers that don't exist in database
  registry_triggers.each do |registry_trigger|
    unless introspection.trigger_exists?(registry_trigger.trigger_name)
      Rails.logger.info("Removing orphaned registry entry for trigger: #{registry_trigger.trigger_name}")
      registry_trigger.destroy
    end
  end
end

.current_versionObject



36
37
38
39
40
41
42
# File 'lib/pg_sql_triggers/migrator.rb', line 36

def current_version
  ensure_migrations_table!
  result = ActiveRecord::Base.connection.select_one(
    "SELECT version FROM #{MIGRATIONS_TABLE_NAME} ORDER BY version DESC LIMIT 1"
  )
  result ? result["version"].to_i : 0
end

.ensure_migrations_table!Object



26
27
28
29
30
31
32
33
34
# File 'lib/pg_sql_triggers/migrator.rb', line 26

def ensure_migrations_table!
  return if migrations_table_exists?

  ActiveRecord::Base.connection.create_table MIGRATIONS_TABLE_NAME do |t|
    t.string :version, null: false
  end

  ActiveRecord::Base.connection.add_index MIGRATIONS_TABLE_NAME, :version, unique: true
end

.log_pre_apply_differences(diff_result, migration) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/pg_sql_triggers/migrator.rb', line 242

def log_pre_apply_differences(diff_result, migration)
  diff_report = PreApplyDiffReporter.format(diff_result, migration_name: migration.name)
  if defined?(Rails.logger)
    msg = "Pre-apply comparison for migration #{migration.name}:\n#{diff_report}"
    Rails.logger.warn(msg)
  end
  return unless ENV["VERBOSE"] != "false" || defined?(Rails::Console)

  Rails.logger.debug { "\n#{PreApplyDiffReporter.format_summary(diff_result)}\n" }
end

.migrationsObject



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
# File 'lib/pg_sql_triggers/migrator.rb', line 44

def migrations
  return [] unless Dir.exist?(migrations_path)

  files = Dir.glob(migrations_path.join("*.rb"))
  files.map do |file|
    basename = File.basename(file, ".rb")
    # Handle Rails migration format: YYYYMMDDHHMMSS_name
    # Extract version (timestamp) and name
    if basename =~ /^(\d+)_(.+)$/
      version = ::Regexp.last_match(1).to_i
      name = ::Regexp.last_match(2)
    else
      # Fallback: treat first part as version
      parts = basename.split("_", 2)
      version = parts[0].to_i
      name = parts[1] || basename
    end

    Struct.new(:version, :name, :filename, :path, keyword_init: true).new(
      version: version,
      name: name,
      filename: File.basename(file),
      path: file
    )
  end
end

.migrations_pathObject



18
19
20
# File 'lib/pg_sql_triggers/migrator.rb', line 18

def migrations_path
  Rails.root.join("db/triggers")
end

.migrations_table_exists?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/pg_sql_triggers/migrator.rb', line 22

def migrations_table_exists?
  ActiveRecord::Base.connection.table_exists?(MIGRATIONS_TABLE_NAME)
end

.pending_migrationsObject



71
72
73
74
# File 'lib/pg_sql_triggers/migrator.rb', line 71

def pending_migrations
  current_ver = current_version
  migrations.select { |m| m.version > current_ver }
end

.perform_pre_apply_comparison(captured_sql, migration) ⇒ Object

Run the pre-apply comparator and log the results. Any failure in the comparator itself is swallowed and logged to avoid breaking migrations that are otherwise safe.



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/pg_sql_triggers/migrator.rb', line 228

def perform_pre_apply_comparison(captured_sql, migration)
  diff_result = PreApplyComparator.compare_sql(captured_sql)

  if diff_result[:has_differences]
    log_pre_apply_differences(diff_result, migration)
  elsif defined?(Rails.logger)
    Rails.logger.info("Pre-apply comparison: No differences detected for migration #{migration.name}")
  end
rescue StandardError => e
  return unless defined?(Rails.logger)

  Rails.logger.warn("Pre-apply comparison failed for migration #{migration.name}: #{e.message}")
end

.perform_safety_validation(captured_sql, migration) ⇒ Object

Run the safety validator against captured SQL and translate any UnsafeOperationError into a generic StandardError that halts the migration.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/pg_sql_triggers/migrator.rb', line 208

def perform_safety_validation(captured_sql, migration)
  allow_unsafe = ENV["ALLOW_UNSAFE_MIGRATIONS"] == "true" ||
                 (defined?(PgSqlTriggers) && PgSqlTriggers.allow_unsafe_migrations == true)
  SafetyValidator.validate_sql!(captured_sql, allow_unsafe: allow_unsafe)
rescue SafetyValidator::UnsafeOperationError => e
  error_msg = "\n#{e.message}\n\n"
  Rails.logger.error(error_msg) if defined?(Rails.logger)
  Rails.logger.debug error_msg if ENV["VERBOSE"] != "false" || defined?(Rails::Console)
  raise StandardError, "Migration blocked due to unsafe DROP + CREATE operations. " \
                       "Review the errors above and set ALLOW_UNSAFE_MIGRATIONS=true if you must proceed."
rescue StandardError => e
  # Don't fail the migration if validation fails for other reasons – just log it
  return unless defined?(Rails.logger)

  Rails.logger.warn("Safety validation failed for migration #{migration.name}: #{e.message}")
end

.record_migration_version(version, direction) ⇒ Object



262
263
264
265
266
267
268
269
270
# File 'lib/pg_sql_triggers/migrator.rb', line 262

def record_migration_version(version, direction)
  connection = ActiveRecord::Base.connection
  version_str = connection.quote(version.to_s)
  if direction == :up
    connection.execute("INSERT INTO #{MIGRATIONS_TABLE_NAME} (version) VALUES (#{version_str})")
  else
    connection.execute("DELETE FROM #{MIGRATIONS_TABLE_NAME} WHERE version = #{version_str}")
  end
end

.resolve_migration_class(migration_name) ⇒ Object

Resolve the migration class from its snake_case filename. Tries several naming patterns so migrations can use plain CamelCase, “Add” prefix, or be nested under PgSqlTriggers.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/pg_sql_triggers/migrator.rb', line 189

def resolve_migration_class(migration_name)
  base_class_name = migration_name.camelize
  candidates = [
    base_class_name,
    "Add#{base_class_name}",
    "PgSqlTriggers::#{base_class_name}",
    "PgSqlTriggers::Add#{base_class_name}"
  ]
  last_error = nil
  candidates.each do |candidate|
    return candidate.constantize
  rescue NameError => e
    last_error = e
  end
  raise last_error
end

.run(direction = :up, target_version = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/pg_sql_triggers/migrator.rb', line 76

def run(direction = :up, target_version = nil)
  ensure_migrations_table!

  case direction
  when :up
    run_up(target_version)
  when :down
    run_down(target_version)
  end
end

.run_down(target_version = nil, confirmation: nil) ⇒ Object



123
124
125
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
# File 'lib/pg_sql_triggers/migrator.rb', line 123

def run_down(target_version = nil, confirmation: nil)
  # Check kill switch before running migrations
  # This provides protection when called directly from console
  # When called from rake tasks, the ENV override will already be in place
  # Use ENV["CONFIRMATION_TEXT"] if confirmation is not provided (for rake task compatibility)
  confirmation ||= ENV.fetch("CONFIRMATION_TEXT", nil)
  PgSqlTriggers::SQL::KillSwitch.check!(
    operation: :migrator_run_down,
    environment: Rails.env,
    confirmation: confirmation,
    actor: { type: "Console", id: "Migrator.run_down" }
  )

  current_ver = current_version
  return if current_ver.zero?

  if target_version
    # Rollback to the specified version (rollback all migrations with version > target_version)
    target_migration = migrations.find { |m| m.version == target_version }

    raise StandardError, "Migration version #{target_version} not found or not applied" if target_migration.nil?

    if current_ver <= target_version
      raise StandardError, "Migration version #{target_version} not found or not applied"
    end

    migrations_to_rollback = migrations
                             .select { |m| m.version > target_version && m.version <= current_ver }
                             .sort_by(&:version)
                             .reverse

  else
    # Rollback the last migration by default
    migrations_to_rollback = migrations
                             .select { |m| m.version == current_ver }
                             .sort_by(&:version)
                             .reverse

  end
  migrations_to_rollback.each do |migration|
    run_migration(migration, :down)
  end
end

.run_migration(migration, direction) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/pg_sql_triggers/migrator.rb', line 167

def run_migration(migration, direction)
  require migration.path
  migration_class = resolve_migration_class(migration.name)

  # Capture SQL once from a single inspection instance so that both the
  # safety validator and comparator work from the same snapshot without
  # running the migration code a second time.
  captured_sql = capture_migration_sql(migration_class.new, direction)

  perform_safety_validation(captured_sql, migration)
  perform_pre_apply_comparison(captured_sql, migration)
  apply_migration(migration_class, migration, direction)
  enforce_disabled_triggers if direction == :up
rescue LoadError => e
  raise StandardError, "Error loading trigger migration #{migration.filename}: #{e.message}"
rescue StandardError => e
  raise StandardError,
        "Error running trigger migration #{migration.filename}: #{e.message}\n#{e.backtrace.first(5).join("\n")}"
end

.run_up(target_version = nil, confirmation: nil) ⇒ Object



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
121
# File 'lib/pg_sql_triggers/migrator.rb', line 87

def run_up(target_version = nil, confirmation: nil)
  # Check kill switch before running migrations
  # This provides protection when called directly from console
  # When called from rake tasks, the ENV override will already be in place
  # Use ENV["CONFIRMATION_TEXT"] if confirmation is not provided (for rake task compatibility)
  confirmation ||= ENV.fetch("CONFIRMATION_TEXT", nil)
  PgSqlTriggers::SQL::KillSwitch.check!(
    operation: :migrator_run_up,
    environment: Rails.env,
    confirmation: confirmation,
    actor: { type: "Console", id: "Migrator.run_up" }
  )

  if target_version
    # Apply a specific migration version
    migration_to_apply = migrations.find { |m| m.version == target_version }
    raise StandardError, "Migration version #{target_version} not found" if migration_to_apply.nil?

    # Check if it's already applied
    quoted_version = ActiveRecord::Base.connection.quote(target_version.to_s)
    version_exists = ActiveRecord::Base.connection.select_value(
      "SELECT 1 FROM #{MIGRATIONS_TABLE_NAME} WHERE version = #{quoted_version} LIMIT 1"
    )

    raise StandardError, "Migration version #{target_version} is already applied" if version_exists.present?

    run_migration(migration_to_apply, :up)
  else
    # Apply all pending migrations
    pending = pending_migrations
    pending.each do |migration|
      run_migration(migration, :up)
    end
  end
end

.statusObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/pg_sql_triggers/migrator.rb', line 272

def status
  ensure_migrations_table!
  current_version

  migrations.map do |migration|
    # Check if this specific migration version exists in the migrations table
    # This is more reliable than just comparing versions
    quoted_version = ActiveRecord::Base.connection.quote(migration.version.to_s)
    version_exists = ActiveRecord::Base.connection.select_value(
      "SELECT 1 FROM #{MIGRATIONS_TABLE_NAME} WHERE version = #{quoted_version} LIMIT 1"
    )
    ran = version_exists.present?

    {
      version: migration.version,
      name: migration.name,
      status: ran ? "up" : "down",
      filename: migration.filename
    }
  end
end

.versionObject



294
295
296
# File 'lib/pg_sql_triggers/migrator.rb', line 294

def version
  current_version
end