Class: SqlMigrations::Database

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

Overview

Class that represents database gem will connect to

Constant Summary collapse

HISTORY_TABLE =
:sqlmigrations_schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Database

Returns a new instance of Database.



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

def initialize(name, options)
  @name    = name
  @adapter = options[:adapter]
  begin
    @driver = self.class.connect(options)
  rescue
    puts "[-] Could not connect to `#{@name}` database using #{@adapter} adapter"
    raise
  else
    puts "[+] Connected to `#{@name}` database using #{@adapter} adapter"
  end
  install_table
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



8
9
10
# File 'lib/sql_migrations/database.rb', line 8

def driver
  @driver
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/sql_migrations/database.rb', line 8

def name
  @name
end

Instance Method Details

#historyObject



44
45
46
# File 'lib/sql_migrations/database.rb', line 44

def history
  @driver[HISTORY_TABLE]
end

#migrateObject



24
25
26
27
28
29
30
31
32
# File 'lib/sql_migrations/database.rb', line 24

def migrate
  migrations = Migration.find(@name)
  if !migrations.empty?
    puts "[i] Executing migrations for `#{@name}` database"
    migrations.each { |migration| migration.execute(self) }
  else
    puts "[i] No migrations for `#{@name}` database"
  end
end

#seedObject



34
35
36
37
38
39
40
41
42
# File 'lib/sql_migrations/database.rb', line 34

def seed
  seeds = Seed.find(@name)
  if !seeds.empty?
    puts "[i] Seeding `#{@name}` database"
    seeds.each { |seed| seed.execute(self) }
  else
    puts "[i] No seeds for `#{@name}` database"
  end
end