Sha256: c3fb02abae49c0f2d917fa42442f3806e8a496bbc020c334ec32b29aadd7fb5a

Contents?: true

Size: 1.31 KB

Versions: 69

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module DeclareSchema
  module SchemaChange
    class TableAdd < Base
      def initialize(table_name, fields, create_table_options, sql_options: nil)
        @table_name = table_name
        fields.all? do |type, name, options|
          type.is_a?(Symbol) && name.is_a?(Symbol) && options.is_a?(Hash)
        end or raise ArgumentError, "fields must be Array(Array(Symbol, Symbol, Hash)); got #{fields.inspect}"
        @fields = fields
        @create_table_options = create_table_options
        @create_table_options = @create_table_options.merge(options: sql_options) if sql_options.present?
      end

      def up_command
        longest_field_type_length = @fields.map { |type, _name, _option| type.to_s.length }.max

        <<~EOS.strip
          create_table #{[@table_name.to_sym.inspect, *self.class.format_options(@create_table_options)].join(', ')} do |t|
          #{@fields.map do |type, name, options|
              padded_type = format("%-*s", longest_field_type_length, type)
              args = [name.inspect, *self.class.format_options(options)].join(', ')
              "  t.#{padded_type} #{args}"
            end.join("\n")}
          end
        EOS
      end

      def down_command
        "drop_table #{@table_name.to_sym.inspect}"
      end
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
declare_schema-2.3.0 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.3.0.pre.is.0 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.2.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.2.0.colin.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.2.0 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.1.0 lib/declare_schema/schema_change/table_add.rb
declare_schema-2.1.0.pre.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.4.0 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.4.0.colin.11 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.6 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.6.colin.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.5 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.5.colin.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.4.0.colin.9 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.4 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.4.colin.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.4.0.colin.8 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.3 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.3.3.colin.1 lib/declare_schema/schema_change/table_add.rb
declare_schema-1.4.0.colin.7 lib/declare_schema/schema_change/table_add.rb