postgres

ruby
class AddSlugToPosts < ActiveRecord::Migration[7.1]
  def change
    add_column :posts, :slug, :string, null: false
    add_index :posts, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by codesnips 4 tabs
ruby
namespace :cleanup do
  desc "Enqueue a job to purge expired sessions"
  task expired_sessions: :environment do
    job = ExpiredSessionCleanupJob.perform_later
    Rails.logger.info("[cleanup:expired_sessions] enqueued job #{job.job_id}")
  end

Recurring Cleanup with a Rake Task and an Idempotent Active Job in Rails

rails background-jobs active-job
by codesnips 4 tabs
ruby
class AddSoftDeleteToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :deleted_at, :datetime

    add_index :users, :deleted_at, where: "deleted_at IS NULL", name: "index_users_on_live"

Soft-Delete with a Default Scope, Restore Action, and Unique Index Guard in Rails

rails activerecord soft-delete
by codesnips 3 tabs
ruby
class AddSlugToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :slug, :string
    add_index :articles, :slug, unique: true

    reversible do |dir|

Auto-Generating URL Slugs in Rails with a before_validation Callback and Friendly Finder

rails activerecord slugs
by codesnips 4 tabs
ruby
class Article < ApplicationRecord
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  scope :published, -> { where.not(published_at: nil) }

Filtering a Listing by Tags with a has_many :through Scope and a Query Object

rails activerecord has-many-through
by codesnips 3 tabs
ruby
module QueryBudget
  class Counter
    IGNORED = %w[SCHEMA CACHE TRANSACTION].freeze

    attr_reader :count

Per-Request Query Budget (Detect Runaway Pages)

rails performance observability
by codesnips 4 tabs
ruby
class AddSearchVectorToArticles < ActiveRecord::Migration[7.1]
  def up
    execute <<~SQL
      ALTER TABLE articles
      ADD COLUMN search_vector tsvector
      GENERATED ALWAYS AS (

Multi-Column Full Text Search with tsvector

rails postgres search
by codesnips 3 tabs
sql
CREATE TABLE events (
    id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    source      text        NOT NULL,
    external_id text        NOT NULL,
    payload     jsonb       NOT NULL,
    occurred_at timestamptz NOT NULL,

Batched writes with COPY (conceptual)

postgres performance sql
by codesnips 3 tabs
ruby
class Task < ApplicationRecord
  POSITION_GAP = 1024

  belongs_to :board

  scope :ordered, -> { order(:position) }

Reorder a list server-side and reflect instantly with Turbo Streams

rails hotwire turbo
by codesnips 4 tabs
ruby
class Reminder < ApplicationRecord
  belongs_to :user

  validates :time_zone, inclusion: { in: ActiveSupport::TimeZone::MAPPING.values }
  validates :local_time, presence: true

Time Zone Safe Scheduling

rails timezone scheduling
by codesnips 3 tabs
go
package store

import (
  "context"

  "github.com/jackc/pgx/v5"

Row-level locking with SELECT ... FOR UPDATE in a transaction

go postgres transactions
by Leah Thompson 1 tab
javascript
class BatchLoader {
  constructor(batchFn, { cacheKeyFn = (k) => k } = {}) {
    this.batchFn = batchFn;
    this.cacheKeyFn = cacheKeyFn;
    this.cache = new Map();
    this.queue = [];

Coalescing Concurrent Reads with a DataLoader-Style Batch Loader in Node

dataloader batching graphql
by codesnips 3 tabs