Module: FactDb::Database
- Defined in:
- lib/fact_db/database.rb
Overview
Database management module for FactDb
Provides class methods for establishing database connections, running migrations, and managing database lifecycle (create, drop, reset).
Class Method Summary collapse
-
.connected? ⇒ Boolean
Checks if a database connection is established.
-
.create! ⇒ void
Creates the database.
-
.drop! ⇒ void
Drops the database.
-
.establish_connection!(config = FactDb.config) ⇒ void
Establishes an ActiveRecord database connection.
-
.migrate! ⇒ void
Runs all pending migrations.
-
.reset! ⇒ void
Drops, creates, and migrates the database.
-
.rollback!(steps = 1) ⇒ void
Rolls back migrations.
-
.schema_version ⇒ Integer
Returns the current schema version.
Class Method Details
.connected? ⇒ Boolean
Checks if a database connection is established
37 38 39 |
# File 'lib/fact_db/database.rb', line 37 def connected? ActiveRecord::Base.connected? end |
.create! ⇒ void
This method returns an undefined value.
Creates the database
Connects to postgres maintenance database and creates the configured database.
60 61 62 63 64 65 |
# File 'lib/fact_db/database.rb', line 60 def create! db_name = FactDb.config.database.name ActiveRecord::Base.establish_connection(maintenance_database_url) ActiveRecord::Base.connection.create_database(db_name) puts "Created database '#{db_name}'" end |
.drop! ⇒ void
This method returns an undefined value.
Drops the database
Disconnects from the current database, connects to postgres maintenance database, and drops the configured database.
47 48 49 50 51 52 53 |
# File 'lib/fact_db/database.rb', line 47 def drop! db_name = FactDb.config.database.name ActiveRecord::Base.connection.disconnect! if connected? ActiveRecord::Base.establish_connection(maintenance_database_url) ActiveRecord::Base.connection.drop_database(db_name) puts "Dropped database '#{db_name}'" end |
.establish_connection!(config = FactDb.config) ⇒ void
This method returns an undefined value.
Establishes an ActiveRecord database connection
Uses configuration from FactDb.config by default. Sets up the logger if configured.
28 29 30 31 32 |
# File 'lib/fact_db/database.rb', line 28 def establish_connection!(config = FactDb.config) # config.database is a ConfigSection - convert to AR-compatible hash ActiveRecord::Base.establish_connection(ar_connection_hash(config.database)) ActiveRecord::Base.logger = config.logger if config.logger end |
.migrate! ⇒ void
This method returns an undefined value.
Runs all pending migrations
Establishes connection if needed and runs migrations from db/migrate.
72 73 74 75 76 |
# File 'lib/fact_db/database.rb', line 72 def migrate! establish_connection! migrations_path = File.("../../db/migrate", __dir__) ActiveRecord::MigrationContext.new(migrations_path).migrate end |
.reset! ⇒ void
This method returns an undefined value.
Drops, creates, and migrates the database
Convenience method to completely reset the database to a clean state. Ignores errors when dropping (database may not exist).
94 95 96 97 98 |
# File 'lib/fact_db/database.rb', line 94 def reset! drop! rescue nil create! migrate! end |
.rollback!(steps = 1) ⇒ void
This method returns an undefined value.
Rolls back migrations
82 83 84 85 86 |
# File 'lib/fact_db/database.rb', line 82 def rollback!(steps = 1) establish_connection! unless connected? migrations_path = File.("../../db/migrate", __dir__) ActiveRecord::MigrationContext.new(migrations_path).rollback(steps) end |
.schema_version ⇒ Integer
Returns the current schema version
103 104 105 106 |
# File 'lib/fact_db/database.rb', line 103 def schema_version establish_connection! unless connected? ActiveRecord::SchemaMigration.all.map(&:version).max || 0 end |