Class: DiscordRDA::ActiveRecordSystem
- Inherits:
-
Object
- Object
- DiscordRDA::ActiveRecordSystem
- Defined in:
- lib/discord_rda/persistence/active_record.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #connect(database_url: nil, **config) ⇒ Object
- #connected? ⇒ Boolean
- #default_migration_paths ⇒ Object
- #disconnect ⇒ Object
-
#initialize(logger: nil) ⇒ ActiveRecordSystem
constructor
A new instance of ActiveRecordSystem.
- #migrate(paths = default_migration_paths) ⇒ Object
- #migration_context(paths = default_migration_paths) ⇒ Object
- #rollback(paths = default_migration_paths, steps: 1) ⇒ Object
Constructor Details
#initialize(logger: nil) ⇒ ActiveRecordSystem
Returns a new instance of ActiveRecordSystem.
10 11 12 13 |
# File 'lib/discord_rda/persistence/active_record.rb', line 10 def initialize(logger: nil) @logger = logger @connected = false end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/discord_rda/persistence/active_record.rb', line 8 def logger @logger end |
Instance Method Details
#connect(database_url: nil, **config) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/discord_rda/persistence/active_record.rb', line 15 def connect(database_url: nil, **config) require 'active_record' connection_config = config.dup connection_config[:url] = database_url if database_url connection_config[:url] ||= ENV['DATABASE_URL'] raise ArgumentError, 'database_url or DATABASE_URL is required' unless connection_config[:url] ::ActiveRecord::Base.establish_connection(connection_config) ::DiscordRDA::Record.connection @connected = true logger&.info('ActiveRecord connected', adapter: ::ActiveRecord::Base.connection_db_config.adapter) self end |
#connected? ⇒ Boolean
30 31 32 33 34 |
# File 'lib/discord_rda/persistence/active_record.rb', line 30 def connected? @connected && ::ActiveRecord::Base.connected? rescue StandardError false end |
#default_migration_paths ⇒ Object
58 59 60 |
# File 'lib/discord_rda/persistence/active_record.rb', line 58 def default_migration_paths ['db/migrate'] end |
#disconnect ⇒ Object
36 37 38 39 40 41 |
# File 'lib/discord_rda/persistence/active_record.rb', line 36 def disconnect return unless defined?(::ActiveRecord::Base) ::ActiveRecord::Base.connection_pool.disconnect! @connected = false end |
#migrate(paths = default_migration_paths) ⇒ Object
50 51 52 |
# File 'lib/discord_rda/persistence/active_record.rb', line 50 def migrate(paths = default_migration_paths) migration_context(paths).migrate end |
#migration_context(paths = default_migration_paths) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/discord_rda/persistence/active_record.rb', line 43 def migration_context(paths = default_migration_paths) require 'active_record' paths.each { |path| FileUtils.mkdir_p(path) } ::ActiveRecord::MigrationContext.new(paths, ::ActiveRecord::SchemaMigration) end |
#rollback(paths = default_migration_paths, steps: 1) ⇒ Object
54 55 56 |
# File 'lib/discord_rda/persistence/active_record.rb', line 54 def rollback(paths = default_migration_paths, steps: 1) migration_context(paths).rollback(steps) end |