def db(): environ["POSTGRES_DB"] = "user-manager" postgres_container = PostgresContainer("postgres:12.2-alpine") with postgres_container as postgres: environ["DATABASE_URL"] = postgres.get_connection_url() database = Database(get_test_settings()) system("alembic upgrade head") yield database
def connect_database(self): # DB instance self.db_instance = Database(self.config) # Connect to database self.db_connection = self.db_instance.connect() self.mysql_instance = self.db_connection.get('mysql_instance') self.mysql_connection = self.db_connection.get('mysql_connection') self.mysql_ctx = self.db_connection.get('mysql_ctx')
def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) # app.config.from_mapping( # SECRET_KEY='dev', # DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), # ) if IS_MIGRATE == "FALSE": # db instance db_instance = Database() # create connection db = db_instance.connect() # get mysql instance and connection and context mysql_instance = db.get('mysql_instance') mysql_connection = db.get('mysql_connection') mysql_ctx = db.get('mysql_ctx') app.mysql_instance = mysql_instance app.mysql_connection = mysql_connection app.mysql = mysql_ctx app.mysql_ctx = mysql_ctx app.mysql_lastrowid = None app.db_instance = db_instance app.environment = os.environ # app.mysql_close_connection = db_instance.close_connection(app.mysql_connection, app.mysql) # app.mysql.execute("show databases") # for i in app.mysql: # print(i) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # a simple page that says hello # @app.route('/hello') # def hello(): # return 'Hello, World!' return app
def connect(self): db_instance = Database() # create connection db = db_instance.connect() # get mysql instance and connection and context mysql_instance = db.get('mysql_instance') mysql_connection = db.get('mysql_connection') mysql_ctx = db.get('mysql_ctx') app.mysql_instance = mysql_instance app.mysql_connection = mysql_connection app.mysql = mysql_ctx app.db_instance = db_instance
def db_session(settings: Settings = Depends(get_settings)): db = Database(settings) with db.get_session() as session: yield session
async def load_db() -> Generator: with Database() as db: yield db
async def startup(ctx: Dict[str, Any]) -> None: db = Database(ctx["config"].database_uri) # Warning: This is running blocking IO and if this gets anymore heavy you should really run this in a thread pool db.connect() ctx["database"] = db