Skip to content

Instantly share code, notes, and snippets.

View bobfang1992's full-sized avatar
🏠
Working from home

Bob Fang bobfang1992

🏠
Working from home
View GitHub Profile
@bobfang1992
bobfang1992 / get_session.py
Created July 30, 2020 05:20
Sqlalchemy getsession
from contextlib import contextmanager
from typing import ContextManager
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session
@contextmanager
def get_seesion(engine: Engine) -> ContextManager[Session]:

Things to check:

  1. It seems when creating indeces, the sqlalchemy will prepend table name when it is a Postgres database but does not do it if it is Sqlite Figure out if this is true. Pratically this implies that for PG you can have same index name across tables but not for Sqlite. Small issue but worth checking.
@bobfang1992
bobfang1992 / Alembic for different envs
Last active July 4, 2020 08:35 — forked from twolfson/README.md
Toggling between `alembic` databases
`alembic` is great but lacks an out of the box way to set up running migrations against a specific database (e.g. `development`, `test`, `production`). The following adjustments to its `env.py` and `alembic.ini` allow us to target a specific database:
**Example:**
```bash
alembic -x db=development upgrade head
```
**env.py:**
@bobfang1992
bobfang1992 / compile-time-vector.cpp
Created July 3, 2020 21:43
compile time vector
#include <iostream>
template <int...>
struct static_vector {
};
template <int n>
struct static_vector<n> {
static constexpr int value = n;
static_vector<> rest;