Class: Praxis::Mapper::Support::SchemaDumper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SchemaDumper.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 9

def initialize(schema_root='.', **options)
  @schema_root = Pathname.new(schema_root)

  @options = options
  @connection_manager = ConnectionManager.new

  @repositories = Hash.new do |hash, repository_name|
    hash[repository_name] = Set.new
  end

  setup!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 7

def options
  @options
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



7
8
9
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 7

def repositories
  @repositories
end

#schema_rootObject (readonly)

Returns the value of attribute schema_root.



7
8
9
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 7

def schema_root
  @schema_root
end

Instance Method Details

#dump!(repository_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 44

def dump!(repository_name)
  connection = @connection_manager.checkout(repository_name)
  connection.extension :schema_dumper

  tables = self.repositories.fetch repository_name

  FileUtils.mkdir_p(schema_root + repository_name.to_s)

  tables.each do |table|
    File.open(schema_root + repository_name.to_s + "#{table}.rb" ,"w+") do |file|
      file.puts "Sequel.migration do"
      file.puts "  up do"
      file.puts connection.dump_table_schema(table).gsub(/^/o, '    ')
      file.puts "\n"
      file.puts "  end"
      file.puts "end"
    end
  end
end

#dump_all!Object



38
39
40
41
42
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 38

def dump_all!
  repositories.each do |repository_name, tables|
    self.dump!(repository_name)
  end
end

#setup!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/praxis-mapper/support/schema_dumper.rb', line 22

def setup!
  @connection_manager.repositories.each do |repository_name, config|
    next unless config[:query] == Praxis::Mapper::Query::Sql

    models = Praxis::Mapper::Model.descendants.
      select { |model| model.repository_name == repository_name }.
      select { |model| model.table_name }

    models.each do |model|
      table = model.table_name
      repository = model.repository_name
      self.repositories[repository] << table
    end
  end
end