Class: GeoserverMigrations::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/geoserver_migrations/migrator.rb

Constant Summary collapse

MigrationFilenameRegexp =
/\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/

Instance Method Summary collapse

Instance Method Details

#any_migrations?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/geoserver_migrations/migrator.rb', line 56

def any_migrations?
  migrations(migrations_paths).any?
end

#current_versionObject



48
49
50
# File 'lib/geoserver_migrations/migrator.rb', line 48

def current_version
  get_all_versions.max || 0
end

#get_all_versionsObject



40
41
42
43
44
45
46
# File 'lib/geoserver_migrations/migrator.rb', line 40

def get_all_versions
  begin
    GeoserverMigration.all_versions.map(&:to_i)
  rescue
    []
  end
end

#migrateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/geoserver_migrations/migrator.rb', line 7

def migrate
  if !any_migrations?
    puts "No migration files found!"
  else
    if !needs_migration?
      puts "There are no pending migrations."
    else
      pending_migrations.each do |migration|
        migration.assets_path = GeoserverMigrations.assets_path
        migration.migrate
        
        GeoserverMigration.set_migrated(migration)
      end
    end
  end
end

#migration_files(paths) ⇒ Object



103
104
105
# File 'lib/geoserver_migrations/migrator.rb', line 103

def migration_files(paths)
  Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end

#migrations(paths) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/geoserver_migrations/migrator.rb', line 88

def migrations(paths)
  paths = Array(paths)

  migrations = migration_files(paths).map do |file|
    version, name, scope = parse_migration_filename(file)
    raise GeoserverMigrations::IllegalMigrationNameError.new(file) unless version
    version = version.to_i
    name = name.camelize

    MigrationProxy.new(name, version, file, scope)
  end

  migrations.sort_by(&:version)
end

#migrations_pathsObject



78
79
80
81
82
# File 'lib/geoserver_migrations/migrator.rb', line 78

def migrations_paths
  @migrations_paths ||= ["geoserver/migrate"]
  # just to not break things if someone uses: migrations_path = some_string
  Array(@migrations_paths)
end

#migrations_paths=(paths) ⇒ Object



74
75
76
# File 'lib/geoserver_migrations/migrator.rb', line 74

def migrations_paths=(paths)
  @migrations_paths = paths
end

#needs_migration?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/geoserver_migrations/migrator.rb', line 52

def needs_migration?
  (migrations(migrations_paths).collect(&:version) - get_all_versions).size > 0
end

#parse_migration_filename(filename) ⇒ Object

:nodoc:



84
85
86
# File 'lib/geoserver_migrations/migrator.rb', line 84

def parse_migration_filename(filename) # :nodoc:
  File.basename(filename).scan(MigrationFilenameRegexp).first
end

#pending_migrationsObject



67
68
69
70
71
# File 'lib/geoserver_migrations/migrator.rb', line 67

def pending_migrations
  already_migrated = get_all_versions
  migrations = migrations(migrations_paths)
  migrations.reject { |m| already_migrated.include?(m.version) }
end

#rollback(steps = 1) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/geoserver_migrations/migrator.rb', line 25

def rollback(steps=1)
  if steps < 1
    puts "The nr of steps to rollback has to be at least 1"
  else
    rollback_migrations(steps).each do |migration|
      migration.assets_path = GeoserverMigrations.assets_path
      migration.migrate(:down)

      GeoserverMigration.set_reverted(migration)
    end
  end
end

#rollback_migrations(steps = 1) ⇒ Object



61
62
63
64
65
# File 'lib/geoserver_migrations/migrator.rb', line 61

def rollback_migrations(steps=1)
  to_rollback = get_all_versions[0 - steps .. -1]
  migrations = migrations(migrations_paths)
  migrations.select { |m| to_rollback.include?(m.version) }
end