class Schemaless::Table

Appreciate the irony

A Table, how much information! How much entropy!

Constants

Diffs
Schema

Attributes

current[RW]
fields[RW]
indexes[RW]
model[RW]
name[RW]
opts[RW]
proposed[RW]

Public Class Methods

new(m) click to toggle source
# File lib/schemaless/table.rb, line 35
def initialize(m)
  @model = m
  @name  = m.table_name
  @proposed = Schema.new(m.schemaless_fields, m.schemaless_indexes)
  set_table
end

Public Instance Methods

add_table!() click to toggle source

Creates tables

# File lib/schemaless/table.rb, line 80
def add_table!
  puts "++ Create table '#{name}' for '#{model}'"
  return if Schemaless.sandbox
  ::ActiveRecord::Migration.create_table(name, *opts)
  ::ActiveRecord::Base.clear_cache!
  set_table
end
del_table!() click to toggle source
# File lib/schemaless/table.rb, line 88
def del_table!
  puts "-- Remove table '#{name}' for '#{model}'"
  return if Schemaless.sandbox
  ::ActiveRecord::Migration.drop_table(name)
  set_table
end
exists?() click to toggle source
# File lib/schemaless/table.rb, line 68
def exists?
  # Crazy, does not work: !table.table_exists?
  ::ActiveRecord::Base.connection.tables.include?(name)
end
migrate?() click to toggle source
# File lib/schemaless/table.rb, line 64
def migrate?
  fields.migrate? || indexes.migrate?
end
run!() click to toggle source

Selects what needs to be done for fields.

# File lib/schemaless/table.rb, line 53
def run!
  puts "+-+-+ Schemaless #{self}"
  add_table! unless exists?
  # Order matter here
  (indexes.change + fields.change).each { |f| f.change!(self) }
  (indexes.remove + fields.remove).each { |f| f.remove!(self) }
  (fields.add + indexes.add).each { |f| f.add!(self) }

  model.reset_column_information
end
set_table() click to toggle source
# File lib/schemaless/table.rb, line 42
def set_table
  if exists?
    @current = Schema.new(model.current_attributes, model.current_indexes)
  end
  @fields  = Diffs.new(@proposed.fields, @current.try(:fields))
  @indexes = Diffs.new(@proposed.indexes, @current.try(:indexes))
end
to_s() click to toggle source
# File lib/schemaless/table.rb, line 73
def to_s
  name
end