Skip to content

Commit 16ff30f

Browse files
committed
sm: database: handle preinstalled instance ident field
Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
1 parent e15bf1f commit 16ff30f

6 files changed

Lines changed: 175 additions & 20 deletions

File tree

src/sm/database/database.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ Error Database::GetAllInstancesInfos([[maybe_unused]] Array<InstanceInfo>& infos
367367
try {
368368
std::vector<InstanceInfoRow> rows;
369369

370-
*mSession
371-
<< "SELECT itemID, subjectID, instance, type, version, manifestDigest, runtimeID, ownerID, subjectType, "
372-
"uid, gid, priority, storagePath, statePath, envVars, networkParameters, monitoringParams "
373-
"FROM instances;",
370+
*mSession << "SELECT itemID, subjectID, instance, type, preinstalled, version, manifestDigest, "
371+
"runtimeID, ownerID, subjectType,uid, gid, priority, storagePath, statePath, "
372+
"envVars,networkParameters, monitoringParams "
373+
"FROM instances;",
374374
into(rows), now;
375375

376376
auto instanceInfo = std::make_unique<InstanceInfo>();
@@ -400,9 +400,10 @@ Error Database::UpdateInstanceInfo(const InstanceInfo& info)
400400

401401
FromAos(info, row);
402402

403-
*mSession << "INSERT OR REPLACE INTO instances (itemID, subjectID, instance, type, version, manifestDigest, "
404-
"runtimeID, ownerID, subjectType, uid, gid, priority, storagePath, statePath, envVars, "
405-
"networkParameters, monitoringParams) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
403+
*mSession
404+
<< "INSERT OR REPLACE INTO instances (itemID, subjectID, instance, type, preinstalled, version, "
405+
"manifestDigest, runtimeID, ownerID, subjectType, uid, gid, priority, storagePath, statePath, envVars, "
406+
"networkParameters, monitoringParams) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
406407
bind(row), now;
407408
} catch (const std::exception& e) {
408409
return AOS_ERROR_WRAP(common::utils::ToAosError(e));
@@ -420,9 +421,10 @@ Error Database::RemoveInstanceInfo(const InstanceIdent& ident)
420421
try {
421422
Poco::Data::Statement statement {*mSession};
422423

423-
statement << "DELETE FROM instances WHERE itemID = ? AND subjectID = ? AND instance = ? AND type = ?;",
424+
statement << "DELETE FROM instances WHERE itemID = ? AND subjectID = ? AND instance = ? AND type = ? "
425+
"AND preinstalled = ?;",
424426
bind(ident.mItemID.CStr()), bind(ident.mSubjectID.CStr()), bind(ident.mInstance),
425-
bind(ident.mType.ToString().CStr());
427+
bind(ident.mType.ToString().CStr()), bind(ident.mPreinstalled);
426428

427429
if (statement.execute() == 0) {
428430
return AOS_ERROR_WRAP(ErrorEnum::eNotFound);
@@ -759,6 +761,7 @@ void Database::FromAos(const InstanceInfo& src, InstanceInfoRow& dst)
759761
dst.set<ToInt(InstanceInfoColumns::eSubjectID)>(src.mSubjectID.CStr());
760762
dst.set<ToInt(InstanceInfoColumns::eInstance)>(src.mInstance);
761763
dst.set<ToInt(InstanceInfoColumns::eType)>(src.mType.ToString().CStr());
764+
dst.set<ToInt(InstanceInfoColumns::ePreinstalled)>(src.mPreinstalled);
762765
dst.set<ToInt(InstanceInfoColumns::eVersion)>(src.mVersion.CStr());
763766
dst.set<ToInt(InstanceInfoColumns::eManifestDigest)>(src.mManifestDigest.CStr());
764767
dst.set<ToInt(InstanceInfoColumns::eRuntimeID)>(src.mRuntimeID.CStr());
@@ -779,6 +782,7 @@ void Database::ToAos(const InstanceInfoRow& src, InstanceInfo& dst)
779782
dst.mItemID = src.get<ToInt(InstanceInfoColumns::eItemID)>().c_str();
780783
dst.mSubjectID = src.get<ToInt(InstanceInfoColumns::eSubjectID)>().c_str();
781784
dst.mInstance = src.get<ToInt(InstanceInfoColumns::eInstance)>();
785+
dst.mPreinstalled = src.get<ToInt(InstanceInfoColumns::ePreinstalled)>();
782786
dst.mVersion = src.get<ToInt(InstanceInfoColumns::eVersion)>().c_str();
783787
dst.mManifestDigest = src.get<ToInt(InstanceInfoColumns::eManifestDigest)>().c_str();
784788
dst.mRuntimeID = src.get<ToInt(InstanceInfoColumns::eRuntimeID)>().c_str();

src/sm/database/database.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Database : public sm::launcher::StorageItf, public sm::networkmanager::Sto
171171
Error GetJournalCursor(String& cursor) const override;
172172

173173
private:
174-
static constexpr int sVersion = 3;
174+
static constexpr int sVersion = 4;
175175
static constexpr auto cDBFileName = "servicemanager.db";
176176

177177
// Instance info columns
@@ -180,6 +180,7 @@ class Database : public sm::launcher::StorageItf, public sm::networkmanager::Sto
180180
eSubjectID,
181181
eInstance,
182182
eType,
183+
ePreinstalled,
183184
eVersion,
184185
eManifestDigest,
185186
eRuntimeID,
@@ -194,9 +195,9 @@ class Database : public sm::launcher::StorageItf, public sm::networkmanager::Sto
194195
eNetworkParameters,
195196
eMonitoringParams
196197
};
197-
using InstanceInfoRow = Poco::Tuple<std::string, std::string, uint64_t, std::string, std::string, std::string,
198-
std::string, std::string, std::string, uint32_t, uint32_t, uint64_t, std::string, std::string, std::string,
199-
std::string, std::string>;
198+
using InstanceInfoRow = Poco::Tuple<std::string, std::string, uint64_t, std::string, uint32_t, std::string,
199+
std::string, std::string, std::string, std::string, uint32_t, uint32_t, uint64_t, std::string, std::string,
200+
std::string, std::string, std::string>;
200201

201202
// Network info columns
202203
enum class NetworkInfoColumns : int { eNetworkID = 0, eIP, eSubnet, eVlanID, eVlanIfName, eBridgeIfName };
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
BEGIN TRANSACTION;
2+
3+
4+
-- Create new instances table with updated schema
5+
CREATE TABLE instances_new (
6+
itemID TEXT NOT NULL,
7+
subjectID TEXT NOT NULL,
8+
instance INTEGER NOT NULL,
9+
type TEXT NOT NULL DEFAULT 'service',
10+
version TEXT,
11+
manifestDigest TEXT,
12+
runtimeID TEXT,
13+
ownerID TEXT,
14+
subjectType TEXT,
15+
uid INTEGER,
16+
gid INTEGER,
17+
priority INTEGER,
18+
storagePath TEXT,
19+
statePath TEXT,
20+
envVars TEXT,
21+
networkParameters TEXT,
22+
monitoringParams TEXT,
23+
PRIMARY KEY(itemID, subjectID, instance, type)
24+
);
25+
26+
INSERT INTO instances_new (
27+
itemID,
28+
subjectID,
29+
instance,
30+
type,
31+
version,
32+
manifestDigest,
33+
runtimeID,
34+
ownerID,
35+
subjectType,
36+
uid,
37+
gid,
38+
priority,
39+
storagePath,
40+
statePath,
41+
envVars,
42+
networkParameters,
43+
monitoringParams
44+
)
45+
SELECT
46+
itemID,
47+
subjectID,
48+
instance,
49+
type,
50+
version,
51+
manifestDigest,
52+
runtimeID,
53+
ownerID,
54+
subjectType,
55+
uid,
56+
gid,
57+
priority,
58+
storagePath,
59+
statePath,
60+
envVars,
61+
networkParameters,
62+
monitoringParams
63+
FROM instances;
64+
65+
DROP TABLE instances;
66+
67+
ALTER TABLE instances_new RENAME TO instances;
68+
69+
70+
COMMIT;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
BEGIN TRANSACTION;
2+
3+
-- Create new instances table with updated schema
4+
CREATE TABLE instances_new (
5+
itemID TEXT NOT NULL,
6+
subjectID TEXT NOT NULL,
7+
instance INTEGER NOT NULL,
8+
type TEXT NOT NULL DEFAULT 'service',
9+
preinstalled INTEGER NOT NULL DEFAULT 0,
10+
version TEXT,
11+
manifestDigest TEXT,
12+
runtimeID TEXT,
13+
ownerID TEXT,
14+
subjectType TEXT,
15+
uid INTEGER,
16+
gid INTEGER,
17+
priority INTEGER,
18+
storagePath TEXT,
19+
statePath TEXT,
20+
envVars TEXT,
21+
networkParameters TEXT,
22+
monitoringParams TEXT,
23+
PRIMARY KEY(itemID, subjectID, instance, type, preinstalled)
24+
);
25+
26+
INSERT INTO instances_new (
27+
itemID,
28+
subjectID,
29+
instance,
30+
type,
31+
preinstalled,
32+
version,
33+
manifestDigest,
34+
runtimeID,
35+
ownerID,
36+
subjectType,
37+
uid,
38+
gid,
39+
priority,
40+
storagePath,
41+
statePath,
42+
envVars,
43+
networkParameters,
44+
monitoringParams
45+
)
46+
SELECT
47+
itemID,
48+
subjectID,
49+
instance,
50+
type,
51+
0,
52+
version,
53+
manifestDigest,
54+
runtimeID,
55+
ownerID,
56+
subjectType,
57+
uid,
58+
gid,
59+
priority,
60+
storagePath,
61+
statePath,
62+
envVars,
63+
networkParameters,
64+
monitoringParams
65+
FROM instances;
66+
67+
DROP TABLE instances;
68+
69+
ALTER TABLE instances_new RENAME TO instances;
70+
71+
COMMIT;

src/sm/database/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ set(SOURCES database.cpp)
1616
# Libraries
1717
# ######################################################################################################################
1818

19-
set(LIBRARIES aos::sm::database GTest::gmock_main)
19+
set(LIBRARIES aos::sm::database aos::core::common::tests::utils GTest::gmock_main)
2020

2121
# ######################################################################################################################
2222
# Target

src/sm/database/tests/database.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <gmock/gmock.h>
1010

1111
#include <core/common/tests/utils/log.hpp>
12+
#include <core/common/tests/utils/utils.hpp>
1213

1314
#include <sm/database/database.hpp>
1415

@@ -103,8 +104,11 @@ TEST_F(DatabaseTest, UpdateInstanceInfo)
103104

104105
auto instanceInfo = CreateInstanceInfo("service-1", "subject-1", 1);
105106

106-
ASSERT_TRUE(mDB.UpdateInstanceInfo(instanceInfo).IsNone());
107-
ASSERT_TRUE(mDB.UpdateInstanceInfo(instanceInfo).IsNone());
107+
auto err = mDB.UpdateInstanceInfo(instanceInfo);
108+
ASSERT_TRUE(err.IsNone()) << aos::tests::utils::ErrorToStr(err);
109+
110+
err = mDB.UpdateInstanceInfo(instanceInfo);
111+
ASSERT_TRUE(err.IsNone()) << aos::tests::utils::ErrorToStr(err);
108112
}
109113

110114
TEST_F(DatabaseTest, RemoveInstanceInfo)
@@ -126,16 +130,20 @@ TEST_F(DatabaseTest, RemoveInstanceInfo)
126130

127131
TEST_F(DatabaseTest, GetAllInstancesInfos)
128132
{
129-
ASSERT_TRUE(mDB.Init(mWorkingDir.string(), mMigrationConfig).IsNone());
133+
auto err = mDB.Init(mWorkingDir.string(), mMigrationConfig);
134+
ASSERT_TRUE(err.IsNone()) << aos::tests::utils::ErrorToStr(err);
130135

131-
auto instanceInfo = CreateInstanceInfo("service-1", "subject-1", 1);
136+
auto instanceInfo = CreateInstanceInfo("service-1", "subject-1", 1);
137+
instanceInfo.mPreinstalled = true;
132138

133-
ASSERT_TRUE(mDB.UpdateInstanceInfo(instanceInfo).IsNone());
139+
err = mDB.UpdateInstanceInfo(instanceInfo);
140+
ASSERT_TRUE(err.IsNone()) << aos::tests::utils::ErrorToStr(err);
134141

135142
// aos::InstanceInfoArray result;
136143
auto result = std::make_unique<aos::InstanceInfoArray>();
137144

138-
ASSERT_TRUE(mDB.GetAllInstancesInfos(*result).IsNone());
145+
err = mDB.GetAllInstancesInfos(*result);
146+
ASSERT_TRUE(err.IsNone()) << aos::tests::utils::ErrorToStr(err);
139147

140148
ASSERT_EQ(result->Size(), 1);
141149

@@ -145,6 +153,7 @@ TEST_F(DatabaseTest, GetAllInstancesInfos)
145153
EXPECT_EQ(resultRef.mSubjectID, instanceInfo.mSubjectID);
146154
EXPECT_EQ(resultRef.mInstance, instanceInfo.mInstance);
147155
EXPECT_EQ(resultRef.mType, instanceInfo.mType);
156+
EXPECT_TRUE(resultRef.mPreinstalled);
148157
EXPECT_EQ(resultRef.mManifestDigest, instanceInfo.mManifestDigest);
149158
EXPECT_EQ(resultRef.mRuntimeID, instanceInfo.mRuntimeID);
150159
EXPECT_EQ(resultRef.mSubjectType, instanceInfo.mSubjectType);

0 commit comments

Comments
 (0)