· 8 years ago · May 29, 2018, 01:32 AM
1
2require 'rubygems'
3require 'dm-core'
4require 'dm-migrations'
5
6module Foo
7 class Bar
8 include DataMapper::Resource
9
10 PENDING = 0
11 BUILDING = 1
12 COMPLETE = 3
13
14 property :id, Serial
15 property :status, Integer, :default => PENDING
16 end
17end
18
19DataMapper::Logger.new($stdout, :debug)
20DataMapper.setup(:default, ENV['DB'] ||= "sqlite::memory:")
21DataMapper.auto_migrate!
22
23Foo::Bar.create
24
25while bar = Foo::Bar.first(:status => Foo::Bar::PENDING)
26 if bar.update(:status => Foo::Bar::BUILDING)
27 bar.update(:status => Foo::Bar::COMPLETE)
28 end
29end
30
31
32%{
33
34 ~ (0.000048) SELECT sqlite_version(*)
35 ~ (0.000069) DROP TABLE IF EXISTS "foo_bars"
36 ~ (0.000013) PRAGMA table_info("foo_bars")
37 ~ (0.000281) CREATE TABLE "foo_bars" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "status" INTEGER DEFAULT 0)
38 ~ (0.000044) INSERT INTO "foo_bars" ("status") VALUES (0)
39 ~ (0.000037) SELECT "id", "status" FROM "foo_bars" WHERE "status" = 0 ORDER BY "id" LIMIT 1
40 ~ (0.000039) UPDATE "foo_bars" SET "status" = 1 WHERE "id" = 1
41 ~ (0.000030) UPDATE "foo_bars" SET "status" = 3 WHERE "id" = 1
42 ~ (0.000030) SELECT "id", "status" FROM "foo_bars" WHERE "status" = 0 ORDER BY "id" LIMIT 1
43}