Sha256: 3e7cd6b11be6de66febc5fd429ab3fb86a9d24aba27baadfecb63ab1696b473a

Contents?: true

Size: 1.8 KB

Versions: 19

Compression:

Stored size: 1.8 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 }
        # NOTE: For composite primary keys, the first column of the primary key is used as the foreign key index
        next if Array(attrs[:options][:primary_key]).first == fk_index

        raise "The column `#{fk_index}` of the table `#{table_name}` has a foreign key but no index. Although InnoDB creates an index automatically, please add one explicitly in order for ridgepole to manage it."
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ridgepole-1.0.5 lib/ridgepole/dsl_parser.rb
ridgepole-1.0.4 lib/ridgepole/dsl_parser.rb
ridgepole-1.0.3 lib/ridgepole/dsl_parser.rb
ridgepole-1.0.2 lib/ridgepole/dsl_parser.rb
ridgepole-1.0.2.beta lib/ridgepole/dsl_parser.rb
ridgepole-1.0.1 lib/ridgepole/dsl_parser.rb
ridgepole-1.0.0 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.6 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.5 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.4 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.3 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.2 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.1 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.0 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.13 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.0.rc1 lib/ridgepole/dsl_parser.rb
ridgepole-0.9.0.beta lib/ridgepole/dsl_parser.rb
ridgepole-0.8.12 lib/ridgepole/dsl_parser.rb
ridgepole-0.8.11 lib/ridgepole/dsl_parser.rb