1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <dbm_migration.h>
26 
27 #include <lastseen.h>
28 #include <string_lib.h>
29 
30 extern const DBMigrationFunction dbm_migration_plan_lastseen[];
31 
32 
33 #ifdef LMDB
DBMigrate(ARG_UNUSED DBHandle * db,ARG_UNUSED dbid id)34 bool DBMigrate(ARG_UNUSED DBHandle *db, ARG_UNUSED  dbid id)
35 {
36     return true;
37 }
38 #else
DBVersion(DBHandle * db)39 static size_t DBVersion(DBHandle *db)
40 {
41     char version[64];
42     if (ReadDB(db, "version", version, sizeof(version)) == false)
43     {
44         return 0;
45     }
46     else
47     {
48         return StringToLongDefaultOnError(version, 0);
49     }
50 }
51 
52 static const DBMigrationFunction *const dbm_migration_plans[dbid_max] = {
53     [dbid_lastseen] = dbm_migration_plan_lastseen
54 };
55 
DBMigrate(DBHandle * db,dbid id)56 bool DBMigrate(DBHandle *db, dbid id)
57 {
58     const DBMigrationFunction *plan = dbm_migration_plans[id];
59 
60     if (plan)
61     {
62         size_t step_version = 0;
63         for (const DBMigrationFunction *step = plan; *step; step++, step_version++)
64         {
65             if (step_version == DBVersion(db))
66             {
67                 if (!(*step)(db))
68                 {
69                     return false;
70                 }
71             }
72         }
73     }
74     return true;
75 }
76 #endif
77