Module: Charming::Generators::MigrationTimestamp

Defined in:
lib/charming/generators/migration_timestamp.rb

Overview

MigrationTimestamp produces ActiveRecord-format migration version numbers (YYYYMMDDHHMMSS) that are guaranteed unique within a ‘db/migrate` directory: when generators run within the same second, the version is bumped one second past the highest existing migration version.

Class Method Summary collapse

Class Method Details

.highest_existing(migrate_dir) ⇒ Object

The highest version prefix among existing migration files, or nil.



22
23
24
25
26
# File 'lib/charming/generators/migration_timestamp.rb', line 22

def highest_existing(migrate_dir)
  Dir.glob(File.join(migrate_dir, "*.rb"))
    .filter_map { |path| File.basename(path)[/\A\d{14}/] }
    .max
end

.next(migrate_dir) ⇒ Object

Returns the next available version string for migrate_dir.



13
14
15
16
17
18
19
# File 'lib/charming/generators/migration_timestamp.rb', line 13

def next(migrate_dir)
  now = Time.now.utc.strftime("%Y%m%d%H%M%S")
  highest = highest_existing(migrate_dir)
  return now unless highest && highest >= now

  (highest.to_i + 1).to_s
end