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) ⇒ Object



10
11
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
# File 'lib/temporal_tables/connection_adapters/mysql_adapter.rb', line 10

def create_temporal_triggers(table_name)
	column_names = columns(table_name).map(&:name)

	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 %{
		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 id = new.id
			and eff_to = '9999-12-31';

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

		end
	}

	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 id = old.id
			and eff_to = '9999-12-31';

		end
	}
end

#drop_temporal_triggers(table_name) ⇒ Object



4
5
6
7
8
# File 'lib/temporal_tables/connection_adapters/mysql_adapter.rb', line 4

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