Module: Flydata::Redshift::Util

Included in:
Mysql::RedshiftTableAdapter
Defined in:
lib/flydata/command/sync.rb

Constant Summary collapse

MAX_TABLENAME_LENGTH =
127
REDSHIFT_RESERVED_WORDS =
%w[
aes128 aes256 all allowoverwrite analyse analyze and any array
as asc authorization backup between binary blanksasnull both
bytedict case cast check collate column constraint create
credentials cross current_date current_time current_timestamp
current_user current_user_id default deferrable deflate defrag
delta delta32k desc disable distinct do else emptyasnull enable
encode encrypt encryption end except explicit false for foreign
freeze from full globaldict256 globaldict64k grant group gzip having
identity ignore ilike in initially inner intersect into is isnull
join leading left like limit localtime localtimestamp lun luns
minus mostly13 mostly32 mostly8 natural new not notnull null nulls
off offline offset old on only open or order outer overlaps parallel
partition percent placing primary raw readratio recover references
rejectlog resort restore right select session_user similar some
sysdate system table tag tdes text255 text32k then to top trailing
true truncatecolumns union unique user using verbose wallet when
where with without]
REDSHIFT_RESERVED_WORDS_HASH =

Create a symbol-keyed hash for performance

REDSHIFT_RESERVED_WORDS.inject({}) {|h, word| h[word.to_sym] = true; h}
REDSHIFT_SYSTEM_COLUMNS =
%w[oid tableoid xmin cmin xmax cmax ctid]
REDSHIFT_SYSTEM_COLUMNS_HASH =
REDSHIFT_SYSTEM_COLUMNS.inject({}) {|h, word| h[word.to_sym] = true; h}

Instance Method Summary collapse

Instance Method Details

#convert_to_valid_name(key, type = :table) ⇒ Object



695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/flydata/command/sync.rb', line 695

def convert_to_valid_name(key, type = :table)
  @memo ||= { table:{}, column:{} }
  key_sym = key.to_sym
  return @memo[type][key_sym] if @memo[type][key_sym]

  name = key.downcase.gsub(/[^a-z0-9_$]/, '_')
  name = "_#{name}" if is_redshift_reserved_word?(name, type) or name =~ /^[0-9$]/
  if name.length > MAX_TABLENAME_LENGTH
    name = nil
  end
  @memo[key_sym] = name
  name
end

#is_redshift_reserved_word?(name, type = :table) ⇒ Boolean

Returns:

  • (Boolean)


709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/flydata/command/sync.rb', line 709

def is_redshift_reserved_word?(name, type = :table)
  return false unless name
  return true if REDSHIFT_RESERVED_WORDS_HASH[name.to_sym] == true

  case type
  when :table
    false
  when :column
    REDSHIFT_SYSTEM_COLUMNS_HASH[name.to_sym] == true
  else
    false
  end
end