Skip to content

Commit 9befed4

Browse files
committed
verify db integrity
1 parent 59a64fa commit 9befed4

File tree

4 files changed

+364
-57
lines changed

4 files changed

+364
-57
lines changed

core/Services/Database.vala

Lines changed: 201 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class Services.Database : GLib.Object {
2525
private string errormsg;
2626
private string sql;
2727

28+
private Gee.HashMap<string, Gee.ArrayList<string>> table_columns = new Gee.HashMap<string, Gee.ArrayList<string>>();
29+
2830
public signal void opened ();
2931
public signal void reset ();
3032

@@ -37,9 +39,136 @@ public class Services.Database : GLib.Object {
3739
return _instance;
3840
}
3941

42+
construct {
43+
table_columns ["Attachments"] = new Gee.ArrayList<string> ();
44+
table_columns ["Attachments"].add ("id");
45+
table_columns ["Attachments"].add ("item_id");
46+
table_columns ["Attachments"].add ("file_type");
47+
table_columns ["Attachments"].add ("file_name");
48+
table_columns ["Attachments"].add ("file_size");
49+
table_columns ["Attachments"].add ("file_path");
50+
51+
table_columns ["CurTempIds"] = new Gee.ArrayList<string> ();
52+
table_columns ["CurTempIds"].add ("id");
53+
table_columns ["CurTempIds"].add ("temp_id");
54+
table_columns ["CurTempIds"].add ("object");
55+
56+
table_columns ["Items"] = new Gee.ArrayList<string> ();
57+
table_columns ["Items"].add ("id");
58+
table_columns ["Items"].add ("content");
59+
table_columns ["Items"].add ("description");
60+
table_columns ["Items"].add ("due");
61+
table_columns ["Items"].add ("added_at");
62+
table_columns ["Items"].add ("completed_at");
63+
table_columns ["Items"].add ("updated_at");
64+
table_columns ["Items"].add ("section_id");
65+
table_columns ["Items"].add ("project_id");
66+
table_columns ["Items"].add ("parent_id");
67+
table_columns ["Items"].add ("priority");
68+
table_columns ["Items"].add ("child_order");
69+
table_columns ["Items"].add ("checked");
70+
table_columns ["Items"].add ("is_deleted");
71+
table_columns ["Items"].add ("day_order");
72+
table_columns ["Items"].add ("collapsed");
73+
table_columns ["Items"].add ("pinned");
74+
table_columns ["Items"].add ("labels");
75+
table_columns ["Items"].add ("extra_data");
76+
table_columns ["Items"].add ("item_type");
77+
78+
table_columns ["Labels"] = new Gee.ArrayList<string> ();
79+
table_columns ["Labels"].add ("id");
80+
table_columns ["Labels"].add ("name");
81+
table_columns ["Labels"].add ("color");
82+
table_columns ["Labels"].add ("item_order");
83+
table_columns ["Labels"].add ("is_deleted");
84+
table_columns ["Labels"].add ("is_favorite");
85+
table_columns ["Labels"].add ("backend_type");
86+
table_columns ["Labels"].add ("source_id");
87+
88+
table_columns ["OEvents"] = new Gee.ArrayList<string> ();
89+
table_columns ["OEvents"].add ("id");
90+
table_columns ["OEvents"].add ("event_type");
91+
table_columns ["OEvents"].add ("event_date");
92+
table_columns ["OEvents"].add ("object_id");
93+
table_columns ["OEvents"].add ("object_type");
94+
table_columns ["OEvents"].add ("object_key");
95+
table_columns ["OEvents"].add ("object_old_value");
96+
table_columns ["OEvents"].add ("object_new_value");
97+
table_columns ["OEvents"].add ("parent_item_id");
98+
table_columns ["OEvents"].add ("parent_project_id");
99+
100+
table_columns ["Projects"] = new Gee.ArrayList<string> ();
101+
table_columns ["Projects"].add ("id");
102+
table_columns ["Projects"].add ("name");
103+
table_columns ["Projects"].add ("color");
104+
table_columns ["Projects"].add ("backend_type");
105+
table_columns ["Projects"].add ("inbox_project");
106+
table_columns ["Projects"].add ("team_inbox");
107+
table_columns ["Projects"].add ("child_order");
108+
table_columns ["Projects"].add ("is_deleted");
109+
table_columns ["Projects"].add ("is_archived");
110+
table_columns ["Projects"].add ("is_favorite");
111+
table_columns ["Projects"].add ("shared");
112+
table_columns ["Projects"].add ("view_style");
113+
table_columns ["Projects"].add ("sort_order");
114+
table_columns ["Projects"].add ("parent_id");
115+
table_columns ["Projects"].add ("collapsed");
116+
table_columns ["Projects"].add ("icon_style");
117+
table_columns ["Projects"].add ("emoji");
118+
table_columns ["Projects"].add ("show_completed");
119+
table_columns ["Projects"].add ("description");
120+
table_columns ["Projects"].add ("due_date");
121+
table_columns ["Projects"].add ("inbox_section_hidded");
122+
table_columns ["Projects"].add ("sync_id");
123+
table_columns ["Projects"].add ("source_id");
124+
125+
table_columns ["Queue"] = new Gee.ArrayList<string> ();
126+
table_columns ["Queue"].add ("uuid");
127+
table_columns ["Queue"].add ("object_id");
128+
table_columns ["Queue"].add ("query");
129+
table_columns ["Queue"].add ("temp_id");
130+
table_columns ["Queue"].add ("args");
131+
table_columns ["Queue"].add ("date_added");
132+
133+
table_columns ["Reminders"] = new Gee.ArrayList<string> ();
134+
table_columns ["Reminders"].add ("id");
135+
table_columns ["Reminders"].add ("notify_uid");
136+
table_columns ["Reminders"].add ("item_id");
137+
table_columns ["Reminders"].add ("service");
138+
table_columns ["Reminders"].add ("type");
139+
table_columns ["Reminders"].add ("due");
140+
table_columns ["Reminders"].add ("mm_offset");
141+
table_columns ["Reminders"].add ("is_deleted");
142+
143+
table_columns ["Sections"] = new Gee.ArrayList<string> ();
144+
table_columns ["Sections"].add ("id");
145+
table_columns ["Sections"].add ("name");
146+
table_columns ["Sections"].add ("archived_at");
147+
table_columns ["Sections"].add ("added_at");
148+
table_columns ["Sections"].add ("project_id");
149+
table_columns ["Sections"].add ("section_order");
150+
table_columns ["Sections"].add ("collapsed");
151+
table_columns ["Sections"].add ("is_deleted");
152+
table_columns ["Sections"].add ("is_archived");
153+
table_columns ["Sections"].add ("color");
154+
table_columns ["Sections"].add ("description");
155+
table_columns ["Sections"].add ("hidded");
156+
157+
table_columns ["Sources"] = new Gee.ArrayList<string> ();
158+
table_columns ["Sources"].add ("id");
159+
table_columns ["Sources"].add ("source_type");
160+
table_columns ["Sources"].add ("display_name");
161+
table_columns ["Sources"].add ("added_at");
162+
table_columns ["Sources"].add ("updated_at");
163+
table_columns ["Sources"].add ("is_visible");
164+
table_columns ["Sources"].add ("child_order");
165+
table_columns ["Sources"].add ("sync_server");
166+
table_columns ["Sources"].add ("last_sync");
167+
table_columns ["Sources"].add ("data");
168+
}
169+
40170
public void init_database () {
41171
db_path = Environment.get_user_data_dir () + "/io.github.alainm23.planify/database.db";
42-
print ("DB: %s\n".printf (db_path));
43172
Sqlite.Database.open (db_path, out db);
44173

45174
create_tables ();
@@ -440,7 +569,6 @@ public class Services.Database : GLib.Object {
440569
/*
441570
* Planner 3.10
442571
* - Add description to Projects
443-
* - Add due date to Projects
444572
*/
445573

446574
add_text_column ("Projects", "description", "");
@@ -500,6 +628,75 @@ public class Services.Database : GLib.Object {
500628
}
501629
}
502630

631+
public bool verify_integrity () {
632+
Sqlite.Statement stmt;
633+
634+
// Verify Data Integrity
635+
sql = """
636+
PRAGMA integrity_check;
637+
""";
638+
639+
db.prepare_v2 (sql, sql.length, out stmt);
640+
641+
if (stmt.step () == Sqlite.ROW) {
642+
if (stmt.column_text (0) != "ok") {
643+
return false;
644+
}
645+
}
646+
647+
// Verify Tables Integrity
648+
string[] tables = { "Attachments", "CurTempIds", "Items", "Labels",
649+
"OEvents", "Projects", "Queue", "Reminders", "Sections", "Sources" };
650+
651+
foreach (var table_name in tables) {
652+
if (!table_exists (table_name)) {
653+
return false;
654+
}
655+
}
656+
657+
// Verify Table Columns
658+
foreach (var table in table_columns.keys) {
659+
if (!table_columns_exists (table, table_columns.get (table))) {
660+
return false;
661+
}
662+
}
663+
664+
return true;
665+
}
666+
667+
private bool table_exists (string table_name) {
668+
Sqlite.Statement stmt;
669+
670+
sql = """
671+
SELECT name FROM sqlite_master WHERE type='table' AND name='%s';
672+
""".printf (table_name);
673+
674+
db.prepare_v2 (sql, sql.length, out stmt);
675+
676+
return stmt.step () == Sqlite.ROW;
677+
}
678+
679+
private bool table_columns_exists (string table, Gee.ArrayList<string> columns) {
680+
Sqlite.Statement stmt;
681+
682+
sql = """
683+
PRAGMA table_info(%s);
684+
""".printf (table);
685+
686+
db.prepare_v2 (sql, sql.length, out stmt);
687+
688+
stmt.step ();
689+
690+
while (stmt.step () == Sqlite.ROW) {
691+
if (!columns.contains (stmt.column_text (1))) {
692+
print ("Falta: %s: %s\n".printf (table, stmt.column_text (1)));
693+
return false;
694+
}
695+
}
696+
697+
return true;
698+
}
699+
503700
/*
504701
Sources
505702
*/
@@ -1598,7 +1795,6 @@ public class Services.Database : GLib.Object {
15981795
returned = stmt.column_int (0) > 0;
15991796
}
16001797

1601-
16021798
return returned;
16031799
}
16041800

@@ -1924,7 +2120,6 @@ public class Services.Database : GLib.Object {
19242120

19252121
public bool column_exists (string table, string column) {
19262122
Sqlite.Statement stmt;
1927-
bool returned = false;
19282123

19292124
sql = """
19302125
PRAGMA table_info(%s);
@@ -1936,12 +2131,11 @@ public class Services.Database : GLib.Object {
19362131

19372132
while (stmt.step () == Sqlite.ROW) {
19382133
if (stmt.column_text (1) == column) {
1939-
returned = true;
2134+
return true;
19402135
}
19412136
}
19422137

1943-
1944-
return returned;
2138+
return false;
19452139
}
19462140

19472141
public void add_text_column (string table, string column, string default_value) {

data/io.github.alainm23.planify.gresource.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<file alias="delay-symbolic.svg">resources/icons/delay-symbolic.svg</file>
9999
<file alias="go-up-symbolic.svg">resources/icons/go-up-symbolic.svg</file>
100100
<file alias="go-next-symbolic.svg">resources/icons/go-next-symbolic.svg</file>
101+
<file alias="process-error-symbolic.svg">resources/icons/process-error-symbolic.svg</file>
101102
</gresource>
102103

103104
<gresource prefix="/io/github/alainm23/planify/Devel/icons/scalable/actions">
@@ -170,6 +171,7 @@
170171
<file alias="delay-symbolic.svg">resources/icons/delay-symbolic.svg</file>
171172
<file alias="go-up-symbolic.svg">resources/icons/go-up-symbolic.svg</file>
172173
<file alias="go-next-symbolic.svg">resources/icons/go-next-symbolic.svg</file>
174+
<file alias="process-error-symbolic.svg">resources/icons/process-error-symbolic.svg</file>
173175
</gresource>
174176

175177
<gresource prefix="/io/github/alainm23/planify/quick-add/icons/scalable/actions">
@@ -242,5 +244,6 @@
242244
<file alias="delay-symbolic.svg">resources/icons/delay-symbolic.svg</file>
243245
<file alias="go-up-symbolic.svg">resources/icons/go-up-symbolic.svg</file>
244246
<file alias="go-next-symbolic.svg">resources/icons/go-next-symbolic.svg</file>
247+
<file alias="process-error-symbolic.svg">resources/icons/process-error-symbolic.svg</file>
245248
</gresource>
246249
</gresources>
Lines changed: 55 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)