database

python
import random


class PrimaryReplicaRouter:
    """Route reads to replicas, writes to primary."""

Django database routers for multiple databases

django python database
by Priya Sharma 1 tab
python
from django.db.models import Prefetch
from django.views.generic import ListView
from .models import Post, Comment


class PostListView(ListView):

Django select_related and prefetch_related for N+1 query optimization

django python performance
by Priya Sharma 1 tab
python
from django.db import models
from django.utils.text import slugify


class Article(models.Model):
    title = models.CharField(max_length=200)

Auto-Generate Unique Slugs in Django with a pre_save Signal and Model Method

django signals slugs
by codesnips 3 tabs
ruby
class CreateCustomerRevenueSummaries < ActiveRecord::Migration[7.0]
  def change
    create_view :customer_revenue_summaries, materialized: true, version: 1

    add_index :customer_revenue_summaries,
              :customer_id,

Database Views for Read Models

rails postgres performance
by codesnips 4 tabs
sql
-- Create basic index
CREATE INDEX idx_users_email ON users(email);

-- Unique index (enforces uniqueness)
CREATE UNIQUE INDEX idx_users_username ON users(username);

Database indexing strategies for performance

database indexing performance
by Maria Garcia 2 tabs
php
<?php

use Illuminate\Support\Facades\DB;

public function createOrder(array $items, User $user)
{

Laravel database transactions for data integrity

laravel database transactions
by Carlos Mendez 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Generating URL Slugs from Titles with a Laravel Model Observer

laravel eloquent observers
by codesnips 4 tabs
go
package feed

import (
	"encoding/base64"
	"encoding/json"
	"time"

Cursor-Based Keyset Pagination for a Postgres Feed API in Go

go postgres pagination
by codesnips 3 tabs
python
from django.db import models
from django.contrib.postgres.indexes import GinIndex


class Post(models.Model):
    title = models.CharField(max_length=200, db_index=True)

Django database index strategies

django python database
by Priya Sharma 1 tab
python
from alembic import op
import sqlalchemy as sa

revision = "20240612_add_status"
down_revision = "20240515_create_orders"
branch_labels = None

Zero-Downtime NOT NULL Column Backfill with Alembic and SQLAlchemy

alembic sqlalchemy migrations
by codesnips 2 tabs
python
from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=200)
    sku = models.CharField(max_length=50)

Django model meta options for database optimization

django python models
by Priya Sharma 1 tab
python
from django.db import models
from django.contrib.postgres.indexes import GinIndex, BTreeIndex


class Order(models.Model):
    customer = models.ForeignKey('Customer', on_delete=models.CASCADE)

Django database indexes for query performance

django python database
by Priya Sharma 1 tab