Class: Migu::State
- Inherits:
-
Object
- Object
- Migu::State
- Defined in:
- lib/migu/state.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #create_table ⇒ Object
- #delete(migration_name) ⇒ Object
- #find(migration_name) ⇒ Object
-
#initialize ⇒ State
constructor
A new instance of State.
- #insert(migration_name, defined_at, created_at = Time.now.to_s) ⇒ Object
- #migrated ⇒ Object
- #reset_table ⇒ Object
Constructor Details
#initialize ⇒ State
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
#db ⇒ Object (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_table ⇒ Object
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 |
#migrated ⇒ Object
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_table ⇒ Object
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 |