Class: Volt::Sql::Reconcile

Inherits:
Object
  • Object
show all
Defined in:
app/sql/lib/reconcile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adaptor, db) ⇒ Reconcile

Returns a new instance of Reconcile.



17
18
19
20
# File 'app/sql/lib/reconcile.rb', line 17

def initialize(adaptor, db)
  @adaptor = adaptor
  @db = db
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



16
17
18
# File 'app/sql/lib/reconcile.rb', line 16

def db
  @db
end

Instance Method Details

#reconcile!Object

reconcile takes the database from its current state to the state defined in the model classes with the field helper



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/sql/lib/reconcile.rb', line 24

def reconcile!
  table_reconciles = []
  Volt::RootModels.model_classes.each do |model_class|
    table_reconciles << TableReconcile.new(@adaptor, @db, model_class)
  end

  # Make any missing tables
  table_reconciles.each(&:ensure_table)

  # Make sure the migrations are up to date first.
  Volt::MigrationRunner.new(@db).run

  # After the migrations, reconcile tables
  table_reconciles.each(&:run)

  # After the initial reconcile!, we add a listener for any new models
  # created, so we can reconcile them (in specs mostly)
  reset!
  @@listener = RootModels.on('model_created') do |model_class|
    # We do a full invalidate and wait for the next db access, because the
    # model_created gets called before the class is actually fully defined.
    # (ruby inherited limitation)
    @adaptor.invalidate_reconcile!
  end
end

#reset!Object

Called to clear the listener



52
53
54
55
56
57
# File 'app/sql/lib/reconcile.rb', line 52

def reset!
  if defined?(@@listener) && @@listener
    @@listener.remove
    @@listener = nil
  end
end