Skip to content

Commit 07e1670

Browse files
Merge pull request #4 from GabrielTorelo/develop
Beta Version (v1.0.0) - OK
2 parents f8c97a5 + 5e548ba commit 07e1670

18 files changed

Lines changed: 563 additions & 1 deletion

.gitignore

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Byte-compiled / optimized / DLL files
2+
*/__pycache__/*
3+
*.py[cod]
4+
*$py.class
5+
6+
# Local use
7+
Output.json
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# Installer logs
30+
pip-log.txt
31+
pip-delete-this-directory.txt
32+
33+
# Unit test / coverage reports
34+
htmlcov/
35+
.tox/
36+
.nox/
37+
.coverage
38+
.coverage.*
39+
.cache
40+
nosetests.xml
41+
coverage.xml
42+
*.cover
43+
*.py,cover
44+
.hypothesis/
45+
.pytest_cache/
46+
cover/
47+
48+
# documentation
49+
docs/*
50+
51+
# PyBuilder
52+
.pybuilder/
53+
target/
54+
55+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
56+
__pypackages__/
57+
58+
# Environments
59+
.env
60+
.venv
61+
env/
62+
venv/
63+
ENV/
64+
env.bak/
65+
venv.bak/
66+
67+
# mkdocs documentation
68+
/site
69+
70+
# mypy
71+
.mypy_cache/
72+
.dmypy.json
73+
dmypy.json
74+
75+
# pytype static type analyzer
76+
.pytype/
77+
78+
# PyCharm
79+
.idea/

MOCK/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore directory contents
2+
/OUTPUT_DB/*

MOCK/DATA/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore everything in this directory
2+
*
3+
!.gitignore

MOCK/PATTERN/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore everything in this directory
2+
*
3+
!.gitignore

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,118 @@
11
# Database_Difference_Checker-Python_SQLite
2-
Python script to read SQLite files and check the differences between them
2+
Python script to read SQLite files and check the differences between them.
3+
4+
## Before you start!
5+
This document provides a step-by-step guide to performing comparison between SQLite databases using the `Database_Difference_Checker-Python_SQLite` script.
6+
7+
## Instructions for Use
8+
### 1. Using filters for tables and/or columns _(Optional Step)_
9+
- **Table Filters**: Edit the `constants/general.py` file and add the names of the tables you want to exclude from the comparison within the `EXCLUDED_TABLES` constant.
10+
#### Example:
11+
```
12+
EXCLUDED_TABLES = {"table1", "table2", "table3"}
13+
```
14+
- **Column Filters**: Edit the same file and add the names of the columns to be excluded from the comparison within the `EXCLUDED_COLUMNS` constant.
15+
#### Example:
16+
```
17+
EXCLUDED_COLUMNS = {"column1", "column2", "column3"}
18+
```
19+
20+
### 2. Adding SQLite databases for comparison
21+
Place the SQLite `(.db)` files you want to compare inside the `MOCK/DATA` folder.
22+
23+
- For contexts of different databases _(for example, databases from different companies)_, it is ideal, but not mandatory, to organize the databases in different folders within `MOCK/DATA`.
24+
#### Example:
25+
![image](https://github.com/user-attachments/assets/4d439483-bdcb-4bb4-aa0e-7f73a28ae0b6)
26+
27+
### 3. Adding the reference file
28+
Add the file with the reference data to the `MOCK/PATTERN` folder. **The reference file can be in `.txt`, `.json`, `.sqlite` or `.db` format**.
29+
30+
### **Important**
31+
- **The `.sqlite` and `.db` files take precedence over the others**
32+
- The file must follow the pattern **_key-value_** _(key: value)_, with the **VALUE ALWAYS WRAPPED IN BRACKETS**.
33+
#### Examples for text file `(.txt)`:
34+
- **Example 1 (without line break):**
35+
```
36+
TABLE_1:[{...}]TABLE_2:[...]TABLE_3:[...]
37+
```
38+
- **Example 2 (with line break):**
39+
```
40+
TABLE_1:[{...}, {...}]
41+
TABLE_2:[{...}]
42+
TABLE_3:[...]
43+
```
44+
#### Example for JSON file `(.json)`:
45+
```
46+
{
47+
"TABLE_1": [
48+
{
49+
"name": "XXXX",
50+
"type": "XXXX"
51+
},
52+
{
53+
"name": "XXXX",
54+
"type": "XXXX"
55+
}
56+
],
57+
"TABLE_2": [
58+
{
59+
"name": "XXXX",
60+
"type": "XXXX"
61+
}
62+
],
63+
...
64+
}
65+
```
66+
67+
### 4. Analysis of Results
68+
After executing the script, check the `trackingCode`. The code `00` indicates that the process completed without errors (other tracking codes in the section below).
69+
70+
**The comparison results will be saved in a JSON file in the project root folder, called `Output.json` .**
71+
72+
#### Example of `Output.json` file:
73+
```
74+
{
75+
"MAIN_DATABASE_1": {
76+
"DATABASE_SQLite_1": {
77+
"Missing_Tables": [...],
78+
"Missing_Data": {
79+
"TABLE_1": [
80+
{
81+
"name": "XXXX",
82+
"type": "XXXX"
83+
}
84+
],
85+
"TABLE_2": [...]
86+
}
87+
},
88+
"DATABASE_SQLite_2": {...}
89+
},
90+
"MAIN_DATABASE_2": {...}
91+
}
92+
```
93+
94+
## Tracking Codes
95+
96+
Tracking codes indicate the status of the process:
97+
98+
- `00`: **Finished without errors**
99+
- `01`: **An unexpected error occurred**
100+
- `02`: **Error reading file(s)**
101+
- `03`: **Error reading file(s) from directory**
102+
- `04`: **Error in the comparison process**
103+
- `05`: **Error generating `Output.json` file**
104+
- `06`: **Error reading data from databases**
105+
- `07`: **Error generating JSON from databases**
106+
- `08`: **Error when fetching JSON files from databases**
107+
- `09`: **Error creating reference JSON**
108+
- `10`: **Invalid file extension**
109+
110+
## 🛠 Used Packages
111+
112+
| Name | Version |
113+
| :-------- | :------ |
114+
| 🐍 Python | `3.8.0` |
115+
116+
## 🔗 Links
117+
118+
[![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/GabrielTorelo/Database_Difference_Checker-Python_SQLite)

constants/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .general import *
2+
from .sqlite_queries import *

constants/general.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TRACKING_CODE = "PRJ0000.X.X"
2+
DB_PATHS = "MOCK/DATA"
3+
PATTERN_PATH = "MOCK/PATTERN"
4+
PATTERN_PATH_JSON = "MOCK/PATTERN/padrao_db.json"
5+
OUTPUT_DB_PATH = "MOCK/OUTPUT_DB"
6+
EXCLUDED_TABLES = {"android_metadata", "sqlite_sequence"}
7+
EXCLUDED_COLUMNS = {""}

constants/sqlite_queries.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT_TABLES = "SELECT name FROM sqlite_master WHERE type='table';"
2+
PRAGMA_TABLE_INFO = "PRAGMA table_info(%s);"

dao/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .db_sqlite import *

dao/db_sqlite.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from sqlite3 import connect
2+
from err import Errors
3+
from constants import SELECT_TABLES, EXCLUDED_TABLES, EXCLUDED_COLUMNS, PRAGMA_TABLE_INFO
4+
5+
6+
def list_tables(db_path):
7+
tables_info = {}
8+
9+
try:
10+
connection = connect(db_path)
11+
cursor = connection.cursor()
12+
13+
cursor.execute(SELECT_TABLES)
14+
tables = [row[0] for row in cursor.fetchall()]
15+
16+
tables = sorted(tables)
17+
18+
for table in tables:
19+
if table in EXCLUDED_TABLES:
20+
continue
21+
22+
cursor.execute(PRAGMA_TABLE_INFO % table)
23+
columns = cursor.fetchall()
24+
25+
tables_info[table] = sorted([
26+
{'name': col[1], 'type': col[2]} for col in columns if col[1] not in EXCLUDED_COLUMNS
27+
], key=lambda x: x['name'])
28+
29+
connection.close()
30+
return tables_info
31+
except Exception as e:
32+
raise Errors(code=6, message=f"Error listing database tables: {e}")

0 commit comments

Comments
 (0)