Class: Groovy::Schema
- Inherits:
-
Object
- Object
- Groovy::Schema
- 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
-
#index_columns ⇒ Object
readonly
Returns the value of attribute index_columns.
Instance Method Summary collapse
- #attribute_columns ⇒ Object
- #boolean_columns ⇒ Object
- #column(name, type, options = {}) ⇒ Object
- #column_names ⇒ Object
- #columns_by_type(type) ⇒ Object
-
#initialize(context, table_name, opts = {}) ⇒ Schema
constructor
A new instance of Schema.
- #integer_columns ⇒ Object
- #many(name, table_name = nil, options = {}) ⇒ Object
- #one(name, table_name = nil, options = {}) ⇒ Object
- #plural_references ⇒ Object
- #rebuild! ⇒ Object
- #reference(name, table_name = nil, options = {}) ⇒ Object
- #refs_by_name ⇒ Object
- #refs_by_table ⇒ Object
- #reload ⇒ Object
- #reverse_reference_of(ref_name) ⇒ Object
-
#search_table ⇒ Object
def [](key) # @spec table.columns.select { |c| c.column? && c.name.to_s == “##table_name.#keykey.to_s” } end.
- #singular_references ⇒ Object
- #sync(db_context = nil) ⇒ Object
- #table ⇒ Object
- #time_columns ⇒ Object
- #timestamps(opts = {}) ⇒ Object
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_columns ⇒ Object (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_columns ⇒ Object
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_columns ⇒ Object
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, = {}) # groonga_type = Types.map(type) groonga_type = type @index_columns.push(name) if .delete(:index) @spec[name.to_sym] = { type: groonga_type, args: [COLUMN_DEFAULTS.merge()] } end |
#column_names ⇒ Object
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_columns ⇒ Object
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, = {}) reference(name, table_name, .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, = {}) reference(name, table_name, ) end |
#plural_references ⇒ Object
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, = {}) 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, ] } end |
#refs_by_name ⇒ Object
81 82 83 |
# File 'lib/groovy/schema.rb', line 81 def refs_by_name @refs_by_name ||= {} end |
#refs_by_table ⇒ Object
77 78 79 |
# File 'lib/groovy/schema.rb', line 77 def refs_by_table @refs_by_table ||= {} end |
#reload ⇒ Object
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_table ⇒ Object
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_references ⇒ Object
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 |
#table ⇒ Object
32 33 34 |
# File 'lib/groovy/schema.rb', line 32 def table @table ||= context[table_name] end |
#time_columns ⇒ Object
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 (opts = {}) column(:created_at, 'time') column(:updated_at, 'time') end |