Class: ConfigScripts::Scripts::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/config_scripts/scripts/script.rb

Overview

This class is the base class for all of the config scripts that the app will define.

Running collapse

Config collapse

Pending Scripts collapse

Creating collapse

Running collapse

Constructor Details

#initialize(timestamp) ⇒ Script

This method creates a new script.

Parameters:

  • timestamp (String)

    The timestamp that is used to uniquely indentify the script.



100
101
102
# File 'lib/config_scripts/scripts/script.rb', line 100

def initialize(timestamp)
  @timestamp = timestamp
end

Instance Attribute Details

#timestampString

The timestamp for this instance of the script.

Returns:

  • (String)


108
109
110
# File 'lib/config_scripts/scripts/script.rb', line 108

def timestamp
  @timestamp
end

Class Method Details

.filename_for_script(name) ⇒ String

This method gets the script filename for a given class.

This will convert the class name into the snake case format, and look for a filename that has a timestamp followed by that.

Parameters:

  • name (String)

    The name of the config script class, with the Config suffix.

Returns:

  • (String)

    filename The filename.



61
62
63
64
65
66
67
68
69
# File 'lib/config_scripts/scripts/script.rb', line 61

def self.filename_for_script(name)
  name_underscore = name.underscore
  paths = Dir.glob(File.join(self.script_directory, "*#{name_underscore}.rb"))
  path = paths.select do |path|
    path =~ Regexp.new("[\\d]+_#{name_underscore}.rb")
  end.first
  path = File.basename(path, ".rb") if path
  path
end

.list_pending_scriptsObject

This method prints out the names of all of the scripts that have not been run.



45
46
47
48
49
# File 'lib/config_scripts/scripts/script.rb', line 45

def self.list_pending_scripts
  self.pending_scripts.each do |filename|
    puts filename
  end
end

.pending_scriptsArray<String>

This method gets the scripts that we have not yet run.

We will return the filenames, without the extensions.

Returns:

  • (Array<String>)


24
25
26
27
28
29
30
31
# File 'lib/config_scripts/scripts/script.rb', line 24

def self.pending_scripts
  paths = Dir.glob(File.join(self.script_directory, '*.rb')).sort
  paths.collect do |path|
    filename = File.basename(path, ".rb")
    timestamp = filename[0, 14]
    ScriptHistory.script_was_run?(timestamp) ? nil : filename
  end.compact
end

.run_file(filename, direction = :up) ⇒ Object

This method runs the script with a given filename.

The file must be in the db/config_scripts directory, and should the filename should not include the ‘rb extension.

Parameters:

  • filename (String)

    The name of the file to run, without the extension.

  • direction (Symbol) (defaults to: :up)

    Whether we should run the script up or down.



81
82
83
84
85
86
87
88
89
90
# File 'lib/config_scripts/scripts/script.rb', line 81

def self.run_file(filename, direction=:up)
  path = Rails.root.join('db', 'config_scripts', "#{filename}.rb")
  require path
  timestamp = filename[0,14]
  class_name = filename[15..-1].camelize + 'Config'
  klass = nil
  klass = class_name.constantize
  puts "Running #{filename} #{direction}"
  success = klass.new(timestamp).run(direction)
end

.run_pending_scriptsTrue

This method runs all the scripts that have not yet been run.

Returns:

  • (True)


36
37
38
39
40
41
# File 'lib/config_scripts/scripts/script.rb', line 36

def self.run_pending_scripts
  self.pending_scripts.each do |filename|
    self.run_file(filename)
  end
  true
end

.script_directoryString

This method gets the directory in which the scripts will be stored.

The scripts are stored in the db/config_scripts directory under the app.

Returns:

  • (String)


13
14
15
# File 'lib/config_scripts/scripts/script.rb', line 13

def self.script_directory
  Rails.root.join('db', 'config_scripts')
end

Instance Method Details

#downObject

This method rolls back the changes for this config script.

This implementation raises an exception. Subclasses must define this method if their scripts can be rolled back.

If there are any issues rolling back the script, this method should raise an exception.



128
129
130
# File 'lib/config_scripts/scripts/script.rb', line 128

def down
  raise "Not supported"
end

#run(direction) ⇒ Object

This method runs the script in a given direction.

This will use either the up or down method. That method call will be wrapped in a transaction, so if the script raises an exception, all of its changes will be rolled back.

If the method runs successfully, this will either record a timestamp or remove a timestamp in the config_scripts table.

Parameters:

  • direction (Symbol)

    Whether we should run up or down.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/config_scripts/scripts/script.rb', line 143

def run(direction)
  ActiveRecord::Base.transaction do
    begin
      self.send(direction)
    rescue => e
      puts "Error running script for #{self.class.name}: #{e.message}"
      puts e.backtrace.first
      raise e
    end

    case(direction)
    when :up
      ScriptHistory.record_timestamp(@timestamp)
    when :down
      ScriptHistory.remove_timestamp(@timestamp)
    end
    Rails.cache.clear
  end
end

#upObject

This method performs the changes for this config script.

This implementation raises an exception. Subclasses must define this method.

If there are any issues running the script, this method should raise an exception.



117
118
119
# File 'lib/config_scripts/scripts/script.rb', line 117

def up
  raise "Not supported"
end