Class: Pokeedex::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/pokeedex/database.rb

Overview

Class that holds the database connection and methods to connect to the database and run the migrations if needed and clean the database for testing purposes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#db_connectionObject

The database connection to use for the gem (default: /path/to/pokeedex/db/pokeedex_local.sqlite3)



13
14
15
# File 'lib/pokeedex/database.rb', line 13

def db_connection
  @db_connection
end

Class Method Details

.clean!Object

Clean the database by deleting all the rows in all the tables in the database for testing purposes (e.g. before running the tests)



36
37
38
39
40
# File 'lib/pokeedex/database.rb', line 36

def self.clean!
  connection.tables.each do |table|
    connection.from(table).delete
  end
end

.connect(config) ⇒ Object

Connect to the database with the configuration provided and set the database connection to the connection object



17
18
19
# File 'lib/pokeedex/database.rb', line 17

def self.connect(config)
  @db_connection = config.db_connection
end

.connectionObject

Return the database connection to use for the gem (default: /path/to/pokeedex/db/pokeedex_local.sqlite3)



23
24
25
# File 'lib/pokeedex/database.rb', line 23

def self.connection
  @db_connection
end

.run_migrations!Object

Run the migrations for the database connection to the database file to use for the gem (default: /path/to/pokeedex/db/pokeedex_local.sqlite3) using the migrations in the migrations folder The migrations are run in the order of the timestamp in the filename of the migration file (e.g. 001_create_pokemon_table.rb)



30
31
32
# File 'lib/pokeedex/database.rb', line 30

def self.run_migrations!
  Sequel::Migrator.run(connection, File.join(Pokeedex.root_path, 'lib', 'pokeedex', 'db', 'migrations'))
end