Sha256: 65dab7a95265f6ae079c0c610a15e6bb56e8530fbc299f1d5055c369fbad0f22

Contents?: true

Size: 1.34 KB

Versions: 20

Compression:

Stored size: 1.34 KB

Contents

module Snaptime
  module MigrationHelpers
    def self.load
      ActiveRecord::ConnectionAdapters::Table.class_eval do
        include SchemaStatements::Table
      end

      ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
        include SchemaStatements::TopLevel
      end
    end

    module SchemaStatements
      module Table
        def versionize
          column :natural_id, :integer
          column :valid_from, :timestamp, precision: 3
          column :valid_to, :timestamp, precision: 3
          column :deleted, :boolean, null: false, default: 0

          index :natural_id
          index :valid_from
          index :valid_to

          index %i(natural_id valid_to), unique: true

          @base.execute %(
            ALTER TABLE "#{name.to_s.upcase}"
            ADD CONSTRAINT "#{name.to_s.upcase}_VCVD" CHECK (
              VALID_TO IS NULL OR VALID_FROM <= VALID_TO
            )
          )
        end

        def unversionize
          remove :natural_id
          remove :valid_from
          remove :valid_to
          remove :deleted
        end
      end

      module TopLevel
        def versionize_table(table_name)
          change_table table_name, &:versionize
        end

        def unversionize_table(table_name)
          change_table table_name, &:unversionize
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
snaptime-22.0.0 lib/snaptime/migration_helpers.rb
snaptime-21.0.0 lib/snaptime/migration_helpers.rb
snaptime-0.3.0 lib/snaptime/migration_helpers.rb
snaptime-0.2.0 lib/snaptime/migration_helpers.rb
snaptime-0.0.20 lib/snaptime/migration_helpers.rb
snaptime-0.0.19 lib/snaptime/migration_helpers.rb
snaptime-0.0.18 lib/snaptime/migration_helpers.rb
snaptime-0.0.17 lib/snaptime/migration_helpers.rb
snaptime-0.0.16 lib/snaptime/migration_helpers.rb
snaptime-0.0.15 lib/snaptime/migration_helpers.rb
snaptime-0.0.14 lib/snaptime/migration_helpers.rb
snaptime-0.0.13 lib/snaptime/migration_helpers.rb
snaptime-0.0.12 lib/snaptime/migration_helpers.rb
snaptime-0.0.11 lib/snaptime/migration_helpers.rb
snaptime-0.0.10 lib/snaptime/migration_helpers.rb
snaptime-0.0.9 lib/snaptime/migration_helpers.rb
snaptime-0.0.8 lib/snaptime/migration_helpers.rb
snaptime-0.0.7 lib/snaptime/migration_helpers.rb
snaptime-0.0.6 lib/snaptime/migration_helpers.rb
snaptime-0.0.1 lib/snaptime/migration_helpers.rb