File: validate_migration_timestamps

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (24 lines) | stat: -rwxr-xr-x 722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env ruby

# frozen_string_literal: true

require 'time'

MIGRATION_DIRS = %w[db/migrate db/post_migrate].freeze
VERSION_DIGITS = 14
MIGRATION_TIMESTAMP_REGEX = /\A(?<version>\d{#{VERSION_DIGITS}})_/

maximum_timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i

MIGRATION_DIRS.each do |migration_dir|
  Dir[File.join(migration_dir, '*.rb')].each do |filename|
    file_basename = File.basename(filename)
    version_match = MIGRATION_TIMESTAMP_REGEX.match(file_basename)

    raise "#{filename} has an invalid migration version" if version_match.nil?

    migration_timestamp = version_match['version'].to_i

    raise "#{filename} has a future timestamp" if migration_timestamp > maximum_timestamp
  end
end