Sha256: e6bb17d83459ba749d335ee67b37d713b627fdc512c16c9049facd97672ce3d8

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module TemporalTables
  module ConnectionAdapters
    module AbstractMysqlAdapter
      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

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
temporal_tables-0.8.1 lib/temporal_tables/connection_adapters/mysql_adapter.rb
temporal_tables-0.8.0 lib/temporal_tables/connection_adapters/mysql_adapter.rb
temporal_tables-0.7.1 lib/temporal_tables/connection_adapters/mysql_adapter.rb
temporal_tables-0.7.0 lib/temporal_tables/connection_adapters/mysql_adapter.rb