Class: NandoMigrator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nando/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNandoMigrator

Returns a new instance of NandoMigrator.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nando/migrator.rb', line 16

def initialize
  @migration_table = ENV['MIGRATION_TABLE_NAME'] || 'schema_migrations'
  @migration_field = ENV['MIGRATION_TABLE_FIELD'] || 'version'
  @migration_dir   = ENV['MIGRATION_DIR'] || 'db/migrate'

  # accepts urls in the same format as dbmate => protocol://username:password@host:port/database_name
  match = /([a-zA-Z]+)\:\/\/(\w+)\:(\w+)\@([\w\.]+)\:(\d+)\/(\w+)/.match(ENV['DATABASE_URL'])

  raise Nando::GenericError.new('No .env file was found, or no valid DATABASE_URL variable was found in it') if match.nil?

  @db_protocol = match[1]
  @db_username = match[2]
  @db_password = match[3]
  @db_host = match[4]
  @db_port = match[5]
  @db_name = match[6]

  @working_dir = ENV['WORKING_DIR'] || '.'
  @schema_variable = ENV['SCHEMA_VARIABLE'] || '#{schema_name}'
end

Instance Attribute Details

#migration_dirObject

Returns the value of attribute migration_dir.



37
38
39
# File 'lib/nando/migrator.rb', line 37

def migration_dir
  @migration_dir
end

#migration_fieldObject

Returns the value of attribute migration_field.



37
38
39
# File 'lib/nando/migrator.rb', line 37

def migration_field
  @migration_field
end

#migration_tableObject

Returns the value of attribute migration_table.



37
38
39
# File 'lib/nando/migrator.rb', line 37

def migration_table
  @migration_table
end

#schema_variableObject

Returns the value of attribute schema_variable.



37
38
39
# File 'lib/nando/migrator.rb', line 37

def schema_variable
  @schema_variable
end

#working_dirObject

Returns the value of attribute working_dir.



37
38
39
# File 'lib/nando/migrator.rb', line 37

def working_dir
  @working_dir
end

Instance Method Details

#apply(options = {}, args = []) ⇒ Object

applies specific migration



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nando/migrator.rb', line 98

def apply (options = {}, args = [])
  _debug 'Applying!'

  migration_version_to_apply = args[0].to_s
  migration_files = get_migration_files(@migration_dir)

  if migration_files.length == 0
    raise Nando::GenericError.new("No migration files were found in '#{@migration_dir}'")
  end

  @db_connection = get_database_connection()
  create_schema_migrations_table_if_not_exists()
  applied_migrations = get_applied_migrations()

  migration_has_run = applied_migrations.include?(migration_version_to_apply)
  found_migration = false

  for filename in migration_files do
    migration_version, migration_name = NandoUtils.get_migration_version_and_name_from_file_path(filename)

    if migration_version.to_s != migration_version_to_apply.to_s
      next
    end

    found_migration = true
    execute_migration_method(:up, filename, migration_name, migration_version, migration_has_run)
    _debug 'There should only be 1 migration with each version, so we can break'
    break
  end

  if !found_migration
    _error "No migration file with version '#{migration_version_to_apply}' was found!"
  end
end

#baselineObject



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/nando/migrator.rb', line 170

def baseline ()
  _debug 'Creating Baseline!'

  migration_name = "baseline".underscore
  migration_timestamp = Time.now.strftime("%Y%m%d%H%M%S") # same format as ActiveRecord: year-month-day-hour-minute-second

  migration_file_name = "#{migration_timestamp}_#{migration_name}"
  migration_file_path = "#{@migration_dir}/#{migration_file_name}.rb"

  MigrationGenerator::create_baseline_file(migration_file_path, migration_name)
end

#camelize_migration_type(migration_type) ⇒ Object



303
304
305
306
307
308
309
# File 'lib/nando/migrator.rb', line 303

def camelize_migration_type (migration_type)
  camelize_migration_type = migration_type.camelize
  if !['Migration', 'MigrationWithoutTransaction'].include?(camelize_migration_type)
    raise Nando::GenericError.new("Invalid migration type '#{migration_type}'")
  end
  return camelize_migration_type
end

#create_schema_migrations_table_if_not_existsObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/nando/migrator.rb', line 316

