Class: SchemaFerry::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_ferry/config.rb

Defined Under Namespace

Classes: TableRule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



32
33
34
35
36
37
# File 'lib/schema_ferry/config.rb', line 32

def initialize
  @global_type_overrides = {}
  @table_rules           = Hash.new(TableRule::EMPTY)
  @ignored_tables        = []
  @enum_mode             = :string
end

Instance Attribute Details

#enum_modeObject (readonly)

Returns the value of attribute enum_mode.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def enum_mode
  @enum_mode
end

#global_type_overridesObject (readonly)

Returns the value of attribute global_type_overrides.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def global_type_overrides
  @global_type_overrides
end

#ignored_tablesObject (readonly)

Returns the value of attribute ignored_tables.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def ignored_tables
  @ignored_tables
end

#source_urlObject (readonly)

Returns the value of attribute source_url.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def source_url
  @source_url
end

#table_rulesObject (readonly)

Returns the value of attribute table_rules.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def table_rules
  @table_rules
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



5
6
7
# File 'lib/schema_ferry/config.rb', line 5

def target_url
  @target_url
end

Class Method Details

.build(&block) ⇒ Object

Ruby entry point



13
14
15
# File 'lib/schema_ferry/config.rb', line 13

def build(&block)
  evaluate { |config| config.instance_eval(&block) }
end

.load_file(path) ⇒ Object

CLI entry point



18
19
20
# File 'lib/schema_ferry/config.rb', line 18

def load_file(path)
  evaluate { |config| config.instance_eval(File.read(path), path.to_s, 1) }
end

Instance Method Details

#enum_as(mode) ⇒ Object

How to convert MySQL enum columns:

:string — plain varchar, values not enforced (default)
:check  — varchar plus a CHECK constraint restricting the values


64
65
66
67
68
69
70
71
# File 'lib/schema_ferry/config.rb', line 64

def enum_as(mode)
  modes = %i[string check]
  unless modes.include?(mode.to_sym)
    raise ConfigError, "enum_as accepts #{modes.map(&:inspect).join(" or ")}, got #{mode.inspect}"
  end

  @enum_mode = mode.to_sym
end

#ignore_table(table_name) ⇒ Object



57
58
59
# File 'lib/schema_ferry/config.rb', line 57

def ignore_table(table_name)
  @ignored_tables << table_name.to_s
end

#map_type(mysql_type, to:) ⇒ Object



47
48
49
# File 'lib/schema_ferry/config.rb', line 47

def map_type(mysql_type, to:)
  @global_type_overrides[mysql_type.to_sym] = to.to_sym
end

#source(url) ⇒ Object



39
40
41
# File 'lib/schema_ferry/config.rb', line 39

def source(url)
  @source_url = url
end

#table(table_name) ⇒ Object



51
52
53
54
55
# File 'lib/schema_ferry/config.rb', line 51

def table(table_name, &)
  rule = TableRule.new(table_name)
  rule.instance_eval(&)
  @table_rules[table_name.to_s] = rule
end

#target(url) ⇒ Object



43
44
45
# File 'lib/schema_ferry/config.rb', line 43

def target(url)
  @target_url = url
end

#validate!Object

Raises:



73
74
75
76
# File 'lib/schema_ferry/config.rb', line 73

def validate!
  raise ConfigError, 'source is not configured. Add: source "mysql2://..."' unless @source_url
  raise ConfigError, 'target is not configured. Add: target "postgresql://..."' unless @target_url
end