Module: Ronin::Database
- Defined in:
- lib/ronin/database/database.rb,
lib/ronin/database/migrations/1.0.0.rb,
lib/ronin/database/migrations/1.1.0.rb,
lib/ronin/database/migrations/graph.rb,
lib/ronin/database/migrations/migration.rb,
lib/ronin/database/migrations/migrations.rb,
lib/ronin/database/exceptions/invalid_config.rb,
lib/ronin/database/exceptions/unknown_repository.rb,
lib/ronin/database/migrations/exceptions/unknown_migration.rb,
lib/ronin/database/migrations/exceptions/duplicate_migration.rb
Overview
Defined Under Namespace
Modules: Migrations Classes: InvalidConfig, UnknownRepository
Constant Summary collapse
- CONFIG_FILE =
Database configuration file
File.join(Config::PATH,'database.yml')
- DEFAULT_LOG_PATH =
Database log file
File.join(Config::PATH,'database.log')
- DEFAULT_LOG_LEVEL =
Database log level
:info
- DEFAULT_REPOSITORY =
Default database repository
{ :adapter => 'sqlite3', :database => File.join(Config::PATH,'database.sqlite3') }
Class Method Summary collapse
-
.clear(name) { ... } ⇒ nil
private
Clears the Database, by running destructive auto-migrations.
-
.map { ... } ⇒ Array
Performs Database transactions in each of the Database repositories.
-
.repositories ⇒ Hash{Symbol => Hash}
private
Returns the Database repositories to use.
-
.repository(name, &block) ⇒ DataMapper::Repository
Performs Database transactions within a given repository.
-
.repository?(name) ⇒ Boolean
Determines if the Database provides a specific repository.
-
.save { ... } ⇒ true
private
Saves the Database configuration to
CONFIG_FILE
. -
.setup ⇒ Object
Sets up the Database.
-
.setup?(name = :default) ⇒ Boolean
Determines if a specific database repository is setup.
-
.setup_log(options = {}) ⇒ true
private
Setup the Database log.
-
.upgrade! ⇒ Boolean
Upgrades the Database, by running migrations for a given ronin library, but only if the Database has been setup.
Class Method Details
.clear(name) { ... } ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears the Database, by running destructive auto-migrations.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/ronin/database/database.rb', line 276 def Database.clear(name) name = name.to_sym unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end DataMapper.auto_migrate!(name) yield if block_given? return nil end |
.map { ... } ⇒ Array
Performs Database transactions in each of the Database repositories.
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/ronin/database/database.rb', line 304 def Database.map results = [] repositories.each_key do |name| DataMapper.repository(name) do result = yield results << result unless result.nil? end end return results end |
.repositories ⇒ Hash{Symbol => Hash}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the Database repositories to use.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ronin/database/database.rb', line 66 def Database.repositories if @repositories.empty? @repositories[:default] = DEFAULT_REPOSITORY if File.file?(CONFIG_FILE) config = YAML.load_file(CONFIG_FILE) unless config.kind_of?(Hash) raise(InvalidConfig,"#{CONFIG_FILE} must contain a YAML Hash of repositories") end config.each do |name,uri| @repositories[name.to_sym] = uri end end end return @repositories end |
.repository(name, &block) ⇒ DataMapper::Repository
Performs Database transactions within a given repository.
247 248 249 250 251 252 253 254 255 |
# File 'lib/ronin/database/database.rb', line 247 def Database.repository(name,&block) name = name.to_sym unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end return DataMapper.repository(name,&block) end |
.repository?(name) ⇒ Boolean
Determines if the Database provides a specific repository.
99 100 101 |
# File 'lib/ronin/database/database.rb', line 99 def Database.repository?(name) repositories.has_key?(name.to_sym) end |
.save { ... } ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Saves the Database configuration to CONFIG_FILE
.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ronin/database/database.rb', line 116 def Database.save yield if block_given? File.open(CONFIG_FILE,'w') do |file| hash = {} repositories.each do |name,value| hash[name.to_sym] = value end YAML.dump(hash,file) end return true end |
.setup ⇒ Object
Sets up the Database.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ronin/database/database.rb', line 209 def Database.setup # setup the database log unless @log if ($DEBUG || ENV['DEBUG']) setup_log(:stream => $stderr, :level => :debug) else setup_log end end # setup the database repositories repositories.each do |name,uri| DataMapper.setup(name,uri) end # finalize the Models DataMapper.finalize # apply any new migrations to the database upgrade! end |
.setup?(name = :default) ⇒ Boolean
Determines if a specific database repository is setup.
178 179 180 181 182 |
# File 'lib/ronin/database/database.rb', line 178 def Database.setup?(name=:default) repository = DataMapper.repository(name) return repository.class.adapters.has_key?(repository.name) end |
.setup_log(options = {}) ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup the Database log.
158 159 160 161 162 163 164 165 |
# File 'lib/ronin/database/database.rb', line 158 def Database.setup_log(={}) path = .fetch(:path,DEFAULT_LOG_PATH) stream = .fetch(:stream,File.new(path,'w+')) level = .fetch(:level,DEFAULT_LOG_LEVEL) @log = DataMapper::Logger.new(stream,level) return true end |
.upgrade! ⇒ Boolean
Upgrades the Database, by running migrations for a given ronin library, but only if the Database has been setup.
194 195 196 197 198 199 200 |
# File 'lib/ronin/database/database.rb', line 194 def Database.upgrade! if setup? Migrations.migrate_up! else false end end |