From 675558ebaa4399c759f05ac63da63d4a35666c95 Mon Sep 17 00:00:00 2001 From: Hofei90 <29521028+hofei90@users.noreply.github.com> Date: Sun, 3 May 2020 23:00:21 +0200 Subject: [PATCH] InitialCommit --- db_model.py | 22 ++++++++++++++++++++++ peewee_timescale_test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 db_model.py create mode 100644 peewee_timescale_test.py diff --git a/db_model.py b/db_model.py new file mode 100644 index 0000000..2ab8e98 --- /dev/null +++ b/db_model.py @@ -0,0 +1,22 @@ +import peewee + + +database = peewee.Proxy() + + +class BaseModel(peewee.Model): + class Meta: + database = database + + +class TestPeewee(BaseModel): + ts = peewee.DateTimeField() + typ = peewee.TextField() + value = peewee.FloatField() + + class Meta: + primary_key = peewee.CompositeKey('ts', 'typ') + + +def create_tables(): + database.create_tables([TestPeewee]) diff --git a/peewee_timescale_test.py b/peewee_timescale_test.py new file mode 100644 index 0000000..4a6d26b --- /dev/null +++ b/peewee_timescale_test.py @@ -0,0 +1,31 @@ +from peewee import PostgresqlDatabase +from sshtunnel import SSHTunnelForwarder +import toml +import os +import db_model as db + + +def config_laden(): + configfile = os.path.join(SKRIPTPFAD, "config.toml") + with open(configfile) as file: + return toml.loads(file.read()) + + +SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__)) +CONFIG = config_laden() + + +def main(): + with SSHTunnelForwarder( + (CONFIG["ssh"]["ip_server"], CONFIG["ssh"]["ssh_port"]), ssh_username=CONFIG["ssh"]["user"], + ssh_password=CONFIG["ssh"]["pw"], remote_bind_address=('127.0.0.1', CONFIG["pg"]["pgport"])) as server: + + db.database.initialize(PostgresqlDatabase(CONFIG["pg"]["pgdb"], + user=CONFIG["pg"]["pguser"], password=CONFIG["pg"]["pgpw"], + host="127.0.0.1", + port=server.local_bind_port)) + db.create_tables() + + +if __name__ == "__main__": + main()