Class: DataDuck::Source

Inherits:
Database show all
Defined in:
lib/dataduck/source.rb

Direct Known Subclasses

SqlDbSource

Instance Attribute Summary

Attributes inherited from Database

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Database

#connection, #initialize, #query, #table_names

Constructor Details

This class inherits a constructor from DataDuck::Database

Class Method Details

.load_config!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dataduck/source.rb', line 5

def self.load_config!
  all_sources = DataDuck.config['sources']
  return if all_sources.nil?

  all_sources.each_key do |source_name|
    configuration = all_sources[source_name]
    source_type = configuration['type']

    if source_type == "postgresql"
      DataDuck.sources[source_name] = DataDuck::PostgresqlSource.new(source_name, configuration)
    elsif source_type == "mysql"
      DataDuck.sources[source_name] = DataDuck::MysqlSource.new(source_name, configuration)
    else
      raise ArgumentError.new("Unknown type '#{ source_type }' for source #{ source_name }.")
    end
  end
end

.only_sourceObject



43
44
45
46
47
48
49
50
# File 'lib/dataduck/source.rb', line 43

def self.only_source
  if DataDuck.sources.keys.length != 1
    raise ArgumentError.new("Must be exactly 1 source.")
  end

  source_name = DataDuck.sources.keys[0]
  return DataDuck::Source.source(source_name)
end

.skip_these_table_namesObject



60
61
62
# File 'lib/dataduck/source.rb', line 60

def self.skip_these_table_names
  [:delayed_jobs, :schema_migrations]
end

.source(name, allow_nil = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dataduck/source.rb', line 31

def self.source(name, allow_nil = false)
  name = name.to_s

  if DataDuck.sources[name]
    return DataDuck.sources[name]
  elsif allow_nil
    return nil
  else
    raise "Could not find source #{ name } in source configs."
  end
end

.source_config(name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dataduck/source.rb', line 23

def self.source_config(name)
  if DataDuck.config['sources'].nil? || DataDuck.config['sources'][name.to_s].nil?
    raise "Could not find source #{ name } in source configs."
  end

  DataDuck.config['sources'][name.to_s]
end

Instance Method Details

#escape_charObject



52
53
54
# File 'lib/dataduck/source.rb', line 52

def escape_char
  '' # implement in subclass, e.g. " in postgresql and ` in mysql
end

#schema(table_name) ⇒ Object



56
57
58
# File 'lib/dataduck/source.rb', line 56

def schema(table_name)
  self.connection.schema(table_name)
end