Sha256: b8708dc150eeef517e22c4a05bc34b6f836c26d9cc668ab3781f1514515be824

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

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) # rubocop:disable Metrics/MethodLength
        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

1 entries across 1 versions & 1 rubygems

Version Path
temporal_tables-1.0.1 lib/temporal_tables/connection_adapters/mysql_adapter.rb