Remove db when table is missing 92/167992/1
authorJeesun Kim <iamjs.kim@samsung.com>
Tue, 23 Jan 2018 03:53:16 +0000 (12:53 +0900)
committerjeesun kim <iamjs.kim@samsung.com>
Tue, 23 Jan 2018 08:52:34 +0000 (08:52 +0000)
Change-Id: I5ab56a51169d9b9dd7cda61a4b42dd3c1e33ba5d
(cherry picked from commit 37a9ef01f6a4bae8ebe37aa0776a907fe87152e6)

server/cal_server_schema.c
server/db/cal_db.h

index 62dbee8..4168f5e 100644 (file)
@@ -114,10 +114,50 @@ static int __check_integrity(void)
        }
 
        ret = sqlite3_exec(db, "pragma integrity_check", __integrity_callback, NULL, NULL);
+
+       if (true == need_to_remove || SQLITE_CORRUPT == ret || SQLITE_NOTADB == ret) {
+               /* LCOV_EXCL_START */
+               ERR("Delete db");
+               db_util_close(db);
+               return -2;
+               /* LCOV_EXCL_STOP */
+       }
+
+       /* check if table is missing */
+       sqlite3_stmt *stmt = NULL;
+       const char *query = "SELECT count(*) FROM sqlite_master WHERE type='table'"
+               " AND name IN ('schedule_table','rrule_table','normal_instance_table',"
+               "'allday_instance_table','attendee_table','calendar_table','timezone_table',"
+               "'alarm_table','extended_table','deleted_table','version_table')";
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (SQLITE_OK != ret) {
+               /* LCOV_EXCL_START */
+               ERR("sqlite3_qlite3_prepare_v2() Fail(%d)", ret);
+               db_util_close(db);
+               return SQLITE_ERROR == ret ? -2 : -1; /* return -2, when sqlite_master does not exist */
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = sqlite3_step(stmt);
+       if (SQLITE_ROW != ret) {
+               /* LCOV_EXCL_START */
+               ERR("sqlite3_step() Fail(%d)", ret);
+               sqlite3_finalize(stmt);
+               db_util_close(db);
+               return -1;
+               /* LCOV_EXCL_STOP */
+       }
+
+       int get_count = sqlite3_column_int(stmt, 0);
+       sqlite3_finalize(stmt);
        db_util_close(db);
 
-       if (true == need_to_remove || SQLITE_CORRUPT == ret || SQLITE_NOTADB == ret)
+       if (get_count != CAL_TABLE_COUNT) {
+               /* LCOV_EXCL_START */
+               ERR("Table is missing(expected(%d) but result(%d)", CAL_TABLE_COUNT, get_count);
                return -2;
+               /* LCOV_EXCL_STOP */
+       }
 
        return 0;
 }
index cd9aa15..bd91ba6 100644 (file)
@@ -51,6 +51,8 @@
 #define CAL_TABLE_UTIME_INSTANCE "normal_instance_table"
 #define CAL_TABLE_LOCALTIME_INSTANCE "allday_instance_table"
 #define CAL_TABLE_EXTENDED "extended_table"
+/* MUST MACTH table count */
+#define CAL_TABLE_COUNT 11
 
 /* for event or todo.. */
 #define CAL_VIEW_TABLE_EVENT "event_view"