[db] Support sqlite and postgresql as engine, fixes #5
mysql / mariadb still is the only tested configuration. This will break existing databases, as UTF8MB4 is enforced for mysql (real UTF8).
This commit is contained in:
parent
8696699ecb
commit
7b5f854d51
|
@ -9,7 +9,7 @@ from flaschengeist import _module_path, logger
|
||||||
|
|
||||||
|
|
||||||
# Default config:
|
# Default config:
|
||||||
config = {"DATABASE": {"port": 3306}}
|
config = {"DATABASE": {"engine": "mysql", "port": 3306}}
|
||||||
|
|
||||||
|
|
||||||
def update_dict(d, u):
|
def update_dict(d, u):
|
||||||
|
@ -65,17 +65,30 @@ def configure_app(app, test_config=None):
|
||||||
else:
|
else:
|
||||||
app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"]
|
app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"]
|
||||||
|
|
||||||
if test_config is None:
|
if test_config is not None:
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql{driver}://{user}:{passwd}@{host}:{port}/{database}".format(
|
config["DATABASE"]["engine"] = "sqlite"
|
||||||
driver="+pymysql" if os.name == "nt" else "",
|
|
||||||
|
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"],
|
user=config["DATABASE"]["user"],
|
||||||
passwd=config["DATABASE"]["password"],
|
password=config["DATABASE"]["password"],
|
||||||
host=config["DATABASE"]["host"],
|
host=config["DATABASE"]["host"],
|
||||||
database=config["DATABASE"]["database"],
|
|
||||||
port=config["DATABASE"]["port"],
|
port=config["DATABASE"]["port"],
|
||||||
)
|
)
|
||||||
else:
|
app.config["SQLALCHEMY_DATABASE_URI"] = f"{engine}://{host}/{config['DATABASE']['database']}{options}"
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite+pysqlite://{config['DATABASE']['file_path']}"
|
|
||||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
|
|
||||||
if "root" in config["FLASCHENGEIST"]:
|
if "root" in config["FLASCHENGEIST"]:
|
||||||
|
|
|
@ -20,6 +20,7 @@ secret_key = "V3ryS3cr3t"
|
||||||
level = "WARNING"
|
level = "WARNING"
|
||||||
|
|
||||||
[DATABASE]
|
[DATABASE]
|
||||||
|
# engine = "mysql" (default)
|
||||||
# user = "user"
|
# user = "user"
|
||||||
# host = "127.0.0.1"
|
# host = "127.0.0.1"
|
||||||
# password = "password"
|
# password = "password"
|
||||||
|
|
|
@ -2,7 +2,7 @@ import sys
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from sqlalchemy import BigInteger
|
from sqlalchemy import BigInteger
|
||||||
from sqlalchemy.dialects import mysql
|
from sqlalchemy.dialects import mysql, sqlite
|
||||||
from sqlalchemy.types import DateTime, TypeDecorator
|
from sqlalchemy.types import DateTime, TypeDecorator
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class ModelSerializeMixin:
|
||||||
class Serial(TypeDecorator):
|
class Serial(TypeDecorator):
|
||||||
"""Same as MariaDB Serial used for IDs"""
|
"""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):
|
class UtcDateTime(TypeDecorator):
|
||||||
|
|
Loading…
Reference in New Issue