src/pEpEngine.c
changeset 928 c87f1fdaff36
parent 880 c9c9de1e8e27
child 929 b094dd6bb553
     1.1 --- a/src/pEpEngine.c	Tue Jul 26 11:03:29 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Wed Jul 27 12:35:51 2016 +0200
     1.3 @@ -6,6 +6,19 @@
     1.4  
     1.5  static int init_count = -1;
     1.6  
     1.7 +static int user_version(void *_version, int count, char **text, char **name)
     1.8 +{
     1.9 +    assert(_version);
    1.10 +    assert(count == 1);
    1.11 +    assert(text && text[0]);
    1.12 +    if (!(_version && count == 1 && text && text[0]))
    1.13 +        return -1;
    1.14 +
    1.15 +    int *version = (int *) _version;
    1.16 +    *version = atoi(text[0]);
    1.17 +    return 0;
    1.18 +}
    1.19 +
    1.20  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
    1.21  {
    1.22      PEP_STATUS status = PEP_STATUS_OK;
    1.23 @@ -112,15 +125,37 @@
    1.24  
    1.25      sqlite3_busy_timeout(_session->system_db, 1000);
    1.26  
    1.27 +// increment this when patching DDL
    1.28 +#define _DDL_USER_VERSION "1"
    1.29 +
    1.30      if (in_first) {
    1.31          int_result = sqlite3_exec(
    1.32              _session->db,
    1.33 -                "create table if not exists version_info (\n"
    1.34 +                "create table version_info (\n"
    1.35                  "   id integer primary key,\n"
    1.36                  "   timestamp integer default (datetime('now')) ,\n"
    1.37                  "   version text,\n"
    1.38                  "   comment text\n"
    1.39 -                ");\n"
    1.40 +                ");\n",
    1.41 +                NULL,
    1.42 +                NULL,
    1.43 +                NULL
    1.44 +        );
    1.45 +        if (int_result == SQLITE_OK) {
    1.46 +            int_result = sqlite3_exec(
    1.47 +                _session->db,
    1.48 +                "pragma user_version = "_DDL_USER_VERSION";\n"
    1.49 +                "insert or replace into version_info (id, version)"
    1.50 +                    "values (1, '" PEP_ENGINE_VERSION "');",
    1.51 +                NULL,
    1.52 +                NULL,
    1.53 +                NULL
    1.54 +            );
    1.55 +            assert(int_result == SQLITE_OK);
    1.56 +        }
    1.57 +
    1.58 +        int_result = sqlite3_exec(
    1.59 +            _session->db,
    1.60                  "create table if not exists log (\n"
    1.61                  "   timestamp integer default (datetime('now')) ,\n"
    1.62                  "   title text not null,\n"
    1.63 @@ -160,6 +195,7 @@
    1.64                  "       references pgp_keypair (fpr)\n"
    1.65                  "       on delete set null,\n"
    1.66                  "   comment text,\n"
    1.67 +                "   flags integer default (0),"
    1.68                  "   primary key (address, user_id)\n"
    1.69                  ");\n"
    1.70                  "create table if not exists trust (\n"
    1.71 @@ -196,15 +232,41 @@
    1.72          );
    1.73          assert(int_result == SQLITE_OK);
    1.74  
    1.75 +        int version;
    1.76          int_result = sqlite3_exec(
    1.77              _session->db,
    1.78 -            "insert or replace into version_info (id, version) values (1, '1.1');",
    1.79 -            NULL,
    1.80 -            NULL,
    1.81 +            "pragma user_version;",
    1.82 +            user_version,
    1.83 +            &version,
    1.84              NULL
    1.85          );
    1.86          assert(int_result == SQLITE_OK);
    1.87  
    1.88 +        if (version < 1) {
    1.89 +            int_result = sqlite3_exec(
    1.90 +                _session->db,
    1.91 +                "alter table identity\n"
    1.92 +                "   add column flags integer default (0);",
    1.93 +                NULL,
    1.94 +                NULL,
    1.95 +                NULL
    1.96 +            );
    1.97 +            assert(int_result == SQLITE_OK);
    1.98 +        }
    1.99 +
   1.100 +        if (version < atoi(_DDL_USER_VERSION)) {
   1.101 +            int_result = sqlite3_exec(
   1.102 +                _session->db,
   1.103 +                "pragma user_version = "_DDL_USER_VERSION";\n"
   1.104 +                "insert or replace into version_info (id, version)"
   1.105 +                    "values (1, '" PEP_ENGINE_VERSION "');",
   1.106 +                NULL,
   1.107 +                NULL,
   1.108 +                NULL
   1.109 +            );
   1.110 +            assert(int_result == SQLITE_OK);
   1.111 +        }
   1.112 +
   1.113          sql_log = "insert into log (title, entity, description, comment)"
   1.114                    "values (?1, ?2, ?3, ?4);";
   1.115