Class: Nandi::Config

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

Constant Summary collapse

DEFAULT_COMPILE_FILES =
"all"
DEFAULT_LOCKFILE_DIRECTORY =
File.join(Dir.pwd, "db")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer: Renderers::ActiveRecord) ⇒ Config

Returns a new instance of Config.



36
37
38
39
40
41
42
# File 'lib/nandi/config.rb', line 36

def initialize(renderer: Renderers::ActiveRecord)
  @renderer = renderer
  @custom_methods = {}
  @compile_files = DEFAULT_COMPILE_FILES
  @lockfile_directory = DEFAULT_LOCKFILE_DIRECTORY
  @migration_modifiers = [MigrationModifiers::CreateTableValidatesFks]
end

Instance Attribute Details

#compile_filesString

The files to compile when the compile generator is run. Default: ‘all` May be one of the following:

  • ‘all’ compiles all files

  • ‘git-diff’ only files changed since last commit

  • a full or partial version timestamp, eg ‘20190101010101’, ‘20190101’

  • a timestamp range , eg ‘>=20190101010101’

Returns:

  • (String)


26
27
28
# File 'lib/nandi/config.rb', line 26

def compile_files
  @compile_files
end

#custom_methodsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/nandi/config.rb', line 34

def custom_methods
  @custom_methods
end

#lockfile_directory=(value) ⇒ String

Directory where .nandilock.yml will be stored Defaults to project root

Returns:

  • (String)


31
32
33
# File 'lib/nandi/config.rb', line 31

def lockfile_directory=(value)
  @lockfile_directory = value
end

#migration_modifiersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/nandi/config.rb', line 34

def migration_modifiers
  @migration_modifiers
end

#post_processorObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/nandi/config.rb', line 34

def post_processor
  @post_processor
end

#rendererClass

The rendering backend used to produce output. The only supported option at current is Nandi::Renderers::ActiveRecord, which produces ActiveRecord migrations.

Returns:

  • (Class)


17
18
19
# File 'lib/nandi/config.rb', line 17

def renderer
  @renderer
end

Instance Method Details

#access_exclusive_lock_timeout(database_name = nil) ⇒ Object



81
# File 'lib/nandi/config.rb', line 81

def access_exclusive_lock_timeout(database_name = nil) = config(database_name).access_exclusive_lock_timeout

#access_exclusive_lock_timeout_max(database_name = nil) ⇒ Object



82
# File 'lib/nandi/config.rb', line 82

def access_exclusive_lock_timeout_max(database_name = nil) = config(database_name).access_exclusive_lock_timeout_max

#access_exclusive_statement_timeout(database_name = nil) ⇒ Object



83
# File 'lib/nandi/config.rb', line 83

def access_exclusive_statement_timeout(database_name = nil) = config(database_name).access_exclusive_statement_timeout

#access_exclusive_statement_timeout_max(database_name = nil) ⇒ Object



84
# File 'lib/nandi/config.rb', line 84

def access_exclusive_statement_timeout_max(database_name = nil) = config(database_name).access_exclusive_statement_timeout_max

#concurrent_lock_timeout(database_name = nil) ⇒ Object



87
# File 'lib/nandi/config.rb', line 87

def concurrent_lock_timeout(database_name = nil) = config(database_name).concurrent_lock_timeout

#concurrent_lock_timeout_min(database_name = nil) ⇒ Object



85
# File 'lib/nandi/config.rb', line 85

def concurrent_lock_timeout_min(database_name = nil) = config(database_name).concurrent_lock_timeout_min

#concurrent_statement_timeout(database_name = nil) ⇒ Object



88
89
# File 'lib/nandi/config.rb', line 88

def concurrent_statement_timeout(database_name = nil) = config(database_name).concurrent_statement_timeout
# rubocop:enable Layout/LineLength

#concurrent_statement_timeout_min(database_name = nil) ⇒ Object



86
# File 'lib/nandi/config.rb', line 86

def concurrent_statement_timeout_min(database_name = nil) = config(database_name).concurrent_statement_timeout_min

#databasesObject



108
109
110
111
112
# File 'lib/nandi/config.rb', line 108

def databases
  # If we've never registered any databases, use a single database with
  # default values for backwards compatibility.
  @multi_db_config.nil? ? single_db_config : @multi_db_config
end

#lockfile_path(database_name = nil) ⇒ Object



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

def lockfile_path(database_name = nil)
  File.join(lockfile_directory, databases.config(database_name).lockfile_name)
end

#migration_directory(database_name = nil) ⇒ Object

Explicitly define getters for backwards compatibility when the database isnt specified. rubocop:disable Layout/LineLength



79
# File 'lib/nandi/config.rb', line 79

def migration_directory(database_name = nil) = config(database_name).migration_directory

#output_directory(database_name = nil) ⇒ Object



80
# File 'lib/nandi/config.rb', line 80

def output_directory(database_name = nil) = config(database_name).output_directory

#post_process {|migration| ... } ⇒ Object

Register a block to be called on output, for example a code formatter. Whatever is returned will be written to the output file.

Yield Parameters:

  • migration (string)

    The text of a compiled migration.



51
52
53
# File 'lib/nandi/config.rb', line 51

def post_process(&block)
  @post_processor = block
end

#register_database(name, config = {}) ⇒ Object

Register a database to compile migrations for.



69
70
71
# File 'lib/nandi/config.rb', line 69

def register_database(name, config = {})
  multi_db_config.register(name, config)
end

#register_method(name, klass) ⇒ Object

Register a custom DDL method.

Parameters:

  • name (Symbol)

    The name of the method to create. This will be monkey-patched into Nandi::Migration.

  • klass (Class)

    The class to initialise with the arguments to the method. It should define a ‘template` instance method which will return a subclass of Cell::ViewModel from the Cells templating library and a `procedure` method that returns the name of the method. It may optionally define a `mixins` method, which will return an array of `Module`s to be mixed into any migration that uses this method.



64
65
66
# File 'lib/nandi/config.rb', line 64

def register_method(name, klass)
  custom_methods[name] = klass
end

#register_migration_modifier(klass) ⇒ Object



44
45
46
# File 'lib/nandi/config.rb', line 44

def register_migration_modifier(klass)
  @migration_modifiers << klass
end

#validate!Object



114
115
116
117
118
119
120
# File 'lib/nandi/config.rb', line 114

def validate!
  if @single_db_config && @multi_db_config
    raise ArgumentError, "Cannot use multi and single database config. Config setters are now deprecated, " \
                         "use only `register_database(name, config)` to configure Nandi."
  end
  databases.validate!
end