
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('mysql+pymysql://stackviz:stackviz@localhost/stackviz',
                       pool_recycle=3600)

session = scoped_session(sessionmaker(autocommit=False,
                                      autoflush=False,
                                      bind=engine))

Base = declarative_base()
Base.query = session.query_property()


def init_db():
    # noinspection PyUnresolvedReferences
    import stackviz_deployer.db.models

    Base.metadata.create_all(bind=engine)


import os

from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


# override using environment variables if available
url = URL('mysql+pymysql',
          username=os.environ.get('MYSQL_ENV_MYSQL_USER', 'stackviz'),
          password=os.environ.get('MYSQL_ENV_MYSQL_PASSWORD', 'stackviz'),
          host=os.environ.get('MYSQL_PORT_3306_TCP_ADDR', 'localhost'),
          port=int(os.environ.get('MYSQL_PORT_3306_TCP_POST', '3306')),
          database=os.environ.get('MYSQL_ENV_MYSQL_DATABASE', 'stackviz'))

engine = create_engine(url, pool_recycle=3600)

session = scoped_session(sessionmaker(autocommit=False,
                                      autoflush=False,
                                      bind=engine))

Base = declarative_base()
Base.query = session.query_property()


def init_db():
    # noinspection PyUnresolvedReferences
    import stackviz_deployer.db.models

    Base.metadata.create_all(bind=engine)

Allow environment variable overrides for DB connection.
Allow environment variable overrides for DB connection.

This allows docker-style environment variables to override the default
database connection info.

+ 
+ import os
  
  from sqlalchemy import create_engine
+ from sqlalchemy.engine.url import URL
  from sqlalchemy.orm import scoped_session, sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  
- engine = create_engine('mysql+pymysql://stackviz:stackviz@localhost/stackviz',
-                        pool_recycle=3600)
+ 
+ # override using environment variables if available
+ url = URL('mysql+pymysql',
+           username=os.environ.get('MYSQL_ENV_MYSQL_USER', 'stackviz'),
+           password=os.environ.get('MYSQL_ENV_MYSQL_PASSWORD', 'stackviz'),
+           host=os.environ.get('MYSQL_PORT_3306_TCP_ADDR', 'localhost'),
+           port=int(os.environ.get('MYSQL_PORT_3306_TCP_POST', '3306')),
+           database=os.environ.get('MYSQL_ENV_MYSQL_DATABASE', 'stackviz'))
+ 
+ engine = create_engine(url, pool_recycle=3600)
  
  session = scoped_session(sessionmaker(autocommit=False,
                                        autoflush=False,
                                        bind=engine))
  
  Base = declarative_base()
  Base.query = session.query_property()
  
  
  def init_db():
      # noinspection PyUnresolvedReferences
      import stackviz_deployer.db.models
  
      Base.metadata.create_all(bind=engine)
  
Allow environment variable overrides for DB connection.
## Code Before:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('mysql+pymysql://stackviz:stackviz@localhost/stackviz',
                       pool_recycle=3600)

session = scoped_session(sessionmaker(autocommit=False,
                                      autoflush=False,
                                      bind=engine))

Base = declarative_base()
Base.query = session.query_property()


def init_db():
    # noinspection PyUnresolvedReferences
    import stackviz_deployer.db.models

    Base.metadata.create_all(bind=engine)

## Instruction:
Allow environment variable overrides for DB connection.
## Code After:

import os

from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


# override using environment variables if available
url = URL('mysql+pymysql',
          username=os.environ.get('MYSQL_ENV_MYSQL_USER', 'stackviz'),
          password=os.environ.get('MYSQL_ENV_MYSQL_PASSWORD', 'stackviz'),
          host=os.environ.get('MYSQL_PORT_3306_TCP_ADDR', 'localhost'),
          port=int(os.environ.get('MYSQL_PORT_3306_TCP_POST', '3306')),
          database=os.environ.get('MYSQL_ENV_MYSQL_DATABASE', 'stackviz'))

engine = create_engine(url, pool_recycle=3600)

session = scoped_session(sessionmaker(autocommit=False,
                                      autoflush=False,
                                      bind=engine))

Base = declarative_base()
Base.query = session.query_property()


def init_db():
    # noinspection PyUnresolvedReferences
    import stackviz_deployer.db.models

    Base.metadata.create_all(bind=engine)
