def __init__(self, destination_uri): if not database_exists(destination_uri): while True: database = parse_uri(destination_uri) msg = input( f"The database {database} does not exists, would you like to create it in the destination?(y/n) " ) if msg.lower() == "y": try: create_database(destination_uri) logger.info("Database created..") break except Exception as err: goodby_message(database_not_exists(destination_uri), 1) break elif msg.lower() == "n": goodby_message( "Destination database does not exit \nExiting ..", 0) break print("Please, select command") self.base = automap_base() self.engine = create_engine(destination_uri) # self.base.prepare(self.engine, reflect=True) self.session = Session(self.engine, autocommit=False, autoflush=False)
def db_config(): with open(os.path.join("myproject", "cfg", "config.yml"), "r") as ymlfile: cfg = yaml.load(ymlfile)['database'] engine = create_engine('{0}://{1}:{2}@{3}/{4}'.format( cfg['server'], cfg['user'], cfg['passwd'], cfg['host'], cfg['db'])) if not database_exists(engine.url): create_database(engine.url) return cfg
def db_create(): """Creating database, if it not exists""" if not database_exists(db_credentials): create_database(db_credentials) db.create_all() db.session.commit() print "Successfully created database and tables." else: print "The database already exists!"
def __init__(self, config): self.base = automap_base() if not database_exists(config.destination_uri): while True: msg = input( f"{config.destination_uri} db does not exist, create destination database?(y/n) " ) if msg.lower() == "y": try: create_database(config.destination_uri) print("database creted ..") except Exception as err: goodby_message(err, 1) break elif msg.lower() == "n": goodby_message( "Destination database does not exit \nExiting ..", 0) break print("Please, select command") database_name = parse_uri(config.destination_uri) msg = input( f"The database {database_name} does not exists, would you like to create it in the destination?(y/n) " ) if msg.lower() == "y": try: create_database(config.destination_uri) sys.stdout.write("Database created ..") except Exception as err: print(err) sys.exit(1) else: sys.exit(0) self.engine = create_engine(config.destination_uri) self.base.prepare(self.engine, reflect=True) self.session = Session(self.engine, autocommit=False, autoflush=False)
def check_for_or_create_database( self, destination_uri, check_for_database: callable = database_exists): if not check_for_database(destination_uri): while True: database = parse_uri(destination_uri) msg = input( f"The database {database} does not exists, would you like to create it in the destination?(y/n) " ) if msg.lower() == "y": try: create_database(destination_uri) logger.info("Database created..") break except Exception as err: goodby_message(database_not_exists(destination_uri), 1) break elif msg.lower() == "n": goodby_message( "Destination database does not exist \nExiting ...", 0) break print("Please, select command")
from sqlalchemy_utils.functions.database import create_database, database_exists from models.users import Role, User from models.issues import Attachment, Category, Issue, IssueHistory, Status from manage import db from config import Config #Checking if database exists, and if not -> create it with all tables. db_credentials = Config.db_credentials if 'DATABASE_URL' in os.environ: db_credentials = os.environ['DATABASE_URL'] if not database_exists(db_credentials): create_database(db_credentials) db.create_all() db.session.commit() #Creating some test data. role = Role(role='admin') role1 = Role(role='moderator') role2 = Role(role='user') category = Category(category='road accident', favicon='') category1 = Category(category='infrastructure accident', favicon='') category2 = Category(category='another accident', favicon='') status1 = Status(status="new") status2 = Status(status="working")
import sqlalchemy as db from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from sqlalchemy_utils.functions.database import create_database, database_exists from app import log if os.environ.get("DB_URI"): engine = db.create_engine(os.environ["DB_URI"]) else: engine = db.create_engine( 'mysql+pymysql://admin:[email protected]/favorite' ) if not database_exists(engine.url): create_database(engine.url) Session = sessionmaker(bind=engine) Base = declarative_base() metadata = db.MetaData() metadata.create_all(engine) @contextmanager def session_scope(): """Provide a transactional scope around a series of operations.""" session = Session() try: yield session