package dbutil
import (
"context"
"github.com/jackc/pgconn"
class SignupForm
include ActiveModel::Model
include ActiveModel::Attributes
attribute :account_name, :string
attribute :email, :string
package store
import (
"context"
"github.com/jackc/pgx/v5"
package store
import (
"context"
"database/sql"
)
CREATE TABLE outbox (
id BIGSERIAL PRIMARY KEY,
aggregate_type TEXT NOT NULL,
aggregate_id TEXT NOT NULL,
event_type TEXT NOT NULL,
payload JSONB NOT NULL,
class CreateInventoryReservations < ActiveRecord::Migration[7.1]
def change
create_table :inventory_items do |t|
t.string :sku, null: false
t.integer :quantity_on_hand, null: false, default: 0
t.integer :quantity_reserved, null: false, default: 0
-- Row-level locks with SELECT FOR UPDATE
BEGIN;
SELECT * FROM inventory
WHERE product_id = 123
FOR UPDATE; -- Locks selected rows
module RetryableTransaction
module_function
RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze
def run(max_retries: 3, base_delay: 0.05, &block)
from django.db import transaction
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Order, OrderItem, Product
-- Basic transaction
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
def change
create_table :email_deliveries do |t|
t.string :dedupe_key, null: false
t.string :mailer, null: false
t.string :action, null: false
class Document < ApplicationRecord
belongs_to :account
validates :source_url, presence: true
before_save :normalize_checksum