Skip to content

Commit

Permalink
Merge pull request #6 from ValorWaterAnalytics/DEV-7136
Browse files Browse the repository at this point in the history
Dev 7136
  • Loading branch information
vmeow authored May 10, 2021
2 parents dbb8037 + bcf3235 commit 9edaf08
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions activerecord6-redshift-adapter.gemspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'activerecord6-redshift-adapter'
s.version = '1.2.0'
s.version = '1.2.1'
s.summary = 'Amazon Redshift adapter for ActiveRecord '
s.description = 'Amazon Redshift _makeshift_ adapter for ActiveRecord 6.'
s.license = 'MIT'

s.author = ['Nancy Foen', 'Minero Aoki', 'iamdbc', 'Quentin Rousseau']
s.author = ['Nancy Foen', 'Minero Aoki', 'iamdbc', 'Quentin Rousseau', 'vmeow']
s.email = '[email protected]'
s.homepage = 'https://github.com/kwent/activerecord6-redshift-adapter'
s.homepage = 'https://github.com/ValorWaterAnalytics/activerecord6-redshift-adapter'

s.files = Dir.glob(['LICENSE', 'README.md', 'lib/**/*.rb'])
s.require_path = 'lib'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ module ColumnMethods
# require you to assure that you always provide a UUID value before saving
# a record (as primary keys cannot be +nil+). This might be done via the
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, options = {})
def primary_key(name, type = :primary_key, **options)
return super unless type == :uuid
options[:default] = options.fetch(:default, 'uuid_generate_v4()')
options[:primary_key] = true
column name, type, options
end

def json(name, options = {})
def json(name, **options)
column(name, :json, options)
end

def jsonb(name, options = {})
def jsonb(name, **options)
column(name, :jsonb, options)
end
end

class ColumnDefinition < Struct.new(:name, :type, :options, :sql_type)
ColumnDefinition = Struct.new(:name, :type, :options, :sql_type) do
def primary_key?
options[:primary_key]
end
Expand All @@ -56,6 +56,7 @@ def primary_key?
def #{option_name}
options[:#{option_name}]
end
def #{option_name}=(value)
options[:#{option_name}] = value
end
Expand All @@ -68,8 +69,8 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition

private

def create_column_definition(*args)
Redshift::ColumnDefinition.new(*args)
def create_column_definition(name, type, options)
Redshift::ColumnDefinition.new(name, type, options)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class SchemaCreation < SchemaCreation
private

def visit_ColumnDefinition(o)
super
o.sql_type = type_to_sql(o.type, **o.options)
column_sql = +"#{quote_column_name(o.name)} #{o.sql_type}"
add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
column_sql
end

def add_column_options!(sql, options)
Expand Down Expand Up @@ -168,7 +171,7 @@ def create_table(table_name, **options)
super
end

def drop_table(table_name, options = {})
def drop_table(table_name, **options)
execute "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}#{' CASCADE' if options[:force] == :cascade}"
end

Expand Down Expand Up @@ -252,7 +255,7 @@ def create_schema schema_name
end

# Drops the schema for the given schema name.
def drop_schema(schema_name, options = {})
def drop_schema(schema_name, **options)
execute "DROP SCHEMA#{' IF EXISTS' if options[:if_exists]} #{quote_schema_name(schema_name)} CASCADE"
end

Expand Down Expand Up @@ -320,13 +323,13 @@ def rename_table(table_name, new_name)
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
end

def add_column(table_name, column_name, type, options = {}) #:nodoc:
def add_column(table_name, column_name, type, **options) #:nodoc:
clear_cache!
super
end

# Changes the column of a table.
def change_column(table_name, column_name, type, options = {})
def change_column(table_name, column_name, type, **options)
clear_cache!
quoted_table_name = quote_table_name(table_name)
sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
Expand Down Expand Up @@ -373,7 +376,7 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
end

def add_index(table_name, column_name, options = {}) #:nodoc:
def add_index(table_name, column_name, **options) #:nodoc:
end

def remove_index!(table_name, index_name) #:nodoc:
Expand Down Expand Up @@ -426,7 +429,11 @@ def index_name_length
# Maps logical Rails types to PostgreSQL-specific data types.
def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
case type.to_s
when 'integer'
when 'smallint'
return 'smallint'
when 'bigint'
return 'bigint'
when 'integer', 'int'
return 'integer' unless limit

case limit
Expand Down
24 changes: 14 additions & 10 deletions lib/active_record/connection_adapters/redshift_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ class RedshiftAdapter < AbstractAdapter
primary_key: "bigint identity primary key",
string: { name: "varchar" },
text: { name: "varchar" },
integer: { name: "integer" },
varchar: { name: "varchar" },
smallint: { name: "smallint", limit: 2 },
int: { name: "integer", limit: 4 },
integer: { name: "integer", limit: 4 },
bigint: { name: "bigint", limit: 8 },
float: { name: "float" },
decimal: { name: "decimal" },
datetime: { name: "timestamp" },
timestamptz: { name: "timestamptz" },
time: { name: "time" },
date: { name: "date" },
bigint: { name: "bigint" },
boolean: { name: "boolean" },
serial: { name: "integer" },
bigserial: { name: "bigint" },
serial: { name: "integer", limit: 4 },
bigserial: { name: "bigint", limit: 8 },
}

OID = Redshift::OID #:nodoc:
Expand Down Expand Up @@ -350,10 +354,10 @@ def get_oid_type(oid, fmod, column_name, sql_type = '') # :nodoc:
}
end

def initialize_type_map(m) # :nodoc:
register_class_with_limit m, 'int2', Type::Integer
register_class_with_limit m, 'int4', Type::Integer
register_class_with_limit m, 'int8', Type::Integer
def initialize_type_map(m = type_map) # :nodoc:
m.register_type "int2", Type::Integer.new(limit: 2)
m.register_type "int4", Type::Integer.new(limit: 4)
m.register_type "int8", Type::Integer.new(limit: 8)
m.alias_type 'oid', 'int2'
m.register_type 'float4', Type::Float.new
m.alias_type 'float8', 'float4'
Expand Down Expand Up @@ -733,8 +737,8 @@ def construct_coder(row, coder_class)
coder_class.new(oid: row["oid"].to_i, name: row["typname"])
end

def create_table_definition(*args) # :nodoc:
Redshift::TableDefinition.new(self, *args)
def create_table_definition(name, **options) # :nodoc:
Redshift::TableDefinition.new(self, name, **options)
end
end
end
Expand Down

0 comments on commit 9edaf08

Please sign in to comment.