def create_schema_migrations_table_if_not_exists
  results = @db_connection.exec("SELECT EXISTS (
    SELECT FROM information_schema.tables
     WHERE table_schema = 'public'
       AND table_name = '#{@migration_table}')")

  if results[0]["exists"] == 'f'
    _warn "Table '#{@migration_table}' does not exist, creating one"
    @db_connection.exec("CREATE TABLE public.#{@migration_table} (
      #{@migration_field}     VARCHAR(255) PRIMARY KEY,
      executed_at             timestamp DEFAULT NOW()
    )")
  end
end

#diff_schemas(options = {}, args = []) ⇒ Object



189
190
191
192
193
# File 'lib/nando/migrator.rb', line 189

def diff_schemas (options = {}, args = [])
  _debug 'Schema Diff'

  NandoSchemaDiff.diff_schemas(args[0], args[1])
end

#execute_migration_method(method, filename, migration_name, migration_version, skip_insert_version = false) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/nando/migrator.rb', line 268

def execute_migration_method (method, filename, migration_name, migration_version, skip_insert_version = false)
  if method == :up
    migrating = true
  else
    migrating = false
  end

  puts migrating ? "Applying: #{filename}" : "Reverting: #{filename}"

  require "./#{@migration_dir}/#{filename}"

  class_const = get_migration_class(migration_name)

  migration_class = class_const.new(@db_connection, migration_version)
  begin
    migration_class.execute_migration(method)
  rescue => exception
    raise Nando::GenericError.new(exception)
  end

  if !skip_insert_version
    update_migration_table(migration_version, migrating)
  else
    puts "Migration '#{migration_version}' was already in '#{@migration_table}', applying but not re-inserting it into the table"
  end
end

#get_applied_migrationsObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/nando/migrator.rb', line 238

def get_applied_migrations ()
  # run the query
  results = @db_connection.exec("SELECT * FROM #{@migration_table} ORDER BY #{@migration_field} asc")

  applied_migrations = {}
  # puts "---------------------------------"
  # puts "Applied migrations:"
  results.each{ |row|
    # puts "#{row[@migration_field]}"
    applied_migrations[row[@migration_field]] = true
  }
  # puts "---------------------------------"
  return applied_migrations
end

#get_database_connectionObject



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/nando/migrator.rb', line 331

def get_database_connection
  begin
    conn = PG::Connection.open(:host => @db_host,
                               :port => @db_port,
                               :dbname => @db_name,
                               :user => @db_username,
                               :password => @db_password)
  rescue => exception
    raise Nando::GenericError.new(exception)
  end

  return conn
end

#get_migration_class(filename) ⇒ Object



311
312
313
314
# File 'lib/nando/migrator.rb', line 311

def get_migration_class (filename)
  name = filename.camelize
  Object.const_defined?(name) ? Object.const_get(name) : Object.const_missing(name) # if the constant does not exist, raise error
end

#get_migration_files(directory) ⇒ Object




197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/nando/migrator.rb', line 197

def get_migration_files (directory)
  if !File.directory?(directory)
    raise Nando::GenericError.new("No directory '#{directory}' was found")
  end
  files = Dir.children(directory)

  migration_files = []
  for filename in files do
    if !/^(\d+)\_(.*)\.rb$/.match(filename)
      _warn "#{filename} does not have a valid migration name. Skipping!"
      next
    end

    migration_files.push(filename)
  end

  migration_files.sort! # sort to ensure the migrations are executed chronologically
end

#get_migration_files_to_rollback(directory, versions_to_rollback) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/nando/migrator.rb', line 216

def get_migration_files_to_rollback (directory, versions_to_rollback)
  if !File.directory?(directory)
    raise Nando::GenericError.new("No directory '#{directory}' was found")
  end
  files = Dir.children(directory)

  migration_files = []
  for filename in files do
    match = /^(\d+)\_(.*)\.rb$/.match(filename)
    if match.nil?
      _warn "#{filename} does not have a valid migration name. Skipping!"
      next
    end

    if versions_to_rollback.include?(match[1])
      migration_files.push(filename)
    end
  end

  migration_files.sort.reverse # sort and reverse to ensure the migrations are executed chronologically (backwards)
end

#get_migrations_to_revert(count) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/nando/migrator.rb', line 253

def get_migrations_to_revert (count)
  # run the query
  results = @db_connection.exec("SELECT * FROM #{@migration_table} ORDER BY #{@migration_field} desc LIMIT #{count}")

  migrations_to_rollback = []
  # puts "---------------------------------"
  # puts "Rollbacked migrations:"
  results.each{ |row|
    # puts "#{row[@migration_field]}"
    migrations_to_rollback.push(row[@migration_field])
  }
  # puts "---------------------------------"
  return migrations_to_rollback
