Module: Everywhere::Database
- Defined in:
- lib/everywhere/database.rb
Overview
Framework-neutral database preparation for the packaged runtime: create the database if missing and bring it up to the latest migration, so a freshly installed desktop app has a working schema on first launch.
Rather than hardcode one stack, prepare! adapts to whatever ORM the app has already loaded (it runs after the app boots):
* ActiveRecord — Rails uses the multi-db-aware prepare_all; a plain
ActiveRecord app (e.g. Sinatra + sinatra-activerecord) runs its
migrations in-process.
* Sequel — runs Sequel migrations against the app's database handle.
Apps that need something else register a hook and own the whole step:
Everywhere.configure { |c| c.database { MyMigrator.run! } }
Constant Summary collapse
- MIGRATION_DIRS =
%w[db/migrate db/migrations config/db/migrate].freeze
Class Method Summary collapse
- .log(message) ⇒ Object
- .migration_paths(root) ⇒ Object
- .prepare!(root) ⇒ Object
-
.prepare_active_record(root) ⇒ Object
Rails: prepare_all handles the primary/cache/queue/cable databases.
- .prepare_sequel(root) ⇒ Object
- .rails_app? ⇒ Boolean
Class Method Details
.log(message) ⇒ Object
85 86 87 |
# File 'lib/everywhere/database.rb', line 85 def log() puts "[everywhere] #{}" end |
.migration_paths(root) ⇒ Object
76 77 78 |
# File 'lib/everywhere/database.rb', line 76 def migration_paths(root) MIGRATION_DIRS.map { |d| File.join(root, d) }.select { |p| Dir.exist?(p) } end |
.prepare!(root) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/everywhere/database.rb', line 24 def prepare!(root) if (hook = Everywhere.config.database_hook) return hook.call end if defined?(::ActiveRecord::Base) prepare_active_record(root) elsif defined?(::Sequel) prepare_sequel(root) else log "no ActiveRecord/Sequel detected — skipping database prepare" end rescue StandardError => e warn "[everywhere] database prepare failed: #{e.class}: #{e.}" raise end |
.prepare_active_record(root) ⇒ Object
Rails: prepare_all handles the primary/cache/queue/cable databases. Plain ActiveRecord: connect (sqlite creates the file on demand) and run any pending migrations found on disk.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/everywhere/database.rb', line 44 def prepare_active_record(root) if rails_app? ::ActiveRecord::Tasks::DatabaseTasks.prepare_all return end paths = migration_paths(root) return log("no migrations found — skipping") if paths.empty? # Opening the connection creates the sqlite file if it doesn't exist yet. ::ActiveRecord::Base.connection context = ::ActiveRecord::MigrationContext.new(paths) unless context.needs_migration? return log("database up to date") end log "migrating database" context.migrate end |
.prepare_sequel(root) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/everywhere/database.rb', line 64 def prepare_sequel(root) db = Everywhere.config.sequel_db db ||= ::Object.const_get(:DB) if ::Object.const_defined?(:DB) return log("no Sequel database handle (set Everywhere.config.sequel_db)") unless db paths = migration_paths(root) return log("no migrations found — skipping") if paths.empty? ::Sequel.extension :migration paths.each { |dir| ::Sequel::Migrator.run(db, dir) } end |
.rails_app? ⇒ Boolean
80 81 82 83 |
# File 'lib/everywhere/database.rb', line 80 def rails_app? defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application && defined?(::ActiveRecord::Tasks::DatabaseTasks) end |