Class: SqlMigrations::Database

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

Constant Summary collapse

SCHEMA_TABLE =
:sqlmigrations_schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sql_migrations/database.rb', line 7

def initialize(options)
  @name = options[:name] || :default
  begin
    @db = Sequel.connect(adapter:  options['adapter'],
                         host:     options['host'],
                         database: options['database'],
                         user:     options['username'],
                         password: options['password'],
                         test:     true)
  rescue
    puts "[-] Could not connect to database using #{options['adapter']} adapter"
    raise
  else
    puts "[+] Connected to database using #{options['adapter']} adapter"
  end
  install_table
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/sql_migrations/database.rb', line 5

def db
  @db
end

Instance Method Details

#execute_migrationsObject



25
26
27
28
# File 'lib/sql_migrations/database.rb', line 25

def execute_migrations
  puts "[i] Executing migrations"
  Migration.find(@name).each { |migration| migration.execute(self) }
end

#schema_datasetObject



40
41
42
# File 'lib/sql_migrations/database.rb', line 40

def schema_dataset
  @db[SCHEMA_TABLE]
end

#seed_databaseObject



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

def seed_database
  puts "[i] Seeding database"
  Seed.find(@name).each { |seed| seed.execute(self) }
end

#seed_with_fixturesObject



35
36
37
38
# File 'lib/sql_migrations/database.rb', line 35

def seed_with_fixtures
  puts "[i] Seeding test database with fixtures"
  Fixture.find(@name).each { |fixture| fixture.execute(self) }
end