From cb0c0eacd17c40a36490a52febc6bede7166d09d Mon Sep 17 00:00:00 2001 From: T0315986 Date: Tue, 28 Oct 2025 14:12:54 +0100 Subject: [PATCH] feat(create): create and deletion feature --- .env.example | 7 +++++++ .gitignore | 3 +++ helloword.py | 1 + test.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 helloword.py create mode 100644 test.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..eb021aa --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +DB_HOST=ip-address +DB_PORT=port +DB_NAME=name +DB_USER=user +DB_PASSWORD=password + +DATABASE_URL="postgresql+psycopg2://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d15d9d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +.venv/ +__pycache__ diff --git a/helloword.py b/helloword.py new file mode 100644 index 0000000..b17c9a5 --- /dev/null +++ b/helloword.py @@ -0,0 +1 @@ +print("helloWord") diff --git a/test.py b/test.py new file mode 100644 index 0000000..1052091 --- /dev/null +++ b/test.py @@ -0,0 +1,50 @@ +from dotenv import load_dotenv +import os +from sqlalchemy_utils import database_exists, create_database, drop_database +from sqlalchemy import ( + create_engine, + select, + Table, + Column, + MetaData, + Integer, + Identity, + String, +) + +load_dotenv() +DATABASE_URL = os.getenv("DATABASE_URL") + +engine = create_engine( + DATABASE_URL, + # isolation_level="AUTOCOMMIT", +) + +if not database_exists(engine.url): + create_database(engine.url) +else: + drop_database(engine.url) + create_database(engine.url) + +print(database_exists(engine.url)) + +metadata = MetaData() + +test = Table( + "test", + metadata, + Column("id", Integer, Identity(start=1, cycle=True), primary_key=True), + Column("testString", String), +) +metadata.create_all(engine) + +with engine.connect() as conn: + # conn.execution_options(isolation_level="AUTOCOMMIT") + trans = conn.begin() + + insert = test.insert().values(testString="ca") + conn.execute(insert) + rows = conn.execute(select(test)) + trans.commit() + for row in rows: + print(row)