##// END OF EJS Templates
models: new sqlalchemy logic and encryption setup for python3
super-admin -
r5069:aafa637a default
parent child Browse files
Show More
@@ -27,25 +27,32 b' from rhodecode.lib.utils2 import obfusca'
27 27 log = logging.getLogger(__name__)
28 28
29 29
30 def init_model(engine, encryption_key=None):
30 def init_model(engine, encryption_key: bytes = b''):
31 31 """
32 32 Initializes db session, bind the engine with the metadata,
33 33 Call this before using any of the tables or classes in the model,
34 34 preferably once in application start
35 35
36 36 :param engine: engine to bind to
37 :param encryption_key: key used for encryption
37 38 """
39
38 40 engine_str = obfuscate_url_pw(str(engine.url))
39 41 log.info("RhodeCode %s initializing db for %s", rhodecode.__version__, engine_str)
40 meta.Base.metadata.bind = engine
41 db.ENCRYPTION_KEY = encryption_key
42
43 meta.bind_engine_to_session(engine)
44 init_model_encryption(db, enc_key=encryption_key)
42 45
43 46
44 def init_model_encryption(migration_models, config=None):
45 from pyramid.threadlocal import get_current_registry
46 config = config or get_current_registry().settings
47 migration_models.ENCRYPTION_KEY = get_encryption_key(config)
48 db.ENCRYPTION_KEY = get_encryption_key(config)
47 def init_model_encryption(*db_models, enc_key: bytes = b'', config=None):
48 if not enc_key:
49 from pyramid.threadlocal import get_current_registry
50 config = config or get_current_registry().settings
51 enc_key = get_encryption_key(config)
52
53 for db_model in db_models:
54 log.debug('setting encryption key for model %s', db_model)
55 db_model.ENCRYPTION_KEY = enc_key
49 56
50 57
51 58 class BaseModel(object):
@@ -23,33 +23,53 b' SQLAlchemy Metadata and Session object'
23 23
24 24 from sqlalchemy.orm import declarative_base
25 25 from sqlalchemy.orm import scoped_session, sessionmaker
26 from sqlalchemy.orm import Session as SASession
26 from sqlalchemy.orm import Session as SA_Session
27 27 from rhodecode.lib.caching_query import ORMCache
28 28
29 29
30 __all__ = ['Base', 'Session', 'raw_query_executor']
30 __all__ = [
31 'Base', 'Session', 'SA_Session',
32 'raw_query_executor',
33 'bind_engine_to_session',
34 'get_engine'
35 ]
31 36
32 37 # scoped_session. Apply our custom CachingQuery class to it,
33 38 # using a callable that will associate the dictionary
34 39 # of regions with the Query.
35 40 # to use cache use this in query
36 41 # .options(FromCache("sqlalchemy_cache_type", "cachekey"))
37 Session = scoped_session(
38 sessionmaker(
39 expire_on_commit=True,
40 )
41 )
42 session_factory = sessionmaker(
43 expire_on_commit=True,
44 future=True
45 )
42 46
43 # pass empty regions so we can fetch it on-demand inside ORMCache
47 Session = scoped_session(session_factory)
48
49 # The declarative Base
50 Base = declarative_base()
51
52 # pass empty regions, so we can fetch it on-demand inside ORMCache
44 53 cache = ORMCache(regions={})
45 54 cache.listen_on_session(Session)
46 55
47 56
48 # The declarative Base
49 Base = declarative_base()
57 def raw_query_executor(engine=None):
58 """
59
60 :param engine:
61 :return:
62 """
63 if not engine:
64 engine = Session.bind
65 session = SA_Session(engine)
66 return session
50 67
51 68
52 def raw_query_executor():
53 engine = Base.metadata.bind
54 session = SASession(engine)
55 return session
69 def get_engine():
70 return Session.bind
71
72
73 def bind_engine_to_session(engine):
74 Session.remove()
75 Session.configure(bind=engine)
General Comments 0
You need to be logged in to leave comments. Login now