Class: Migrake::SQLStore

Inherits:
Object
  • Object
show all
Defined in:
lib/migrake/sql_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ SQLStore

Public: Connect to the database.

db - Any valid argument to ‘Sequel.connect`.



9
10
11
# File 'lib/migrake/sql_store.rb', line 9

def initialize(db)
  @db = db.is_a?(Sequel::Database) ? db : Sequel.connect(db)
end

Instance Method Details

#allObject

Public: Load all the tasks from the store.

Returns a Set.



33
34
35
# File 'lib/migrake/sql_store.rb', line 33

def all
  Set.new(@db[:migrake_tasks].map { |row| row[:task] })
end

#prepareObject

Public: Make sure the table in which we will store tasks exists. Create it if it doesn’t.



15
16
17
18
19
# File 'lib/migrake/sql_store.rb', line 15

def prepare
  @db.create_table?(:migrake_tasks) do
    column :task, String, primary_key: true
  end
end

#put(task) ⇒ Object

Public: Insert a task into the store.

task - A string with a task’s name.

Returns nothing.



26
27
28
# File 'lib/migrake/sql_store.rb', line 26

def put(task)
  @db[:migrake_tasks].insert(task: task)
end

#write(set) ⇒ Object

Public: Write a whole set of tasks to the database, replacing what is in it.

set - A Set of tasks.

Returns nothing.



42
43
44
45
46
47
# File 'lib/migrake/sql_store.rb', line 42

def write(set)
  @db.transaction do
    @db[:migrake_tasks].truncate
    @db[:migrake_tasks].insert_multiple(set.to_a) { |task| { task: task } }
  end
end