Skip to content

Instantly share code, notes, and snippets.

View nickretallack's full-sized avatar

Nick Retallack nickretallack

View GitHub Profile
@nickretallack
nickretallack / env.py
Last active March 16, 2023 20:57
How to run multi-tenant migrations in alembic.
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool, MetaData, Table, ForeignKeyConstraint
from logging.config import fileConfig
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Tenant(db.Model):
__tablename__ = 'tenant'
name = db.Column(db.String, nullable=False, unique=True, primary_key=True)
__table_args__ = {"schema_name":"public"}
@nickretallack
nickretallack / gist:81730aa53ff40fc05a56
Created October 30, 2014 23:51
setting search path messes up extensions?
$ createdb testext
$ psql testext
psql (9.3.5)
Type "help" for help.
testext=# create schema things;
CREATE SCHEMA
testext=# set search_path to things,public;
SET
@nickretallack
nickretallack / add-middle-name.json
Last active August 29, 2015 14:08
Demonstrates how a simplistic JSON PATCH could miss out on an intermediate value due to a race condition.
{
"name":{
"middle":"John"
}
}
@nickretallack
nickretallack / gist:ec096a8013046c42d8f9
Created October 30, 2014 01:00
LocalProxy and Flask-SQLAlchemy don't play nicely together
from flask import Flask, Response
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug.local import LocalProxy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.config["SQLALCHEMY_ECHO"] = True
db = SQLAlchemy(app)
@nickretallack
nickretallack / gist:76bcd65cc0305abcd33b
Last active August 29, 2015 14:08
How do I override the query class in Flask-SQLAlchemy?
from flask import *
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Query
class MyQuery(Query): pass
db = SQLAlchemy(session_options={'query_cls': MyQuery})
class MyModel(db.Model):
<Routes location="history">
<Route handler={AdminLayout} flux={flux}>
<Route name="home" path="/" handler={FrontPage}/>
</Route>
<Route handler={AuthenticationLayout}>
<Route name="sign-in" path="/sign-in" handler={LoginForm} />
</Route>
</Routes>
@nickretallack
nickretallack / composite_keys.py
Last active August 29, 2015 14:07
This should work, right? This demonstrates an issue I'm having with composite primary keys.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey, Integer, String, ForeignKeyConstraint
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship
Base = declarative_base()
class Tenant(Base):
__tablename__ = 'tenant'
id = Column(Integer, primary_key=True)
$ pip install pillow
Downloading/unpacking pillow
Downloading Pillow-2.5.3-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (3.0MB): 3.0MB downloaded
Installing collected packages: pillow
Successfully installed pillow
Cleaning up...
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
defmodule Foo do
defmacro __using__(_) do
quote do
def foo() do
IO.puts "Foo"
end
defoverridable [foo: 0]
end
end
end