-
-
Save gpaul/648189354975992a2c285143d08be88a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sqlalchemy as sa | |
from sqlalchemy.ext.declarative import declarative_base | |
import sqlalchemy.orm | |
from sqlalchemy_utils import create_database | |
from cockroachdb.sqlalchemy import run_transaction | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
logging.getLogger('sqlalchemy').setLevel(level=logging.DEBUG) | |
logging.getLogger('sqlalchemy.orm').setLevel(level=logging.DEBUG) | |
_engine = sqlalchemy.create_engine('cockroachdb://root@127.0.0.1:26257/test8', echo=False) | |
_session_factory = sqlalchemy.orm.sessionmaker(bind=_engine) | |
try: | |
create_database(_engine.url) | |
except Exception: | |
pass | |
Base = declarative_base() | |
class ConfigItem(Base): | |
__tablename__ = 'configs' | |
id = sa.Column(sa.Integer, primary_key=True) | |
key = sa.Column(sa.String, unique=True) | |
value = sa.Column(sa.String) | |
Base.metadata.create_all(_engine) | |
def callback(conn): | |
item=ConfigItem(key='some-key', value='some-value') | |
conn.add(item) | |
conn.commit() | |
run_transaction(_session_factory, callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment