Method to add a table and its columns to and ObjectWithDatabase
This commit is contained in:
parent
881d249256
commit
55b47ed1f7
@ -11,7 +11,7 @@ __author__ = "Davide Testa"
|
|||||||
__email__ = "davide@davte.it"
|
__email__ = "davide@davte.it"
|
||||||
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "2.8.8"
|
__version__ = "2.8.9"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
# Standard library modules
|
# Standard library modules
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
# Third party modules
|
# Third party modules
|
||||||
import dataset
|
import dataset
|
||||||
@ -71,3 +72,32 @@ class ObjectWithDatabase(object):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"{e}")
|
logging.error(f"{e}")
|
||||||
|
|
||||||
|
def add_table_and_columns_if_not_existent(self,
|
||||||
|
table_name: str,
|
||||||
|
columns: Tuple[
|
||||||
|
Tuple[str,
|
||||||
|
dataset.database.Types],
|
||||||
|
...] = None):
|
||||||
|
"""Create table (if it does not exist) and add given columns (if missing).
|
||||||
|
|
||||||
|
@param table_name: Table name (string)
|
||||||
|
@param columns: Table columns as tuples of column name and type
|
||||||
|
@return: None
|
||||||
|
"""
|
||||||
|
if table_name not in self.db.tables:
|
||||||
|
table = self.db.create_table(table_name=table_name)
|
||||||
|
logging.info(f"Created table `{table_name}`")
|
||||||
|
else:
|
||||||
|
table = self.db[table_name]
|
||||||
|
if columns is None:
|
||||||
|
columns = []
|
||||||
|
for column_name, column_type in columns:
|
||||||
|
if not table.has_column(column_name):
|
||||||
|
table.create_column(
|
||||||
|
column_name,
|
||||||
|
column_type
|
||||||
|
)
|
||||||
|
logging.info(f"Added column `{column_name}` "
|
||||||
|
f"(type `{column_type}`) "
|
||||||
|
f"to table `{table_name}`")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user