Replies: 2 comments 2 replies
-
A loop in |
Beta Was this translation helpful? Give feedback.
-
It looks as follows now: # lib/sequel/extensions/updated_at.rb
module Sequel
class Migration
module UpdatedAt
def create_updated_at_trigger_function
create_function :set_updated_at, <<-SQL, language: :plpgsql, returns: :trigger
BEGIN
NEW.updated_at := CURRENT_TIMESTAMP;
RETURN NEW;
END;
SQL
end
def drop_updated_at_trigger_function
drop_function :set_updated_at
end
def create_updated_at_trigger(table)
create_trigger(table, "#{table}_updated_at_trigger", :set_updated_at,
events: :update,
each_row: true,
when: { Sequel[:new][:updated_at] => Sequel[:old][:updated_at] }
)
end
def drop_updated_at_trigger(table)
drop_trigger(table, "#{table}_updated_at_trigger")
end
end
end
end And then the migrations (ROM which just delegates to Sequel): # 00000000000000_create_updated_at_trigger_function.rb
Sequel.extension :updated_at
ROM::SQL.migration do
up do
extend Sequel::Migration::UpdatedAt
create_updated_at_trigger_function
end
down do
extend Sequel::Migration::UpdatedAt
drop_updated_at_trigger_function
end
end # 00000000000000_create_users.rb
Sequel.extension :updated_at
ROM::SQL.migration do
up do
create_table :users do
column :id, :uuid, primary_key: true, default: Sequel.lit('uuidv7()')
column :updated_at, :timestamptz, default: Sequel::CURRENT_TIMESTAMP
column :email, :citext, null: false, unique: true, index: true
column :mobile_phone, :text, unique: true
column :first_name, :text
column :last_name, :text
end
extend Sequel::Migration::UpdatedAt
create_updated_at_trigger :users
end
down do
drop_table :users
end
end I get that for support of But what's also bugging me a little is the explicit |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to use the same trigger on most tables to automatically set
updated_at
when rows are modified. To keep things tidy, a custom addition to the migration DSL likeautomate_updated_at :mytable
might come in handy.Extending PORO style is the simple approach, but that won't work in
change do
(can't guess the down action), so I'm wondering whether there's a better way to tackle this?Beta Was this translation helpful? Give feedback.
All reactions