Skip to content

Commit

Permalink
Merge pull request #218 from psrenergy/pr/update-label-rules
Browse files Browse the repository at this point in the history
Update label rules
  • Loading branch information
guilhermebodin authored Apr 29, 2024
2 parents 4b2c457 + fb36d9f commit ee8225a
Show file tree
Hide file tree
Showing 41 changed files with 179 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PSRClassesInterface"
uuid = "1eab49e5-27d8-4905-b9f6-327b6ea666c4"
version = "0.14.5"
version = "0.15.0"

[deps]
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
Expand Down
1 change: 1 addition & 0 deletions docs/src/psrdatabasesqlite/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CREATE TABLE Configuration (
### Non-vector Attributes

- The name of an Attribute should be in snake case and be in singular form.
- If the attribute's name is `label`, it should be stored as a `TEXT` and have the `UNIQUE` and `NOT NULL` constraints.

Example:
```sql
Expand Down
178 changes: 0 additions & 178 deletions src/PSRDatabaseSQLite/README.md

This file was deleted.

47 changes: 47 additions & 0 deletions src/PSRDatabaseSQLite/database_sqlite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,53 @@ function table_info(db::SQLite.DB, table_name::String)
return df
end

function _check_column_type(
db::SQLite.DB,
table_name::String,
column_name::String,
expected_type::String,
)
df = table_info(db, table_name)
for row in eachrow(df)
if row.name == column_name
if row.type != expected_type
return false
else
return true
end
return
end
end
return error("Column $column_name not found in table $table_name.")
end

function _is_column_not_null(db::SQLite.DB, table_name::String, column_name::String)
df = table_info(db, table_name)
return any(x -> (x.name == column_name && x.notnull == 1), eachrow(df))
end

function _is_column_unique(db::SQLite.DB, table_name::String, column_name::String)
if !(table_name in table_names(db))
error("Table $table_name not found in database.")
end
if !(column_name in column_names(db, table_name))
error("Column $column_name not found in table $table_name.")
end
query_index_list = "PRAGMA index_list('$(table_name)');"
index_list = DBInterface.execute(db, query_index_list) |> DataFrame
if count(x -> x == 1, index_list.unique) == 0
false
end
for row in eachrow(index_list)
query_index_info = "PRAGMA index_info('$(row.name)');"
index_info = DBInterface.execute(db, query_index_info) |> DataFrame
if (index_info.name[1] == column_name)
return true
end
end
return false
end

function foreign_keys_list(db::SQLite.DB, table_name::String)
query = "PRAGMA foreign_key_list($table_name);"
df = DBInterface.execute(db, query) |> DataFrame
Expand Down
14 changes: 14 additions & 0 deletions src/PSRDatabaseSQLite/validate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ function _validate_table(db::SQLite.DB, table::String)
@error("Table $table does not have an \"id\" column.")
num_errors += 1
end
if ("label" in attributes)
if !_is_column_unique(db, table, "label")
@error("Table $table has a non-unique \"label\" column.")
num_errors += 1
end
if !_is_column_not_null(db, table, "label")
@error("Table $table has a nullable \"label\" column.")
num_errors += 1
end
if !_check_column_type(db, table, "label", "TEXT")
@error("Table $table has a non-text \"label\" column.")
num_errors += 1
end
end
for attribute in attributes
num_errors += _validate_column_name(table, attribute)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PRAGMA foreign_keys = ON;

CREATE TABLE Configuration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE
label TEXT UNIQUE NOT NULL
);

CREATE TABLE TestOne (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE
label TEXT UNIQUE NOT NULL
);

CREATE TABLE TestTwo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE,
label TEXT UNIQUE NOT NULL,
capacity INTEGER,
some_coefficient REAL
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PRAGMA user_version = 3;

CREATE TABLE TestThree (
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE,
label TEXT UNIQUE NOT NULL,
capacity INTEGER,
some_other_coefficient REAL
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE Configuration (

CREATE TABLE Product (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
label TEXT UNIQUE NOT NULL,
unit TEXT NOT NULL,
initial_availability REAL DEFAULT 0.0,
sell_limit REAL,
Expand All @@ -19,7 +19,7 @@ CREATE TABLE Product (

CREATE TABLE Process (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
label TEXT UNIQUE NOT NULL,
capex REAL,
opex REAL,
base_capacity REAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PRAGMA foreign_keys = OFF;
BEGIN TRANSACTION;
CREATE TABLE Product_new (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
label TEXT UNIQUE NOT NULL,
unit TEXT NOT NULL,
initial_availability REAL DEFAULT 0.0 NOT NULL,
sell_limit REAL,
Expand All @@ -65,7 +65,7 @@ ALTER TABLE Product_new
RENAME TO Product;
CREATE TABLE Process_new (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
label TEXT UNIQUE NOT NULL,
capex REAL NOT NULL,
opex REAL NOT NULL,
base_capacity REAL NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion test/PSRDatabaseSQLite/test_create/test_create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function test_create_empty_parameters()
path_schema = joinpath(@__DIR__, "test_create_parameters.sql")
db_path = joinpath(@__DIR__, "test_create_empty_parameters.sqlite")
db = PSRDatabaseSQLite.create_empty_db_from_schema(db_path, path_schema; force = true)
PSRDatabaseSQLite.create_element!(db, "Configuration")
PSRDatabaseSQLite.create_element!(db, "Configuration"; label = "Toy Case")
@test_throws PSRDatabaseSQLite.DatabaseException PSRDatabaseSQLite.create_element!(
db,
"Resource",
Expand Down
4 changes: 2 additions & 2 deletions test/PSRDatabaseSQLite/test_create/test_create_parameters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ PRAGMA foreign_keys = ON;

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

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

2 comments on commit ee8225a

@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/105850

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.15.0 -m "<description of version>" ee8225a0b326e616e6505a08b8a051a3d24db13d
git push origin v0.15.0

Please sign in to comment.