Class: GeoserverMigrations::Migration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = self.class.name, version = nil) ⇒ Migration

Returns a new instance of Migration.



6
7
8
9
10
11
12
13
14
15
# File 'lib/geoserver_migrations/migration.rb', line 6

def initialize(name = self.class.name, version = nil)
  @name       = name
  @version    = version
  @connection = nil
  @layers_to_create = {}
  @layers_to_delete = []
  @styles_to_delete = []
  @ordered_actions_to_take = []
  @assets_path = nil
end

Instance Attribute Details

#assets_pathObject

Returns the value of attribute assets_path.



4
5
6
# File 'lib/geoserver_migrations/migration.rb', line 4

def assets_path
  @assets_path
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/geoserver_migrations/migration.rb', line 4

def name
  @name
end

#versionObject

Returns the value of attribute version.



4
5
6
# File 'lib/geoserver_migrations/migration.rb', line 4

def version
  @version
end

Instance Method Details

#add_resource(resource_name) ⇒ Object

Raises:

  • (StandardError)


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

def add_resource(resource_name)
  # translate to full file-name and raise error if not exists?

  full_resource_name = File.join(File.expand_path(self.assets_path), resource_name )

  raise StandardError.new("File #{full_resource_name} not found!") unless File.exists?(full_resource_name)

  @ordered_actions_to_take << {action: :add_resource, params: {name: full_resource_name }}
end

#announce(message) ⇒ Object



95
96
97
98
99
# File 'lib/geoserver_migrations/migration.rb', line 95

def announce(message)
  text = "#{version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end

#create_layer(layer_name, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/geoserver_migrations/migration.rb', line 50

def create_layer(layer_name, &block)
  layer_config = GeoserverMigrations::LayerConfig.new(layer_name)
  layer_config.instance_eval(&block) if block_given?
  if layer_config.valid?
    @layers_to_create[layer_name] = layer_config
    @ordered_actions_to_take << {action: :create_layer, params: {name: layer_name, layer_config: layer_config}}
  end
end

#create_layergroup(layer_name, &block) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/geoserver_migrations/migration.rb', line 59

def create_layergroup(layer_name, &block)
  layergroup_config = GeoserverMigrations::LayergroupConfig.new(layer_name)
  layergroup_config.instance_eval(&block) if block_given?
  if layergroup_config.valid?
    @layers_to_create[layer_name] = layergroup_config
    @ordered_actions_to_take << {action: :create_layergroup, params: {name: layer_name, layer_config: layergroup_config}}
  end
end

#delete_layer(layer_name) ⇒ Object



76
77
78
79
# File 'lib/geoserver_migrations/migration.rb', line 76

def delete_layer(layer_name)
  @layers_to_delete << layer_name
  @ordered_actions_to_take << {action: :delete_layer, params: {name: layer_name }}
end

#delete_layergroup(layer_name) ⇒ Object



81
82
83
84
# File 'lib/geoserver_migrations/migration.rb', line 81

def delete_layergroup(layer_name)
  @layers_to_delete << layer_name
  @ordered_actions_to_take << {action: :delete_layergroup, params: {name: layer_name }}
end

#delete_style(style_name) ⇒ Object



86
87
88
89
# File 'lib/geoserver_migrations/migration.rb', line 86

def delete_style(style_name)
  @styles_to_delete << style_name
  @ordered_actions_to_take << {action: :delete_style, params: {name: style_name }}
end

#migrate(direction = :up) ⇒ Object



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

def migrate(direction = :up)
  return unless [:up, :down].include?(direction)

  case direction
    when :up   then announce "migrating"
    when :down then announce "reverting"
  end

  time = Benchmark.measure do
    # collect configuration
    run

    # puts @layers_to_create.inspect

    # create configured layers
    GeoserverMigrations::Base.connection.execute(@ordered_actions_to_take, direction)
  end

  operation = direction == :up ? 'migrated' : 'reverted'
  announce "#{operation} (%.4fs)" % time.real; write
end

#say(message, subitem = false) ⇒ Object



101
102
103
# File 'lib/geoserver_migrations/migration.rb', line 101

def say(message, subitem = false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

#say_with_time(message) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/geoserver_migrations/migration.rb', line 105

def say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end

#update_style(layer_name, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/geoserver_migrations/migration.rb', line 68

def update_style(layer_name, &block)
  layer_config = GeoserverMigrations::LayerConfig.new(layer_name, true)
  layer_config.instance_eval(&block) if block_given?
  if layer_config.valid?
    @ordered_actions_to_take << {action: :update_style, params: {name: layer_name, layer_config: layer_config}}
  end
end

#write(text = "") ⇒ Object



91
92
93
# File 'lib/geoserver_migrations/migration.rb', line 91

def write(text = "")
  puts(text) unless Rails.env.test? 
end