Sha256: 87fb2808b4d532f2a4656b0bdc3896b5bbc3e078f1784b3e025f7d1ba2b9a655

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Ridgepole
  class DSLParser
    def initialize(options = {})
      @options = options
    end

    def parse(dsl, opts = {})
      definition, execute = Context.eval(dsl, opts)
      check_definition(definition)
      [definition, execute]
    end

    private

    def check_definition(definition)
      definition.each do |table_name, attrs|
        check_orphan_index(table_name, attrs)
        check_orphan_foreign_key(table_name, attrs)
        check_foreign_key_without_index(table_name, attrs)
      end
    end

    def check_orphan_index(table_name, attrs)
      raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}" if attrs[:indices] && !(attrs[:definition])
    end

    def check_orphan_foreign_key(table_name, attrs)
      raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}" if attrs[:foreign_keys] && !(attrs[:definition])
    end

    def check_foreign_key_without_index(table_name, attrs)
      return unless attrs[:foreign_keys]
      return unless attrs[:options][:options]&.include?('ENGINE=InnoDB')

      attrs[:foreign_keys].each do |_, foreign_key_attrs|
        fk_index = foreign_key_attrs[:options][:column] || "#{foreign_key_attrs[:to_table].singularize}_id"
        next if attrs[:indices]&.any? { |_k, v| v[:column_name].first == fk_index }

        Ridgepole::Logger.instance.warn(<<-MSG)
[WARNING] Table `#{table_name}` has a foreign key on `#{fk_index}` column, but doesn't have any indexes on the column.
          Although an index will be added automatically by InnoDB, please add an index explicitly for your future operations.
        MSG
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ridgepole-0.8.8 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.7 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.6 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.5 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.4 lib/ridgepole/dsl_parser.rb