-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_sqlite3.py
More file actions
131 lines (98 loc) · 5.05 KB
/
Copy pathvalidate_sqlite3.py
File metadata and controls
131 lines (98 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2025 Deutsche Telekom Technik GmbH <f.vonstudsinske@telekom.de>
# SPDX-License-Identifier: GPL-3.0-only
import sqlite3
from typing import List, Tuple
class ValidateSqlite3File:
""" Validates a new DB has the same tables and columns with an DB template.
.. code-block:: python
# tests if `validate.db` has same tables, columns defined in `file.db`
ok, results = ValidateSqlite3File.compare("main/file.db", "to/validate.db")
"""
GET_TABLES = """SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';"""
GET_COLUMNS = """PRAGMA table_info({});"""
@classmethod
def compare(cls, template_db_file: str, check_db_file: str) -> Tuple[bool, Tuple[str, str]]:
""" proofs whether the new DB has more or less Tables or columns than the DB template
:param template_db_file: path to master db
:param check_db_file: path to db to validate
:return: boolean falls to False on error, Tuple with a bool and a tuple with error texts
"""
# calls the compareTables method
ok, table_error = cls.compare_tables(template_db_file, check_db_file)
if not ok:
# outputs an error and the missing tables
name = ",".join(table_error)
return False, ("Prüfdatenbank fehlerhaft, Tabellen Fehlen", name)
ok, table_error = cls.compare_tables(check_db_file, template_db_file)
if not ok:
# outputs an error and the unknown tables
name = ",".join(table_error)
return False, ("Projektdatenbank fehlerhaft, Tabellen unbekannt", name)
con_template = sqlite3.connect(template_db_file)
cur_template = con_template.cursor()
cur_template.execute(cls.GET_TABLES)
template_tables = cur_template.fetchall()
con_template.close()
for table in template_tables:
# calls the compare_columns method
ok, column_error = cls.compare_columns(template_db_file, check_db_file, table[0])
if not ok:
column_names = ",".join(column_error)
return False, ("Projektdatenbank fehlerhaft, Spalte:", column_names)
return True, tuple()
@classmethod
def compare_tables(cls, template_db_file: str, check_db_file: str) -> Tuple[bool, List[str]]:
""" compares whether the tables of the template are also in the new DB
:param template_db_file: path to master db
:param check_db_file: path to db to validate
:return: Tuple with a bool and table names
"""
template_tables_list = []
data_tables_list = []
table_error = []
con_template = sqlite3.connect(template_db_file) # DB Template connection
cur_template = con_template.cursor()
cur_template.execute(cls.GET_TABLES) # gets all tables from DB Template
con_database = sqlite3.connect(check_db_file) # new DB connection
cur_database = con_database.cursor()
cur_database.execute(cls.GET_TABLES)
for table in cur_template.fetchall(): # from all tables
template_tables_list.append(table[0])
for table in cur_database.fetchall():
data_tables_list.append(table[0])
for table in template_tables_list:
if table not in data_tables_list:
table_error.append(table)
con_template.close() # closes the template connection
con_database.close() # closes the new DB connection
return not bool(table_error), table_error
@classmethod
def compare_columns(cls, template_db_file: str, check_db_file: str, name: str) -> Tuple[bool, List[str]]:
""" Proofs whether the columns of the master DB are also in the check DB
:param template_db_file: path to master db
:param check_db_file: path to db to validate
:param name: table name
:return: Tuple with a bool and table names
"""
# ["UID-CHAR", ...]
template_columns_list = []
data_columns_list = []
column_error = []
con_template = sqlite3.connect(template_db_file)
cur_template = con_template.cursor()
cur_template.execute(cls.GET_COLUMNS.format(name))
con_database = sqlite3.connect(check_db_file)
cur_database = con_database.cursor()
cur_database.execute(cls.GET_COLUMNS.format(name))
for column in cur_template.fetchall():
template_columns_list.append(f"{column[1]}-{column[2]}") # adds the column 1,2 to new list
for column in cur_database.fetchall():
data_columns_list.append(f"{column[1]}-{column[2]}") # adds the column 1,2 to new list
for column in template_columns_list:
# proofs whether columns from master in new DB. if not adds column name to new list
if column not in data_columns_list:
column_error.append(column)
con_template.close()
con_database.close()
return not bool(column_error), column_error