Class: Groovy::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/groovy/schema.rb

Constant Summary collapse

SEARCH_TABLE_NAME =
'Terms'.freeze
COLUMN_DEFAULTS =
{
  compress: :zstandard
}.freeze
TYPES =
{
  'string' => 'short_text',
  'text' => 'text',
  'float' => 'float',
  # 'Bool' => 'boolean',
  'boolean' => 'boolean',
  'integer' => 'int32',
  'big_integer' => 'int64',
  'date' => 'date',
  'time' => 'time',
  'datetime' => 'time'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, table_name, opts = {}) ⇒ Schema

Returns a new instance of Schema.



26
27
28
29
30
# File 'lib/groovy/schema.rb', line 26

def initialize(context, table_name, opts = {})
  @context, @table_name, @opts = context, table_name, opts || {}
  @spec, @index_columns = {}, []
  @cache = {}
end

Instance Attribute Details

#index_columnsObject (readonly)

Returns the value of attribute index_columns.



24
25
26
# File 'lib/groovy/schema.rb', line 24

def index_columns
  @index_columns
end

Instance Method Details

#attribute_columnsObject



57
58
59
# File 'lib/groovy/schema.rb', line 57

def attribute_columns
  @cache[:attribute_columns] ||= get_names(table.columns.select { |c| c.column? && !c.reference_column? && !c.vector? })
end

#boolean_columnsObject



69
70
71
# File 'lib/groovy/schema.rb', line 69

def boolean_columns
  @cache[:boolean_columns] ||= columns_by_type('Bool')
end

#column(name, type, options = {}) ⇒ Object



104
105
106
107
108
109
# File 'lib/groovy/schema.rb', line 104

def column(name, type, options = {})
  # groonga_type = Types.map(type)
  groonga_type = type
  @index_columns.push(name) if options.delete(:index)
  @spec[name.to_sym] = { type: groonga_type, args: [COLUMN_DEFAULTS.merge(options)] }
end

#column_namesObject



45
46
47
# File 'lib/groovy/schema.rb', line 45

def column_names
  @cache[:column_names] ||= get_names(table.columns)
end

#columns_by_type(type) ⇒ Object



73
74
75
# File 'lib/groovy/schema.rb', line 73

def columns_by_type(type)
  get_names(table.columns.select { |c| c.column? && c.range.name == type })
end

#integer_columnsObject



65
66
67
# File 'lib/groovy/schema.rb', line 65

def integer_columns
  @cache[:integer_columns] ||= columns_by_type('Int32')
end

#many(name, table_name = nil, options = {}) ⇒ Object



134
135
136
# File 'lib/groovy/schema.rb', line 134

def many(name, table_name = nil, options = {})
  reference(name, table_name, options.merge(type: :vector))
end

#one(name, table_name = nil, options = {}) ⇒ Object



130
131
132
# File 'lib/groovy/schema.rb', line 130

def one(name, table_name = nil, options = {})
  reference(name, table_name, options)
end

#plural_referencesObject



53
54
55
# File 'lib/groovy/schema.rb', line 53

def plural_references
  @cache[:plural_references] ||= get_names(table.columns.select(&:vector?))
end

#rebuild!Object



97
98
99
100
101
102
# File 'lib/groovy/schema.rb', line 97

def rebuild!
  log("Rebuilding!")
  # remove_table! if table
  remove_columns_not_in([])
  sync
end

#reference(name, table_name = nil, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/groovy/schema.rb', line 117

def reference(name, table_name = nil, options = {})
  table_name = "#{name}s" if table_name.nil?

  unless context[table_name]
    log "Table #{table_name} doesn't exist yet! Creating now..."
    create_table!(table_name)
  end

  refs_by_table[table_name] = name.to_sym
  refs_by_name[name.to_sym] = table_name
  @spec[name.to_sym] = { type: :reference, args: [table_name, options] }
end

#refs_by_nameObject



81
82
83
# File 'lib/groovy/schema.rb', line 81

def refs_by_name
  @refs_by_name ||= {}
end

#refs_by_tableObject



77
78
79
# File 'lib/groovy/schema.rb', line 77

def refs_by_table
  @refs_by_table ||= {}
end

#reloadObject



93
94
95
# File 'lib/groovy/schema.rb', line 93

def reload
  @cache = {}
end

#reverse_reference_of(ref_name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/groovy/schema.rb', line 85

def reverse_reference_of(ref_name)
  if ref_table = refs_by_name[ref_name.to_sym]
    if foreign_model = Groovy.models[ref_table]
      foreign_model.schema.refs_by_table[@table_name]
    end
  end
end

#search_tableObject

def [](key)

# @spec[key.to_sym]
table.columns.select { |c| c.column? && c.name.to_s == "#{table_name}.#{key.to_s}" }

end



41
42
43
# File 'lib/groovy/schema.rb', line 41

def search_table
  @cache[:search_table] ||= context[SEARCH_TABLE_NAME]
end

#singular_referencesObject



49
50
51
# File 'lib/groovy/schema.rb', line 49

def singular_references
  @cache[:singular_references] ||= get_names(table.columns.select(&:reference_column?).reject(&:vector?))
end

#sync(db_context = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/groovy/schema.rb', line 143

def sync(db_context = nil)
  switch_to_context(db_context) if db_context

  ensure_created!
  remove_columns_not_in(@spec.keys)

  @spec.each do |col, spec|
    check_and_add_column(col, spec[:type], spec[:args])
  end

  @index_columns.each do |col|
    add_index_on(col)
  end

  reload
  self
end

#tableObject



32
33
34
# File 'lib/groovy/schema.rb', line 32

def table
  @table ||= context[table_name]
end

#time_columnsObject



61
62
63
# File 'lib/groovy/schema.rb', line 61

def time_columns
  @cache[:time_columns] ||= columns_by_type('Time')
end

#timestamps(opts = {}) ⇒ Object



138
139
140
141
# File 'lib/groovy/schema.rb', line 138

def timestamps(opts = {})
  column(:created_at, 'time')
  column(:updated_at, 'time')
end