Class: ConfigScripts::Scripts::Script
- Inherits:
-
Object
- Object
- ConfigScripts::Scripts::Script
- 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
-
#timestamp ⇒ String
The timestamp for this instance of the script.
Config collapse
-
.script_directory ⇒ String
This method gets the directory in which the scripts will be stored.
Pending Scripts collapse
-
.filename_for_script(name) ⇒ String
This method gets the script filename for a given class.
-
.list_pending_scripts ⇒ Object
This method prints out the names of all of the scripts that have not been run.
-
.pending_scripts ⇒ Array<String>
This method gets the scripts that we have not yet run.
-
.run_file(filename, direction = :up) ⇒ Object
This method runs the script with a given filename.
-
.run_pending_scripts ⇒ True
This method runs all the scripts that have not yet been run.
Creating collapse
-
#initialize(timestamp) ⇒ Script
constructor
This method creates a new script.
Running collapse
-
#down ⇒ Object
This method rolls back the changes for this config script.
-
#run(direction) ⇒ Object
This method runs the script in a given direction.
-
#up ⇒ Object
This method performs the changes for this config script.
Constructor Details
#initialize(timestamp) ⇒ Script
This method creates a new script.
100 101 102 |
# File 'lib/config_scripts/scripts/script.rb', line 100 def initialize() @timestamp = end |
Instance Attribute Details
#timestamp ⇒ String
The timestamp for this instance of the script.
108 109 110 |
# File 'lib/config_scripts/scripts/script.rb', line 108 def @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.
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_scripts ⇒ Object
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_scripts ⇒ Array<String>
This method gets the scripts that we have not yet run.
We will return the filenames, without the extensions.
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") = filename[0, 14] ScriptHistory.script_was_run?() ? 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.
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 = filename[0,14] class_name = filename[15..-1].camelize + 'Config' klass = nil klass = class_name.constantize puts "Running #{filename} #{direction}" success = klass.new().run(direction) end |
.run_pending_scripts ⇒ True
This method runs all the scripts that have not yet been run.
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_directory ⇒ String
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.
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
#down ⇒ Object
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.
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.}" puts e.backtrace.first raise e end case(direction) when :up ScriptHistory.(@timestamp) when :down ScriptHistory.(@timestamp) end Rails.cache.clear end end |
#up ⇒ Object
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 |