Module: Hiiro::DB
- Defined in:
- lib/hiiro/db.rb
Defined Under Namespace
Modules: JSON
Constant Summary collapse
- MODELS =
[]
- DB_FILE =
Hiiro::Config.path('hiiro.db')
- ALL_IMPORTERS =
%i[todos links branches prs pinned_prs tasks assignments apps projects pane_homes pins reminders tags].freeze
Class Method Summary collapse
- .connection ⇒ Object (also: db)
- .disable_dual_write! ⇒ Object
- .dual_write? ⇒ Boolean
- .migrated? ⇒ Boolean
- .register(cls) ⇒ Object
-
.remigrate!(only: nil) ⇒ Object
Re-run import for specific tables (or all if none specified).
- .setup! ⇒ Object
Class Method Details
.connection ⇒ Object Also known as: db
16 17 18 19 20 21 22 23 24 |
# File 'lib/hiiro/db.rb', line 16 def connection @connection ||= begin url = ENV.fetch('HIIRO_TEST_DB', "sqlite://#{DB_FILE}") db = Sequel.connect(url, logger: nil) db.run('PRAGMA journal_mode=WAL') Sequel::Model.db = db db end end |
.disable_dual_write! ⇒ Object
70 71 72 |
# File 'lib/hiiro/db.rb', line 70 def disable_dual_write! connection[:schema_migrations].insert_conflict.insert(name: 'full_migration', ran_at: Time.now.iso8601) end |
.dual_write? ⇒ Boolean
64 65 66 67 68 |
# File 'lib/hiiro/db.rb', line 64 def dual_write? !connection[:schema_migrations].where(name: 'full_migration').count.positive? rescue true # safe default: keep writing YAML if DB is unavailable end |
.migrated? ⇒ Boolean
58 59 60 61 62 |
# File 'lib/hiiro/db.rb', line 58 def migrated? connection[:schema_migrations].count > 0 rescue Sequel::DatabaseError false end |
.register(cls) ⇒ Object
12 13 14 |
# File 'lib/hiiro/db.rb', line 12 def register(cls) MODELS << cls end |
.remigrate!(only: nil) ⇒ Object
Re-run import for specific tables (or all if none specified). Useful after a migration that partially failed. The YAML source files must still exist (they are only renamed to .bak on successful import).
79 80 81 82 83 84 |
# File 'lib/hiiro/db.rb', line 79 def remigrate!(only: nil) base = Hiiro::Config::BASE_DIR targets = only ? Array(only).map(&:to_sym) & ALL_IMPORTERS : ALL_IMPORTERS targets.each { |name| send(:"import_#{name}", base) } puts "Remigration complete: #{targets.join(', ')}" end |
.setup! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hiiro/db.rb', line 27 def setup! return if @setup_done @setup_done = true conn = connection conn.create_table?(:schema_migrations) do String :name, primary_key: true String :ran_at end MODELS.each do |cls| cls.create_table!(conn) # Run model-specific migrations (e.g., adding columns to existing tables) cls.migrate!(conn) if cls.respond_to?(:migrate!) # Clear all schema-related caches on the model and its anonymous Sequel # parent class. When require_valid_table=false and the table didn't exist # at class-definition time, Sequel caches empty results for @db_schema, # @columns, and @setter_methods. Re-invoking get_db_schema after tables # are created re-introspects and re-defines column getter/setter methods. [cls, cls.superclass].each do |klass| klass.instance_variable_set(:@db_schema, nil) klass.instance_variable_set(:@columns, nil) klass.instance_variable_set(:@setter_methods, nil) end cls.send(:get_db_schema) end migrate_yaml! unless migrated? || ENV['HIIRO_TEST_DB'] end |