Sha256: 3a50643f303565f039dfdaaeb8217c1aa82ebe0af39708674d1b9b52882e4206

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'sql/lib/table_reconcile'
require 'uri'

module Volt
  # Add reconcile! directly to the model (on the server)
  class Model
    class_attribute :reconciled
  end

  class MultiTypeException < Exception

  end

  module Sql
    class Reconcile
      attr_reader :db
      def initialize(adaptor, db)
        @adaptor = adaptor
        @db = db
      end

      # reconcile takes the database from its current state to the state defined
      # in the model classes with the field helper
      def reconcile!
        # Make sure the migrations are up to date first.
        Volt::MigrationRunner.new(@db).run

        Volt::RootModels.model_classes.each do |model_class|
          TableReconcile.new(@adaptor, @db, model_class).run
        end

        # 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


      # Called to clear the listener
      def reset!
        if defined?(@@listener) && @@listener
          @@listener.remove
          @@listener = nil
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volt-sql-0.0.3 app/sql/lib/reconcile.rb
volt-sql-0.0.2 app/sql/lib/reconcile.rb