Skip to content

Commit

Permalink
Allow updating some vector attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroripper committed Oct 14, 2024
1 parent 9d95af3 commit 4045c66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/PSRDatabaseSQLite/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,32 @@ function _update_vector_parameters!(
Dict(Symbol(attribute_id) => vals),
)
else
# If there are rows in the table we must check that the number of rows is the same as the number of new relations
psr_database_sqlite_error(
"There is currently a vector of $num_rows_in_query elements in the group $group_id. " *
"User is trying to set a vector of length $num_new_elements. This is invalid. " *
"If you want to change the number of elements in the group you might have to delete " *
"the element and create it again with the new vector.",
)
collection = _get_collection(db, collection_id)
if keys(collection.vector_parameters) == [attribute_id]
DBInterface.execute(db.sqlite_db, "BEGIN TRANSACTION")
try
statement = "DELETE FROM $table_name WHERE id = '$id'"
DBInterface.execute(db.sqlite_db, statement)
_create_vectors!(
db,
collection_id,
id,
Dict(Symbol(attribute_id) => vals),
)
DBInterface.execute(db.sqlite_db, "COMMIT TRANSACTION")
catch e
DBInterface.execute(db.sqlite_db, "ROLLBACK TRANSACTION")
rethrow(e)
end
else
# If there are rows in the table we must check that the number of rows is the same as the number of new relations
psr_database_sqlite_error(
"There is currently a vector of $num_rows_in_query elements in the group $group_id. " *
"User is trying to set a vector of length $num_new_elements. This is invalid. " *
"If you want to change the number of elements in the group you might have to delete " *
"the element and create it again with the new vector.",
)
end
end
else
# Update the elements
Expand Down
11 changes: 10 additions & 1 deletion test/PSRDatabaseSQLite/test_update/test_update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,16 @@ function test_update_vector_parameters()
path_schema = joinpath(@__DIR__, "test_update_vector_parameters.sql")
db_path = joinpath(@__DIR__, "test_update_vector_parameters.sqlite")
db = PSRDatabaseSQLite.create_empty_db_from_schema(db_path, path_schema; force = true)
PSRDatabaseSQLite.create_element!(db, "Configuration"; label = "Toy Case", value1 = 1.0)
PSRDatabaseSQLite.create_element!(db, "Configuration"; label = "Toy Case", value1 = 1.0,
some_value_1 = [1.0, 2.0, 3.0])
PSRDatabaseSQLite.update_vector_parameters!(
db,
"Configuration",
"some_value_1",
"Toy Case",
[4.0, 5.0, 6.0],
)

PSRDatabaseSQLite.create_element!(
db,
"Resource";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ CREATE TABLE Configuration (
enum1 TEXT NOT NULL DEFAULT 'A' CHECK(enum1 IN ('A', 'B', 'C'))
) STRICT;

CREATE TABLE Configuration_vector_some_group (
id INTEGER,
vector_index INTEGER NOT NULL,
some_value_1 REAL NOT NULL,
FOREIGN KEY(id) REFERENCES Configuration(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id, vector_index)
) STRICT;

CREATE TABLE Resource (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL,
Expand Down

0 comments on commit 4045c66

Please sign in to comment.