From 41f38d0b2349af099e0d35e0e66700b9af7cbbdc Mon Sep 17 00:00:00 2001 From: Davte Date: Wed, 23 Aug 2023 18:23:17 +0200 Subject: [PATCH] Method to add table columns if missing --- davtelepot/__init__.py | 2 +- davtelepot/utilities.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index 3cb3f1c..173e16a 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -11,7 +11,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.9.5" +__version__ = "2.9.6" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/utilities.py b/davtelepot/utilities.py index f2a9605..99b8c20 100644 --- a/davtelepot/utilities.py +++ b/davtelepot/utilities.py @@ -21,6 +21,7 @@ from typing import Tuple, Union # Third party modules import aiohttp +import dataset from bs4 import BeautifulSoup @@ -1747,3 +1748,18 @@ async def aio_subprocess_shell(command: str, def join_path(*args): return os.path.abspath(os.path.join(*args)) + + +def add_table_and_columns_if_not_existent(database: dataset.Database, + table_name: str, + columns: Tuple[Tuple, ...]): + if table_name not in database.tables: + table = database.create_table(table_name=table_name) + else: + table = database[table_name] + for column_name, column_type in columns: + if not table.has_column(column_name): + table.create_column( + column_name, + column_type + )