Module: TemporalTables::ConnectionAdapters::AbstractMysqlAdapter

Defined in:
lib/temporal_tables/connection_adapters/mysql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#create_temporal_triggers(table_name, primary_key) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/temporal_tables/connection_adapters/mysql_adapter.rb', line 12

def create_temporal_triggers(table_name, primary_key) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  column_names = columns(table_name).map(&:name)

  execute "drop trigger if exists #{table_name}_ai"
  execute %{
    create trigger #{table_name}_ai after insert on #{table_name}
    for each row
    begin
      set @current_time = utc_timestamp(6);

      insert into #{temporal_name(table_name)} (#{column_names.join(', ')}, eff_from)
      values (#{column_names.collect { |c| "new.#{c}" }.join(', ')}, @current_time);

    end
  }

  execute "drop trigger if exists #{table_name}_au"
  execute %{
    create trigger #{table_name}_au after update on #{table_name}
    for each row
    begin
      set @current_time = utc_timestamp(6);

      update #{temporal_name(table_name)} set eff_to = @current_time
      where #{primary_key} = new.#{primary_key}
      and eff_to = '#{TemporalTables::END_OF_TIME}';

      insert into #{temporal_name(table_name)} (#{column_names.join(', ')}, eff_from)
      values (#{column_names.collect { |c| "new.#{c}" }.join(', ')}, @current_time);

    end
  }

  execute "drop trigger if exists #{table_name}_ad"
  execute %{
    create trigger #{table_name}_ad after delete on #{table_name}
    for each row
    begin
      set @current_time = utc_timestamp(6);

      update #{temporal_name(table_name)} set eff_to = @current_time
      where #{primary_key} = old.#{primary_key}
      and eff_to = '#{TemporalTables::END_OF_TIME}';

    end
  }
end

#drop_temporal_triggers(table_name) ⇒ Object



6
7
8
9
10
# File 'lib/temporal_tables/connection_adapters/mysql_adapter.rb', line 6

def drop_temporal_triggers(table_name)
  execute "drop trigger #{table_name}_ai"
  execute "drop trigger #{table_name}_au"
  execute "drop trigger #{table_name}_ad"
end