Skip to content

Instantly share code, notes, and snippets.

@jasonwalkeryung
jasonwalkeryung / conftest.py
Last active October 1, 2018 16:09
Testing with rollback and multiple dbs
"""
Unit test fixture for testing with read replicas as described in:
https://gist.github.com/jasonwalkeryung/5133383d66782461cdc3b4607ae35d98
"""
import pytest
from sqlalchemy_replica import db
@pytest.fixture
def rollback():
@jasonwalkeryung
jasonwalkeryung / sqlalchemy_replica.py
Last active June 29, 2023 06:34
SQLAlchemy read replica wrapper
"""This is not the full code. We do a lot of stuff to clean up connections, particularly for unit testing."""
import sqlalchemy
from sqlalchemy.orm import Query, Session, scoped_session, sessionmaker
CONFIG_KEY_SQLALCHEMY_BINDS = 'SQLALCHEMY_BINDS'
CONFIG_KEY_SQLALCHEMY_RO_BINDS = 'SQLALCHEMY_READ_ONLY_BINDS'
class Config:
# These default values are for testing. In a deployed environment, they would be three separate instances.
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/branded_dev'
@jasonwalkeryung
jasonwalkeryung / reverse_string.sql
Created February 9, 2018 22:33
EXPLAIN like reverse('%.example.com')
psql=> explain analyze select distinct users.id, users.name, users.email from users where reverse(users.email) like reverse('%example.com');
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=54001.19..54170.10 rows=16891 width=71) (actual time=0.114..0.154 rows=25 loops=1)
Group Key: id, name, email
-> Bitmap Heap Scan on users (cost=845.69..53874.51 rows=16891 width=71) (actual time=0.036..0.092 rows=25 loops=1)
Filter: (reverse((email)::text) ~~ 'moc.elpmaxe%'::text)
Heap Blocks: exact=25
-> Bitmap Index Scan on idx_users_email_reverse (cost=0.00..841.46 rows=16891 width=0) (actual time=0.023..0.023 rows=25 loops=1)
Index Cond: ((reverse((email)::text) ~>=~ 'moc.elpmaxe'::text) AND (reverse((email)::text)
@jasonwalkeryung
jasonwalkeryung / openended_like.sql
Last active February 9, 2018 22:30
EXPLAIN like '%example'
psql=> explain analyze select distinct users.id, users.name, users.email from users where users.email ilike '%example%';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Unique (cost=290618.12..290621.50 rows=338 width=71) (actual time=5458.523..5458.560 rows=41 loops=1)
-> Sort (cost=290618.12..290618.97 rows=338 width=71) (actual time=5458.521..5458.539 rows=41 loops=1)
Sort Key: id, name, email
Sort Method: quicksort Memory: 28kB
-> Seq Scan on users (cost=0.00..290603.92 rows=338 width=71) (actual time=97.851..5458.380 rows=41 loops=1)
Filter: ((email)::text ~~* '%example%'::text)
Rows Removed by Filter: 3378716
@jasonwalkeryung
jasonwalkeryung / product.py
Last active December 5, 2017 20:35
Eager loading
class Product(ExampleModel):
name = Column(String, nullable=False)
description = Column(Text)
list_price = Column(Numeric)
sell_price = Column(Numeric)
variants = relationship('Variant', backref="product")
class Variant(ExampleModel):
product_id = Column(ForeignKey('products.id'))

Keybase proof

I hereby claim:

  • I am jasonwalkeryung on github.
  • I am jasonwalkeryung (https://keybase.io/jasonwalkeryung) on keybase.
  • I have a public key ASBV0DZBlZ-vte-AuUa7IUetss2SpYGUZJU8p6r8p9Im0go

To claim this, I am signing this object: