Class: SchemaFerry::DSL::Config

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

Constant Summary collapse

ENUM_MODES =
%i[string check].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @global_type_overrides = {}
  @table_rules           = {}
  @ignored_tables        = []
  @enum_mode             = :string
end

Instance Attribute Details

#enum_modeObject (readonly)

Returns the value of attribute enum_mode.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def enum_mode
  @enum_mode
end

#global_type_overridesObject (readonly)

Returns the value of attribute global_type_overrides.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def global_type_overrides
  @global_type_overrides
end

#ignored_tablesObject (readonly)

Returns the value of attribute ignored_tables.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def ignored_tables
  @ignored_tables
end

#source_urlObject (readonly)

Returns the value of attribute source_url.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def source_url
  @source_url
end

#table_rulesObject (readonly)

Returns the value of attribute table_rules.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def table_rules
  @table_rules
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



8
9
10
# File 'lib/schema_ferry/dsl/config.rb', line 8

def target_url
  @target_url
end

Class Method Details

.build(&block) ⇒ Object

Evaluates a definition block and returns a validated Config.



15
16
17
18
19
20
# File 'lib/schema_ferry/dsl/config.rb', line 15

def self.build(&block)
  new.tap do |config|
    config.instance_eval(&block)
    config.validate!
  end
end

.load_file(path) ⇒ Object

Evaluates a definition file (the DSL body, without the SchemaFerry.define wrapper) and returns a validated Config.



24
25
26
27
28
29
# File 'lib/schema_ferry/dsl/config.rb', line 24

def self.load_file(path)
  new.tap do |config|
    config.instance_eval(File.read(path), path.to_s, 1)
    config.validate!
  end
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


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

def enum_as(mode)
  unless ENUM_MODES.include?(mode.to_sym)
    raise ConfigError, "enum_as accepts #{ENUM_MODES.map(&:inspect).join(" or ")}, got #{mode.inspect}"
  end

  @enum_mode = mode.to_sym
end

#ignore_table(table_name) ⇒ Object



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

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

#map_type(mysql_type, to:) ⇒ Object



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

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

#source(url) ⇒ Object



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

def source(url)
  @source_url = url
end

#table(table_name) ⇒ Object



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

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

#target(url) ⇒ Object



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

def target(url)
  @target_url = url
end

#validate!Object

Raises:



71
72
73
74
# File 'lib/schema_ferry/dsl/config.rb', line 71

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