end

#migrate(options = {}) ⇒ Object

migrates all missing migrations



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nando/migrator.rb', line 56

def migrate (options = {})
  _debug 'Migrating!'

  migrations_to_apply = []
  migration_files = get_migration_files(@migration_dir)

  if migration_files.length == 0
    raise Nando::GenericError.new("No migration files were found in '#{@migration_dir}'")
  end

  @db_connection = get_database_connection()
  create_schema_migrations_table_if_not_exists()
  applied_migrations = get_applied_migrations()

  for filename in migration_files do
    migration_version, migration_name = NandoUtils.get_migration_version_and_name_from_file_path(filename)

    if applied_migrations[migration_version]
      next
    end

    if options[:dry_run]
      migrations_to_apply << {:migration_version => migration_version, :migration_name => migration_name}
    else
      execute_migration_method(:up, filename, migration_name, migration_version)
    end
  end

  if options[:dry_run]
    if migrations_to_apply.count > 0
      puts "Migrations that would be applied:"
      for migration in migrations_to_apply do
        puts "=> #{migration[:migration_version]} - '#{migration[:migration_name]}'"
      end
    else
      _warn 'No migration would be applied'
    end
  end

end

#new_migration(options = {}, args = []) ⇒ Object

creates a new migration for the tool



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nando/migrator.rb', line 42

def new_migration (options = {}, args = [])
  migration_name = args[0].underscore
  migration_type = options[:type] || Nando::Migration.name.demodulize # default type is migration with transaction
  migration_timestamp = Time.now.strftime("%Y%m%d%H%M%S") # same format as ActiveRecord: year-month-day-hour-minute-second

  final_migration_type = camelize_migration_type(migration_type)

  migration_file_name = "#{migration_timestamp}_#{migration_name}"
  migration_file_path = "#{@migration_dir}/#{migration_file_name}.rb"

  MigrationGenerator::create_migration_file(migration_file_path, migration_name, final_migration_type)
end

#parse(options = {}, args = []) ⇒ Object

parses migrations from dbmate to nando



164
165
166
167
168
# File 'lib/nando/migrator.rb', line 164

def parse (options = {}, args = [])
  _debug 'Parsing!'

  NandoParser.parse_from_dbmate(args[0], args[1])
end

#rollback(options = {}) ⇒ Object

rollbacks 1 migration (or more depending on argument)



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nando/migrator.rb', line 134

def rollback (options = {})
  _debug 'Rollback!'

  rollback_count = 1 # TODO: temporary constant, add option in command interface

  @db_connection = get_database_connection()
  create_schema_migrations_table_if_not_exists()
  migrations_to_revert = get_migrations_to_revert(rollback_count)

  if migrations_to_revert.length == 0
    raise Nando::GenericError.new("There are no migrations to revert")
  end

  migration_files = get_migration_files_to_rollback(@migration_dir, migrations_to_revert)
  if migration_files.length == 0
    # TODO: this won't work as expected if we start accepting rollbacks of multiple files, since as long as 1 file is valid it will be rollbacked
    raise Nando::GenericError.new("Could not find any valid files in '#{@migration_dir}' that match the migrations to revert #{migrations_to_revert}")
  end

  for migration_index in 0...migration_files.length do
    filename = migration_files[migration_index]
    migration_version, migration_name = NandoUtils.get_migration_version_and_name_from_file_path(filename)

    execute_migration_method(:down, filename, migration_name, migration_version)
  end
end

#update_migration(options = {}, args = []) ⇒ Object



182
183
184
185
186
187
# File 'lib/nando/migrator.rb', line 182

def update_migration (options = {}, args = [])
  _debug 'Updating!'
  functions_to_add = options[:functions_to_add]

  MigrationUpdater.update_migration(args[0], @working_dir, functions_to_add)
end

#update_migration_table(version, to_apply = true) ⇒ Object



295
296
297
298
299
300
301
# File 'lib/nando/migrator.rb', line 295

def update_migration_table (version, to_apply = true)
  if to_apply
    @db_connection.exec("INSERT INTO #{@migration_table} (#{@migration_field}) VALUES (#{version})")
  else
    @db_connection.exec("DELETE FROM #{@migration_table} WHERE #{@migration_field} = '#{version}'")
  end
end