class AddUniqueIndexToInventorySnapshots < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_index :inventory_snapshots,
[:warehouse_id, :sku],
class Article < ApplicationRecord
scope :ranked, -> { order(score: :desc, id: :desc) }
scope :after_cursor, ->(cursor) do
return all if cursor.nil?
-- Product.active.touch_all(:cache_synced_at) for catalog 42
UPDATE "products"
SET "updated_at" = '2024-05-01 12:00:00.123456',
"cache_synced_at" = '2024-05-01 12:00:00.123456'
WHERE "products"."catalog_id" = 42
AND "products"."status" = 'active';
module KeysetPageable
extend ActiveSupport::Concern
included do
scope :keyset_page, ->(cursor: nil, per: 20) do
per = per.to_i.clamp(1, 100)
ALTER TABLE documents ADD COLUMN version BIGINT NOT NULL DEFAULT 0;
class Document < ApplicationRecord
belongs_to :account
validates :source_url, presence: true
before_save :normalize_checksum
class Order < ApplicationRecord
belongs_to :customer
scope :for_export, -> {
select(:id, :reference, :total_cents, :currency, :created_at, :customer_id)
.where.not(exported_at: nil)
class CreateSubscriptions < ActiveRecord::Migration[7.1]
STATUSES = %w[pending active past_due canceled].freeze
def change
create_table :subscriptions do |t|
t.references :account, null: false, foreign_key: true
class Category < ApplicationRecord
has_many :products, dependent: :restrict_with_error
has_many :subcategories,
class_name: "Category",
foreign_key: :parent_id,
dependent: :restrict_with_error
CREATE TABLE daily_events (
id BIGSERIAL PRIMARY KEY,
category TEXT NOT NULL,
item_id BIGINT NOT NULL,
score NUMERIC(10,2) NOT NULL DEFAULT 0,
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now()
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
# comments_count is maintained by counter_cache on Comment#belongs_to
scope :stale_comment_counts, lambda {
export type CheckResult = { name: string; status: 'up' | 'down'; durationMs: number; error?: string };
export type Check = () => Promise<void>;
function withTimeout(fn: Check, ms: number): Promise<void> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`timeout after ${ms}ms`)), ms);