Module: ActiveRecord::ConnectionAdapters::Duckdb::ColumnMethods

Extended by:
ActiveSupport::Concern, ConnectionAdapters::ColumnMethods::ClassMethods
Included in:
Table, TableDefinition
Defined in:
lib/active_record/connection_adapters/duckdb/schema_definitions.rb

Overview

DuckDB-specific column method definitions for table creation Provides both standard Rails column types and DuckDB-specific data types

Instance Method Summary collapse

Instance Method Details

#enum(name, values: []) ⇒ void

This method returns an undefined value.

Creates an ENUM column with predefined values

Examples:

Create a status enum

t.enum :status, values: ['active', 'inactive', 'pending']

Parameters:

  • name (String, Symbol)

    The column name

  • values (Array) (defaults to: [])

    Array of allowed enum values (default: [])



82
83
84
85
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 82

def enum(name, values: [], **)
  enum_values = values.map { |v| "'#{v}'" }.join(', ')
  column(name, "ENUM(#{enum_values})", **)
end

#list(name, element_type: :string) ⇒ void

This method returns an undefined value.

Creates a LIST column with specified element type

Examples:

Create a list of strings

t.list :tags, element_type: :string

Parameters:

  • name (String, Symbol)

    The column name

  • element_type (Symbol) (defaults to: :string)

    The type of elements in the list (default: :string)



50
51
52
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 50

def list(name, element_type: :string, **)
  column(name, "#{element_type.to_s.upcase}[]", **)
end

#map(name, key_type: :string, value_type: :string) ⇒ void

This method returns an undefined value.

Creates a MAP column with specified key and value types

Examples:

Create a string-to-string map

t.map :metadata, key_type: :string, value_type: :string

Parameters:

  • name (String, Symbol)

    The column name

  • key_type (Symbol) (defaults to: :string)

    The type of map keys (default: :string)

  • value_type (Symbol) (defaults to: :string)

    The type of map values (default: :string)



72
73
74
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 72

def map(name, key_type: :string, value_type: :string, **)
  column(name, "MAP(#{key_type.to_s.upcase}, #{value_type.to_s.upcase})", **)
end

#struct(name, fields: {}) ⇒ void

This method returns an undefined value.

Creates a STRUCT column with named fields

Examples:

Create an address struct

t.struct :address, fields: { street: :string, city: :string, zip: :integer }

Parameters:

  • name (String, Symbol)

    The column name

  • fields (Hash) (defaults to: {})

    Hash mapping field names to their types (default: {})



60
61
62
63
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 60

def struct(name, fields: {}, **)
  field_definitions = fields.map { |field_name, field_type| "#{field_name} #{field_type.to_s.upcase}" }
  column(name, "STRUCT(#{field_definitions.join(", ")})", **)
end