Class: DBGeni::Migration
- Inherits:
-
Object
- Object
- DBGeni::Migration
- Defined in:
- lib/dbgeni/migration.rb
Constant Summary collapse
- NEW =
These are all the states a migration can be in. The NEW status is never in the database, as that is the default state when it has been created as a file, but never applied to the database.
PENDING - this is the state a migration goes into before the migration is runin
and while it is running.
COMPLETED - after the migration completes, if it was successful it gets moved to this state FAILED - after the migration completes, if it failed it gets moved to this state ROLLEDBACK - if a migration has been rolledback, it goes into this state.
'New'
- PENDING =
'Pending'
- FAILED =
'Failed'
- COMPLETED =
'Completed'
- ROLLEDBACK =
'Rolledback'
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
TODO - add verified state?.
-
#error_messages ⇒ Object
readonly
TODO - add verified state?.
-
#logfile ⇒ Object
readonly
TODO - add verified state?.
-
#migration_file(dir = 'up') ⇒ Object
readonly
TODO - add verified state?.
-
#migration_type ⇒ Object
Returns the value of attribute migration_type.
-
#name ⇒ Object
readonly
TODO - add verified state?.
-
#rollback_file ⇒ Object
readonly
TODO - add verified state?.
-
#sequence ⇒ Object
readonly
TODO - add verified state?.
Class Method Summary collapse
- .filename_from_internal_name(internal_name) ⇒ Object
- .get_milestone_migration(directory, name) ⇒ Object
- .initialize_from_internal_name(directory, name) ⇒ Object
- .internal_name_from_filename(filename) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #applied?(config, connection) ⇒ Boolean
-
#apply!(config, connection, force = nil) ⇒ Object
“.
- #convert_migration(config) ⇒ Object
- #convert_rollback(config) ⇒ Object
-
#initialize(directory, migration) ⇒ Migration
constructor
A new instance of Migration.
- #rollback!(config, connection, force = nil) ⇒ Object
- #runnable_migration ⇒ Object
- #runnable_rollback ⇒ Object
- #set_completed(config, connection) ⇒ Object
- #set_failed(config, connection) ⇒ Object
- #set_pending(config, connection) ⇒ Object
- #set_rolledback(config, connection) ⇒ Object
- #status(config, connection) ⇒ Object
- #to_s ⇒ Object
- #verify!(config, connection) ⇒ Object
Constructor Details
#initialize(directory, migration) ⇒ Migration
Returns a new instance of Migration.
53 54 55 56 57 58 59 60 61 |
# File 'lib/dbgeni/migration.rb', line 53 def initialize(directory, migration) @migration_type = 'Migration' @directory = directory @migration_file = migration parse_file @rollback_file = "#{sequence}_down_#{name}.sql" @runnable_migration = nil @runnable_rollback = nil end |
Instance Attribute Details
#directory ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def directory @directory end |
#error_messages ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def @error_messages end |
#logfile ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def logfile @logfile end |
#migration_file(dir = 'up') ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def migration_file @migration_file end |
#migration_type ⇒ Object
Returns the value of attribute migration_type.
22 23 24 |
# File 'lib/dbgeni/migration.rb', line 22 def migration_type @migration_type end |
#name ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def name @name end |
#rollback_file ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def rollback_file @rollback_file end |
#sequence ⇒ Object (readonly)
TODO - add verified state?
21 22 23 |
# File 'lib/dbgeni/migration.rb', line 21 def sequence @sequence end |
Class Method Details
.filename_from_internal_name(internal_name) ⇒ Object
29 30 31 32 |
# File 'lib/dbgeni/migration.rb', line 29 def self.filename_from_internal_name(internal_name) internal_name =~ /^(\d{12})::(.+)$/ "#{$1}_up_#{$2}.sql" end |
.get_milestone_migration(directory, name) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dbgeni/migration.rb', line 38 def self.get_milestone_migration(directory, name) migration = '' begin f = File.open(File.join(directory,name), 'r') migration = f.readline.chomp rescue EOFError ensure f.close if f end unless migration =~ /^(\d{12})_(up|down)_(.+)\.sql$/ raise DBGeni::MilestoneHasNoMigration, name end migration end |
.initialize_from_internal_name(directory, name) ⇒ Object
34 35 36 |
# File 'lib/dbgeni/migration.rb', line 34 def self.initialize_from_internal_name(directory, name) self.new(directory, Migration.filename_from_internal_name(name)) end |
.internal_name_from_filename(filename) ⇒ Object
24 25 26 27 |
# File 'lib/dbgeni/migration.rb', line 24 def self.internal_name_from_filename(filename) filename =~ /^(\d{12})_(up|down)_(.+)\.sql$/ "#{$1}::#{$3}" end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/dbgeni/migration.rb', line 67 def ==(other) if other.migration_file == @migration_file and other.directory == @directory true else false end end |
#applied?(config, connection) ⇒ Boolean
75 76 77 78 |
# File 'lib/dbgeni/migration.rb', line 75 def applied?(config, connection) result = status(config, connection) result == COMPLETED ? true : false end |
#apply!(config, connection, force = nil) ⇒ Object
“
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dbgeni/migration.rb', line 91 def apply!(config, connection, force=nil) set_env(config, connection) if applied?(config, connection) and force != true raise DBGeni::MigrationAlreadyApplied, self.to_s end ensure_file_exists migrator = DBGeni::Migrator.initialize(config, connection) convert_migration(config) set_pending! begin migrator.apply(self, force) set_completed! rescue Exception => e set_failed! if e.class == DBGeni::MigratorCouldNotConnect raise e else raise DBGeni::MigrationApplyFailed, self.to_s end ensure @logfile = migrator.logfile @error_messages = migrator.migration_errors end end |
#convert_migration(config) ⇒ Object
169 170 171 |
# File 'lib/dbgeni/migration.rb', line 169 def convert_migration(config) @runnable_migration = FileConverter.convert(@directory, @migration_file, config) end |
#convert_rollback(config) ⇒ Object
173 174 175 |
# File 'lib/dbgeni/migration.rb', line 173 def convert_rollback(config) @runnable_rollback = FileConverter.convert(@directory, @rollback_file, config) end |
#rollback!(config, connection, force = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dbgeni/migration.rb', line 116 def rollback!(config, connection, force=nil) set_env(config, connection) if [NEW, ROLLEDBACK].include? status(config, connection) and force != true raise DBGeni::MigrationNotApplied, self.to_s end ensure_file_exists('down') migrator = DBGeni::Migrator.initialize(config, connection) convert_rollback(config) set_pending! begin migrator.rollback(self, force) set_rolledback!() rescue Exception => e set_failed! if e.class == DBGeni::MigratorCouldNotConnect raise e else raise DBGeni::MigrationApplyFailed, self.to_s end ensure @logfile = migrator.logfile @error_messages = migrator.migration_errors end end |
#runnable_migration ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/dbgeni/migration.rb', line 177 def runnable_migration if @runnable_migration @runnable_migration else File.join(@directory, @migration_file) end end |
#runnable_rollback ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/dbgeni/migration.rb', line 185 def runnable_rollback if @runnable_rollback @runnable_rollback else File.join(@directory, @rollback_file) end end |
#set_completed(config, connection) ⇒ Object
150 151 152 153 |
# File 'lib/dbgeni/migration.rb', line 150 def set_completed(config, connection) set_env(config, connection) set_completed! end |
#set_failed(config, connection) ⇒ Object
155 156 157 158 |
# File 'lib/dbgeni/migration.rb', line 155 def set_failed(config, connection) set_env(config, connection) set_failed! end |
#set_pending(config, connection) ⇒ Object
145 146 147 148 |
# File 'lib/dbgeni/migration.rb', line 145 def set_pending(config, connection) set_env(config, connection) set_pending! end |
#set_rolledback(config, connection) ⇒ Object
160 161 162 163 |
# File 'lib/dbgeni/migration.rb', line 160 def set_rolledback(config, connection) set_env(config, connection) set_rolledback! end |
#status(config, connection) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/dbgeni/migration.rb', line 80 def status(config, connection) set_env(config, connection) results = connection.execute("select migration_state from #{@config.db_table} where sequence_or_hash = ? and migration_name = ? and migration_type = ?", @sequence, @name, @migration_type) results.length == 1 ? results[0][0] : NEW end |
#to_s ⇒ Object
165 166 167 |
# File 'lib/dbgeni/migration.rb', line 165 def to_s "#{@sequence}::#{@name}" end |
#verify!(config, connection) ⇒ Object
142 143 |
# File 'lib/dbgeni/migration.rb', line 142 def verify!(config, connection) end |