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: [])



86
87
88
89
90
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 86

def enum(name, values: [], **)
  # Escape single quotes in enum values to prevent SQL injection
  enum_values = values.map { |v| "'#{v.to_s.gsub("'", "''")}'" }.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)



76
77
78
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 76

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
64
65
66
67
# File 'lib/active_record/connection_adapters/duckdb/schema_definitions.rb', line 60

def struct(name, fields: {}, **)
  # Quote field names as identifiers and validate field types
  field_definitions = fields.map do |field_name, field_type|
    quoted_name = %("#{field_name.to_s.gsub('"', '""')}")
    "#{quoted_name} #{field_type.to_s.upcase}"
  end
  column(name, "STRUCT(#{field_definitions.join(", ")})", **)
end