Queer European MD passionate about IT
Browse Source

Method to add table columns if missing

Davte 1 year ago
parent
commit
41f38d0b23
2 changed files with 17 additions and 1 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 16 0
      davtelepot/utilities.py

+ 1 - 1
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"
 

+ 16 - 0
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
+            )