Sha256: d1f3722d48854840b4162e8be69c749a4ba1a12fbdec97d8ae0fe878bb63e619

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

module SchemaPlus::DefaultExpr
  module Middleware

    module Sql
      module ColumnOptions

        # Add options for default expressions
        def before(env)
          options = env.options

          return unless (default = options[:default])

          if default.is_a? Hash and [[:expr], [:value]].include?(default.keys)
            value = default[:value]
            expr = env.connection.sql_for_function(default[:expr]) || default[:expr] if default[:expr]
          else
            value = default
            expr = env.connection.sql_for_function(default)
          end

          if expr
            raise ArgumentError, "Invalid default expression" unless env.connection.default_expr_valid?(expr)
            env.sql << " DEFAULT #{expr}"
            # must explicitly check for :null to allow change_column to work on migrations
            if options[:null] == false
              env.sql << " NOT NULL"
            end
            options.delete(:default)
            options.delete(:null)
          else
            options[:default] = value
          end
        end
      end
    end

    module Dumper
      module Table

        # Emit default expression options in dump
        def after(env)
          env.connection.columns(env.table.name).each do |column|
            if !column.default_function.nil?
              if col = env.table.columns.find{|col| col.name == column.name}
                options = "default: { expr: #{column.default_function.inspect} }"
                options += ", #{col.options}" unless col.options.blank?
                col.options = options
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
schema_plus-2.0.0.pre11 lib/schema_plus/default_expr/middleware.rb
schema_plus-2.0.0.pre10 lib/schema_plus/default_expr/middleware.rb
schema_plus-2.0.0.pre9 lib/schema_plus/default_expr/middleware.rb
schema_plus-2.0.0.pre8 lib/schema_plus/default_expr/middleware.rb
schema_plus-2.0.0.pre7 lib/schema_plus/default_expr/middleware.rb
schema_plus-2.0.0.pre6 lib/schema_plus/default_expr/middleware.rb