Last active
January 1, 2019 12:52
-
-
Save satooshi/bfe4c01ac48a5f2b80db7cf4fba42a03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "pg" | |
DB.open "postgres://postgres:postgres@localhost:5432/blog_development" do |db| | |
db.transaction do |tx| | |
conn = tx.connection | |
pp conn.scalar "select count(*) from articles" # => 5 | |
pp conn.exec "insert into articles (title, url, markdown, created_at, updated_at) values ('title', 'url', 'markdown', now(), now())" | |
pp db.scalar "select count(*) from articles" # => 5 (because this established another connection) | |
pp conn.scalar "select count(*) from articles" # => 6 | |
tx.rollback | |
pp conn.scalar "select count(*) from articles" # => 5 (rollbacked properly) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment