File: utils.py

package info (click to toggle)
ormar 0.20.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,136 kB
  • sloc: python: 23,758; makefile: 34; sh: 14
file content (21 lines) | stat: -rw-r--r-- 642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import functools

import ormar
import sqlalchemy


def create_drop_database(base_config: ormar.OrmarConfig) -> None:
    # create all tables in the database before execution
    # and drop them after, note that in production you should use migrations
    def wrapper(func):
        @functools.wraps(func)
        async def wrapped(*args):
            engine = sqlalchemy.create_engine(str(base_config.database.url))
            base_config.metadata.drop_all(engine)
            base_config.metadata.create_all(engine)
            await func(*args)
            base_config.metadata.drop_all(engine)

        return wrapped

    return wrapper