Class: EnableAutoUpdateForUpdatedAtColumns

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/mysql/search/templates/db/migrate/enable_auto_update_of_updated_at.rb

Overview

This migration enables automatic updates for ‘updated_at` columns in all tables that have this column. It changes the column type to `DATETIME ON UPDATE CURRENT_TIMESTAMP`, which allows MySQL to automatically update the `updated_at` timestamp whenever the row is updated.

Instance Method Summary collapse

Instance Method Details

#downObject



15
16
17
18
19
20
21
# File 'lib/generators/mysql/search/templates/db/migrate/enable_auto_update_of_updated_at.rb', line 15

def down
  ActiveRecord::Base.connection.tables.each do |table_name|
    next unless column_exists?(table_name, :updated_at)

    change_column table_name, :updated_at, :datetime
  end
end

#upObject



7
8
9
10
11
12
13
# File 'lib/generators/mysql/search/templates/db/migrate/enable_auto_update_of_updated_at.rb', line 7

def up
  ActiveRecord::Base.connection.tables.each do |table_name|
    next unless column_exists?(table_name, :updated_at)

    change_column table_name, :updated_at, 'DATETIME ON UPDATE CURRENT_TIMESTAMP'
  end
end