Class: Migu::State

Inherits:
Object
  • Object
show all
Defined in:
lib/migu/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState



7
8
9
10
11
# File 'lib/migu/state.rb', line 7

def initialize
  Dir.mkdir("migu") unless Dir.exist?("migu")
  File.open("migu/state.sqlite3", "w") {} unless File.exist?("migu/state.sqlite3")
  @db = SQLite3::Database.new("migu/state.sqlite3")
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/migu/state.rb', line 5

def db
  @db
end

Instance Method Details

#create_tableObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/migu/state.rb', line 13

def create_table
  if db.execute("select * from sqlite_master where type = 'table' and name = 'state'").empty?
    rows = db.execute "      create table state (\n        id integer primary key autoincrement,\n        migration_name text not null,\n        defined_at datetime not null,\n        created_at datetime not null\n      );\n    SQL\n  end\nend\n"

#delete(migration_name) ⇒ Object



43
44
45
46
47
# File 'lib/migu/state.rb', line 43

def delete(migration_name)
  db.execute("delete from state where migration_name = ?", migration_name) do |row|
    return row
  end
end

#find(migration_name) ⇒ Object



49
50
51
52
53
# File 'lib/migu/state.rb', line 49

def find(migration_name)
  db.execute("select * from state where migration_name = ?", migration_name) do |row|
    return row
  end
end

#insert(migration_name, defined_at, created_at = Time.now.to_s) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/migu/state.rb', line 32

def insert(migration_name, defined_at, created_at = Time.now.to_s)
  db.execute(
    "insert into state (migration_name, defined_at, created_at) values (?, ?, ?)",
    migration_name,
    defined_at,
    created_at
  ) do |row|
    return row
  end
end

#migratedObject



55
56
57
58
59
60
61
# File 'lib/migu/state.rb', line 55

def migrated
  rows = []
  db.execute("select * from state order by defined_at asc") do |row|
    rows << row
  end
  rows
end

#reset_tableObject



26
27
28
29
30
# File 'lib/migu/state.rb', line 26

def reset_table
  unless db.execute("select * from sqlite_master where type = 'table' and name = 'state'").empty?
    db.execute("drop table state")
  end
end