Class: Squasher::Config

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

Defined Under Namespace

Modules: Render

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



39
40
41
42
43
44
45
# File 'lib/squasher/config.rb', line 39

def initialize
  @root_path = Dir.pwd.freeze
  @flags = []
  @multi_db_format = nil
  @databases = []
  set_app_path(@root_path)
end

Instance Attribute Details

#databasesObject (readonly)

Returns the value of attribute databases.



37
38
39
# File 'lib/squasher/config.rb', line 37

def databases
  @databases
end

#migration_versionObject (readonly)

Returns the value of attribute migration_version.



37
38
39
# File 'lib/squasher/config.rb', line 37

def migration_version
  @migration_version
end

#multi_db_formatObject (readonly)

Returns the value of attribute multi_db_format.



37
38
39
# File 'lib/squasher/config.rb', line 37

def multi_db_format
  @multi_db_format
end

Instance Method Details

#dbconfig?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/squasher/config.rb', line 118

def dbconfig?
  !dbconfig.nil?
end

#default_migration_folderObject



114
115
116
# File 'lib/squasher/config.rb', line 114

def default_migration_folder
  File.join(@root_path, 'db', 'migrate')
end

#in_app_root(&block) ⇒ Object



142
143
144
# File 'lib/squasher/config.rb', line 142

def in_app_root(&block)
  Dir.chdir(@app_path, &block)
end

#migration_file(timestamp, migration_name, database = nil) ⇒ Object



93
94
95
# File 'lib/squasher/config.rb', line 93

def migration_file(timestamp, migration_name, database = nil)
  File.join(migrations_folder(database), "#{ timestamp }_#{ migration_name }.rb")
end

#migration_files(database = nil) ⇒ Object



89
90
91
# File 'lib/squasher/config.rb', line 89

def migration_files(database = nil)
  Dir.glob(File.join(migrations_folder(database), '**.rb'))
end

#migrations_folder(database = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/squasher/config.rb', line 97

def migrations_folder(database = nil)
  return default_migration_folder if database.nil?

  migrations_paths = dbconfig['development'][database]['migrations_paths']
  return default_migration_folder unless migrations_paths

  File.join(@app_path, migrations_paths)
end

#migrations_folders?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/squasher/config.rb', line 106

def migrations_folders?
  if @multi_db_format != 'rails'
    Dir.exist?(migrations_folder)
  else
    @databases.all? { |db| Dir.exist?(migrations_folder(db)) }
  end
end

#schema_file(database = nil) ⇒ Object



82
83
84
85
86
87
# File 'lib/squasher/config.rb', line 82

def schema_file(database = nil)
  prefix = database.nil? || database == 'primary' ? '' : "#{ database }_"
  file = set?(:sql) ? 'structure.sql' : 'schema.rb'

  File.join(@app_path, 'db', "#{ prefix }#{ file }")
end

#schema_filesObject



76
77
78
79
80
# File 'lib/squasher/config.rb', line 76

def schema_files
  return [schema_file] unless @multi_db_format == 'rails'

  @databases.map { |db| schema_file(db) }
end

#set(key, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/squasher/config.rb', line 47

def set(key, value)
  if key == :engine
    base = value.nil? ? @root_path : File.expand_path(value, @root_path)
    list = Dir.glob(File.join(base, '**', '*', 'config', 'application.rb'))
    case list.size
    when 1
      set_app_path(File.expand_path('../..', list.first))
    when 0
      Squasher.error(:cannot_find_dummy, base: base)
    else
      Squasher.error(:multi_dummy_case, base: base)
    end
  elsif key == :migration
    Squasher.error(:invalid_migration_version, value: value) unless value.to_s =~ /\A\d.\d\z/
    @migration_version = "[#{value}]"
  elsif key == :multi_db_format
    Squasher.error(:invalid_multi_db_format, value: value) unless %w[rails multiverse].include?(value)
    @multi_db_format = value
  elsif key == :databases
    @databases = value
  else
    @flags << key
  end
end

#set?(k) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/squasher/config.rb', line 72

def set?(k)
  @flags.include?(k)
end

#stub_dbconfigObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/squasher/config.rb', line 122

def stub_dbconfig
  return unless dbconfig?

  list = [dbconfig_file, *schema_files]
  list.each do |file|
    next unless File.exist?(file)
    FileUtils.mv file, "#{ file }.sq"
  end

  File.open(dbconfig_file, 'wb') { |stream| stream.write dbconfig.to_yaml }

  yield

ensure
  list.each do |file|
    next unless File.exist?("#{ file }.sq")
    FileUtils.mv "#{ file }.sq", file
  end
end