Skip to content

Commit

Permalink
Merge pull request #214 from psrenergy/pr/sql-read-only
Browse files Browse the repository at this point in the history
Add read only mode
  • Loading branch information
guilhermebodin authored Apr 9, 2024
2 parents a708246 + 5f4a551 commit ebd81b0
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/PSRDatabaseSQLite/database_sqlite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function DatabaseSQLite_from_schema(
)
sqlite_db = SQLite.DB(database_path)

DBInterface.execute(sqlite_db, "PRAGMA busy_timeout = 5000;")

collections_map = try
execute_statements(sqlite_db, path_schema)
_validate_database(sqlite_db)
Expand All @@ -32,6 +34,8 @@ function DatabaseSQLite_from_migrations(
)
sqlite_db = SQLite.DB(database_path)

DBInterface.execute(sqlite_db, "PRAGMA busy_timeout = 5000;")

collections_map = try
current_version = get_user_version(sqlite_db)
most_recent_version = get_last_user_version(path_migrations)
Expand Down Expand Up @@ -59,9 +63,14 @@ function DatabaseSQLite_from_migrations(
end

function DatabaseSQLite(
database_path::String,
database_path::String;
read_only::Bool = false,
)
sqlite_db = SQLite.DB(database_path)
sqlite_db =
read_only ? SQLite.DB("file:" * database_path * "?mode=ro&immutable=1") :
SQLite.DB(database_path)

DBInterface.execute(sqlite_db, "PRAGMA busy_timeout = 5000;")

collections_map = try
_validate_database(sqlite_db)
Expand Down
5 changes: 3 additions & 2 deletions src/PSRDatabaseSQLite/psri_study_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ end

function PSRI.load_study(
::PSRDatabaseSQLiteInterface,
data_path::String,
data_path::String;
read_only::Bool = false,
)
return PSRDatabaseSQLite.load_db(data_path)
return PSRDatabaseSQLite.load_db(data_path; read_only = read_only)
end

function PSRI.load_study(
Expand Down
5 changes: 3 additions & 2 deletions src/PSRDatabaseSQLite/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ function create_empty_db_from_migrations(
return db
end

function load_db(database_path::String)
function load_db(database_path::String; read_only::Bool = false)
db = try
DatabaseSQLite(
database_path,
database_path;
read_only = read_only,
)
catch e
rethrow(e)
Expand Down
64 changes: 64 additions & 0 deletions test/PSRDatabaseSQLite/test_read_only/test_read_only.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module TestReadOnly

using PSRClassesInterface.PSRDatabaseSQLite
using SQLite
using Dates
using Test

function test_read_only()
path_schema = joinpath(@__DIR__, "test_read_only.sql")
db_path = joinpath(@__DIR__, "test_read_only.sqlite")
db = PSRDatabaseSQLite.create_empty_db_from_schema(db_path, path_schema; force = true)
PSRDatabaseSQLite.create_element!(
db,
"Configuration";
label = "Toy Case",
date_initial = DateTime(2020, 1, 1),
)
PSRDatabaseSQLite.create_element!(
db,
"Resource";
label = "Resource 1",
)
PSRDatabaseSQLite.create_element!(
db,
"Resource";
label = "Resource 2",
)

PSRDatabaseSQLite.close!(db)

db = PSRDatabaseSQLite.load_db(db_path; read_only = true)

@test PSRDatabaseSQLite.read_scalar_parameters(db, "Configuration", "label") ==
["Toy Case"]
@test PSRDatabaseSQLite.read_scalar_parameters(db, "Resource", "label") ==
["Resource 1", "Resource 2"]
@test PSRDatabaseSQLite.read_scalar_parameter(db, "Resource", "label", "Resource 1") ==
"Resource 1"

@test_throws SQLite.SQLiteException PSRDatabaseSQLite.create_element!(
db,
"Resource";
label = "Resource 3",
)

PSRDatabaseSQLite.close!(db)
return rm(db_path)
end

function runtests()
Base.GC.gc()
Base.GC.gc()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
end

TestReadOnly.runtests()

end
17 changes: 17 additions & 0 deletions test/PSRDatabaseSQLite/test_read_only/test_read_only.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
PRAGMA user_version = 1;
PRAGMA foreign_keys = ON;

CREATE TABLE Configuration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE,
value1 REAL NOT NULL DEFAULT 100,
date_initial TEXT,
enum1 TEXT NOT NULL DEFAULT 'A' CHECK(enum1 IN ('A', 'B', 'C'))
) STRICT;


CREATE TABLE Resource (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE,
type TEXT NOT NULL DEFAULT "D" CHECK(type IN ('D', 'E', 'F'))
) STRICT;

2 comments on commit ebd81b0

@guilhermebodin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104575

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.5 -m "<description of version>" ebd81b0458db3e5b735d3718b49d5b3785dd683e
git push origin v0.14.5

Please sign in to comment.