Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active January 1, 2019 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satooshi/bfe4c01ac48a5f2b80db7cf4fba42a03 to your computer and use it in GitHub Desktop.
Save satooshi/bfe4c01ac48a5f2b80db7cf4fba42a03 to your computer and use it in GitHub Desktop.
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