Skip to content

Provide database connection from outside #613

Answered by collerek
Strernd asked this question in Q&A
Discussion options

You must be logged in to vote

Since you already follow best practice with BaseMeta you can easily switch to the db on the fly, a dummy example is like follows:

import sqlite3
import uuid
from typing import Any, Type

import databases
import pytest
import sqlalchemy

import ormar
from tests.settings import DATABASE_URL

metadata = sqlalchemy.MetaData()


def create_db_with_secrets_manager():
    # logic to grab database from somewhere
    return databases.Database(DATABASE_URL)


class BaseMeta(ormar.ModelMeta):
    # for now a valid databases db instance is required but it can be switched on the fly
    database = databases.Database("sqlite:///not_used.db")
    metadata = metadata


class Example(ormar.Model):
    class 

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@Strernd
Comment options

Comment options

You must be logged in to vote
2 replies
@Strernd
Comment options

@collerek
Comment options

Answer selected by Strernd
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants