Class: Praxis::Mapper::Support::SchemaLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis-mapper/support/schema_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_root = '.', **options) ⇒ SchemaLoader

Returns a new instance of SchemaLoader.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/praxis-mapper/support/schema_loader.rb', line 11

def initialize(schema_root='.', **options)
  @schema_root = Pathname.new(schema_root)
  @options = options
  @connection_manager = ConnectionManager.new
  @repositories = Set.new

  @migrations = Hash.new

  @connection_manager.repositories.each do |repository_name, config|

    next unless config[:query] == Praxis::Mapper::Query::Sql

    migration_path = @schema_root + repository_name.to_s

    migration_path.children.each do |file|
      table = file.basename.to_s[0..-4]

      before = Sequel::Migration.descendants.clone
      require file.expand_path if file.exist?

      after = Sequel::Migration.descendants

      migration = (after - before).first

      @migrations[repository_name] ||= Array.new
      @migrations[repository_name] << [table, migration]
    end

  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/praxis-mapper/support/schema_loader.rb', line 9

def options
  @options
end

#schema_rootObject (readonly)

Returns the value of attribute schema_root.



9
10
11
# File 'lib/praxis-mapper/support/schema_loader.rb', line 9

def schema_root
  @schema_root
end

Instance Method Details

#load!Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/praxis-mapper/support/schema_loader.rb', line 43

def load!
  @migrations.each do |repository_name, migrations|
    connection = @connection_manager.checkout(repository_name)

    migrations.each do |(table, migration)|
      migration.apply(connection, :up)
    end
  end
   
end