예제 #1
0
    def build_zip_map():
        config = configparser.ConfigParser()
        config.read("alembic.ini")
        url = get_db_url()
        print("Creating engine at", url)
        engine = create_engine(url, echo=False)
        session_factory = sessionmaker(bind=engine)
        ss = scoped_session(session_factory)
        ss.configure()
        session: Session = ss()

        # noinspection PyUnresolvedReferences
        f = Address.zip_code.isnot(None)
        # noinspection PyUnresolvedReferences
        count = session.query(func.count(Address.id)).filter(f).scalar()
        query = session.query(Address).options(load_only('raw',
                                                         'zip_code')).filter(f)

        print("Selecting", count, "addresses")
        rows: List[Address] = query.all()
        print("Complete")

        pbar = progressbar.ProgressBar(max_value=count, initial_value=0)
        i = 0
        for row in rows:
            REDIS.hset(ZIP_HASH, row.raw, row.zip_code)
            i += 1
            pbar.update(i)
 def __init__(self):
     config = configparser.ConfigParser()
     config.read("alembic.ini")
     url = get_db_url()
     print("Creating engine at", url)
     self._engine = create_engine(url, echo=self.ECHO_SQL)
     session_factory = sessionmaker(bind=self._engine)
     self._Session = scoped_session(session_factory)
     self._Session.configure()
     self._session: Session = self._Session()
예제 #3
0
 def get_session() -> Session:
     config = configparser.ConfigParser()
     config.read("alembic.ini")
     url = get_db_url()
     print("Creating engine at", url)
     engine = create_engine(url)
     session_factory = sessionmaker(bind=engine)
     session = scoped_session(session_factory)
     session.configure()
     return session()
예제 #4
0
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        url=get_db_url(),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    with connectable.connect() as connection:
        configure_options['connection'] = connection
        context.configure(**configure_options)

        with context.begin_transaction():
            context.run_migrations()
예제 #5
0
    def __init__(self, plugins: Iterable[Type[MungerPlugin]], debug: bool):

        assert "id" not in self.ROW_FIELDS, "Should not set ID!"

        config = configparser.ConfigParser()
        config.read("alembic.ini")
        url = get_db_url()
        print("Creating engine at", url)
        self._engine = create_engine(url, echo=ECHO_SQL)
        session_factory = sessionmaker(bind=self._engine)
        self._Session = scoped_session(session_factory)
        self._Session.configure()
        self._session: Session = self._Session()
        self._r = StrictRedis(decode_responses=True)

        self._directory_map: MutableMapping[int, Directory] = {}

        # init plugins with session
        self._plugins: List[MungerPlugin] = []
        for constructor in plugins:
            self._plugins.append(constructor(self._session, debug))
예제 #6
0
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = providers.Provider.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

# Autogeneration information will always be here:
# http://alembic.zzzcomputing.com/en/latest/autogenerate.html
configure_options = {
    'url': get_db_url(),
    'target_metadata': target_metadata,
    'compare_type': True,
    'include_schemas': True,
    'compare_server_default': True
}


def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.
예제 #7
0
import configparser as configparser

from sqlalchemy import create_engine

from db_url import get_db_url
from provider.models.directories import Directory
from provider.models.providers import Provider

config = configparser.ConfigParser()
config.read("alembic.ini")
url = get_db_url()
engine = create_engine(url, echo=True)
Provider.metadata.drop_all(engine)
Directory.metadata.drop_all(engine)
Directory.metadata.create_all(engine)
Provider.metadata.create_all(engine)

# Reset all sequences
LIST_SEQUENCES = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';"
SEQUENCES = []
for row in engine.execute(LIST_SEQUENCES):
    engine.execute("ALTER SEQUENCE monday.{} RESTART WITH 1;".format(row[0]))