diff --git a/flaschengeist/config.py b/flaschengeist/config.py index ed74470..9820e9e 100644 --- a/flaschengeist/config.py +++ b/flaschengeist/config.py @@ -9,7 +9,7 @@ from flaschengeist import _module_path, logger # Default config: -config = {"DATABASE": {"port": 3306}} +config = {"DATABASE": {"engine": "mysql", "port": 3306}} def update_dict(d, u): @@ -65,17 +65,30 @@ def configure_app(app, test_config=None): else: app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"] - if test_config is None: - app.config["SQLALCHEMY_DATABASE_URI"] = "mysql{driver}://{user}:{passwd}@{host}:{port}/{database}".format( - driver="+pymysql" if os.name == "nt" else "", + if test_config is not None: + config["DATABASE"]["engine"] = "sqlite" + + if config["DATABASE"]["engine"] == "mysql": + engine = "mysql" + ("+pymysql" if os.name == "nt" else "") + options = "?charset=utf8mb4" + elif config["DATABASE"]["engine"] == "postgres": + engine = "postgresql+psycopg2" + options = "?client_encoding=utf8" + elif config["DATABASE"]["engine"] == "sqlite": + engine = "sqlite" + options = "" + host = "" + else: + logger.error(f"Invalid database engine configured. >{config['DATABASE']['engine']}< is unknown") + raise Exception + if config["DATABASE"]["engine"] in ["mysql", "postgresql"]: + host = "{user}:{password}@{host}:{port}".format( user=config["DATABASE"]["user"], - passwd=config["DATABASE"]["password"], + password=config["DATABASE"]["password"], host=config["DATABASE"]["host"], - database=config["DATABASE"]["database"], port=config["DATABASE"]["port"], ) - else: - app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite+pysqlite://{config['DATABASE']['file_path']}" + app.config["SQLALCHEMY_DATABASE_URI"] = f"{engine}://{host}/{config['DATABASE']['database']}{options}" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False if "root" in config["FLASCHENGEIST"]: diff --git a/flaschengeist/flaschengeist.toml b/flaschengeist/flaschengeist.toml index 0ec50a0..ffbb51d 100644 --- a/flaschengeist/flaschengeist.toml +++ b/flaschengeist/flaschengeist.toml @@ -20,6 +20,7 @@ secret_key = "V3ryS3cr3t" level = "WARNING" [DATABASE] +# engine = "mysql" (default) # user = "user" # host = "127.0.0.1" # password = "password" diff --git a/flaschengeist/models/__init__.py b/flaschengeist/models/__init__.py index f63d173..1b503dd 100644 --- a/flaschengeist/models/__init__.py +++ b/flaschengeist/models/__init__.py @@ -2,7 +2,7 @@ import sys import datetime from sqlalchemy import BigInteger -from sqlalchemy.dialects import mysql +from sqlalchemy.dialects import mysql, sqlite from sqlalchemy.types import DateTime, TypeDecorator @@ -44,7 +44,7 @@ class ModelSerializeMixin: class Serial(TypeDecorator): """Same as MariaDB Serial used for IDs""" - impl = BigInteger().with_variant(mysql.BIGINT(unsigned=True), "mysql") + impl = BigInteger().with_variant(mysql.BIGINT(unsigned=True), "mysql").with_variant(sqlite.INTEGER, "sqlite") class UtcDateTime(TypeDecorator):