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:
| """ | |
| 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(): |
| """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' |
| 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) |
| 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 |
| 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')) |
I hereby claim:
To claim this, I am signing this object: