Sha256: be22cc4ef8ba2e1c8f04caf793c2ebc1dc4c02c789f65d9e1b1880b00efa3379

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

module Cardio
  module Schema
    def assume_migrated_upto_version type
      Cardio.schema_mode(type) do
        ActiveRecord::Schema.assume_migrated_upto_version(
          Cardio.schema(type), Cardio.migration_paths(type)
        )
      end
    end

    def schema_suffix type
      case type
      when :core_cards then "_core_cards"
      when :deck_cards then "_deck_cards"
      when :deck then "_deck"
      else ""
      end
    end

    def schema_mode type
      with_suffix type do
        paths = Cardio.migration_paths(type)
        yield(paths)
      end
    end

    def with_suffix type
      return yield unless (new_suffix = Cardio.schema_suffix type) &&
                          new_suffix.present?
      original_name = ActiveRecord::Base.schema_migrations_table_name
      ActiveRecord::Base.schema_migrations_table_name =
        "#{original_name}#{new_suffix}"
      ActiveRecord::SchemaMigration.table_name = "#{original_name}#{new_suffix}"
      # ActiveRecord::Base.table_name_suffix = new_suffix
      # ActiveRecord::SchemaMigration.reset_table_name
      # original_suffix = ActiveRecord::Base.table_name_suffix
      yield
      ActiveRecord::Base.schema_migrations_table_name = original_name
      ActiveRecord::SchemaMigration.table_name = original_name
      # ActiveRecord::Base.table_name_suffix = original_suffix
      # ActiveRecord::SchemaMigration.reset_table_name
    end

    def schema type=nil
      File.read(schema_stamp_path(type)).strip
    end

    def schema_stamp_path type
      root_dir = deck_migration?(type) ? root : gem_root
      stamp_dir = ENV["SCHEMA_STAMP_PATH"] || File.join(root_dir, "db")

      File.join stamp_dir, "version#{schema_suffix type}.txt"
    end

    def deck_migration? type
      type.in? %i[deck_cards deck]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
card-1.94.1 lib/cardio/schema.rb
card-1.94.0 lib/cardio/schema.rb
card-1.93.13 lib/cardio/schema.rb
card-1.93.12 lib/cardio/schema.rb
card-1.93.11 lib/cardio/schema.rb
card-1.93.10 lib/cardio/schema.rb
card-1.93.9 lib/cardio/schema.rb
card-1.93.8 lib/cardio/schema.rb
card-1.93.7 lib/cardio/schema.rb