Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: db: introduce db interface #521

Closed
wants to merge 5 commits into from
Closed

Conversation

kwy95
Copy link
Collaborator

@kwy95 kwy95 commented Nov 25, 2021

In an effort to standardize our data collection and storage, as well as
improve scalability this introduces a library to create and interact
with sqlite3 databases. This will eventually replace the files and
folders found in KW_DATA_DIR with a unified database.

Signed-off-by: Rubens Gomes Neto rubens.gomes.neto@usp.br

@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2021

Codecov Report

Merging #521 (c9d484c) into unstable (e3c3e83) will increase coverage by 0.44%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##           unstable     #521      +/-   ##
============================================
+ Coverage     71.42%   71.87%   +0.44%     
============================================
  Files            31       32       +1     
  Lines          4451     4522      +71     
============================================
+ Hits           3179     3250      +71     
  Misses         1272     1272              
Impacted Files Coverage Δ
src/kw_db.sh 100.00% <100.00%> (ø)
src/kw_string.sh 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3c3e83...c9d484c. Read the comment docs.

Copy link
Collaborator

@rodrigosiqueira rodrigosiqueira left a comment

Choose a reason for hiding this comment

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

Nice PR!
Could you break it in small set of commits followed by documentation and tests?
Thanks.

Comment on lines 1 to 19
CREATE TABLE IF NOT EXISTS tags(
id INTEGER PRIMARY KEY,
tag VARCHAR(32) UNIQUE
);

CREATE TABLE IF NOT EXISTS pomodoro(
-- id INTEGER PRIMARY KEY,
tag_id INTEGER REFERENCES tags(id),
dt_start DATETIME NOT NULL,
dt_end DATETIME NOT NULL,
descript VARCHAR(512)
);

CREATE TABLE IF NOT EXISTS statistic(
-- id INTEGER PRIMARY KEY,
label VARCHAR(32),
dt_start DATETIME NOT NULL,
dt_end DATETIME NOT NULL
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we want to have this file here at this stage. We will need to discuss the data structure later... for now, you can move it to tests/samples/db_files and use it for test only.

src/kw_db.sh Show resolved Hide resolved
src/kw_db.sh Outdated
Comment on lines 4 to 5
DB_NAME='kw.db'
DB_PATH="$KW_ETC_DIR/db_files/$DB_NAME"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's use declare -g.

src/kw_db.sh Show resolved Hide resolved
src/kw_db.sh Outdated
Comment on lines 47 to 148
function read_sql()
{
local sql_path="$1"
local db="${2:-$DB_PATH}"

if [[ ! -f "$sql_path" ]]; then
printf '%s\n' "Could not find the file: $sql_path"
return 22
fi

sqlite3 "$db" < "$sql_path"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Commit 1: Add support to create a database by using an SQL file.

  1. Find a better name for this function
  2. Validate each parameter
  3. Add documentation
  4. Add unit test

src/kw_db.sh Outdated
Comment on lines 60 to 66
function apply_command()
{
local cmd="$1"
local db="${2:-$DB_PATH}"

sqlite3 "$db" -batch "$cmd"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Commit 2: Add support to run SQL commands
See previous comments.

src/kwio.sh Outdated
@@ -172,3 +172,23 @@ function ask_with_default()

printf '%s\n' "$response"
}

# Check if file exists and display an error message if supplied
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about something like this:
Check if the file exists, and if a message parameter is passed to this function, it will display that as an error message.

src/kw_db.sh Outdated
@@ -0,0 +1,30 @@
# This file handles the interactions with the kw database
# It should be used to read, write and modify data in kw's dbs
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think line 1 is enough here, and you can delete the second line.

src/kw_db.sh Outdated
Comment on lines 26 to 27
file_exists "$sql_path" || exit_msg "Could not find the file: $sql_path" 2
file_exists "$db_path" || warning "Creating database: $db_path"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You introduced a message parameter to file_exists in the previous commit. Why not use it here? Actually, you can add an extra parameter to file_exists for force exit, what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm thinking of removing the message from that function and only check if the file exists and return accordingly. And now I think it might be easier to just use [[ -f "$path" ]]. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree

src/kwio.sh Outdated
{
local file_path="$1"
local message="$2"

Copy link
Collaborator

Choose a reason for hiding this comment

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

how about adding one extra parameter to allow the developer to select the message type? For example, it would be nice to opt for a warning instead of complain message.


assertTrue "($LINENO) DB file should be created" '[[ -f "$KW_DATA_DIR/kw.db" ]]'

output=$(sqlite3 "$KW_DATA_DIR/kw.db" -cmd '.tables' -batch ';')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you just add a comment or two that describes the sqlite command you executed? I mean, I don't know what I should expect from that.
Maybe add a sample of the expected output?

src/kw_db.sh Outdated
Comment on lines 126 to 127
value="${value//\'/\'\'}" # escapes single quotes
values+="'$value'"
Copy link
Collaborator

Choose a reason for hiding this comment

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

value and values sound confusing in the code. Maybe you can you something else instead of value.

src/kw_db.sh Outdated
local value
local count=0

[[ "$#" == 0 ]] && exit_msg 'No arguments given' 22
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to exit here?

assert_equals_helper 'No arguments, should fail' "$LINENO" 22 "$ret"
assert_equals_helper 'Expected error msg' "$LINENO" "$output" "$expected"

output=$(format_entries_db 'first' 'second' 'third')
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about adding a test with a single value?

Comment on lines 79 to 83
cd "$FAKE_DATA" || {
ret="$?"
fail "($LINENO): Failed to move to fake data dir"
exit "$ret"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, I would prefer that we do not cd this folder.


output=$(format_values_db 1 "some 'quotes'")
ret="$?"
expected="('some ''quotes''')"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we have one extra quote here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope. There should be quotes around the full string, and then 1 extra quote for each quote inside the original string.

@rodrigosiqueira
Copy link
Collaborator

Btw, I don't think this is a draft anymore.

src/kw_db.sh Outdated
Comment on lines 26 to 27
file_exists "$sql_path" || exit_msg "Could not find the file: $sql_path" 2
file_exists "$db_path" || warning "Creating database: $db_path"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree

src/kw_db.sh Outdated

file_exists "$db_path" || exit_msg 'Database does not exist' 2

sqlite3 "$db" -batch "$sql_cmd"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should return something in case of a problem and let the function that invokes it handle the issue. However, I don't think we should add any warning message here because this is not something the user will understand in case of an error. Ideally, we should have the verbose or debug mode for handling these cases. (#179)

In an effort to standardize our data collection and storage, as well as
improve scalability this starts a library to create and interact
with `sqlite3` databases. This will eventually replace the files and
folders found in `KW_DATA_DIR` with a unified database.
This first commit creates a function `execute_sql` that executes a SQL
script on a given database.

Signed-off-by: Rubens Gomes Neto <rubens.gomes.neto@usp.br>
Introduce function to execute an SQL command in a given database.

Signed-off-by: Rubens Gomes Neto <rubens.gomes.neto@usp.br>
Adds a function that adds values to a given table of a given database.

Signed-off-by: Rubens Gomes Neto <rubens.gomes.neto@usp.br>
Adds `concatenate_with_commas` that returns the arguments as a comma
separated list.
Adds `format_values_db` that takes arguments and formats them to be
used with commands like `insert_into`.

Signed-off-by: Rubens Gomes Neto <rubens.gomes.neto@usp.br>
Adds a function to perform simple selects to any given table of a
database.

Signed-off-by: Rubens Gomes Neto <rubens.gomes.neto@usp.br>
@rodrigosiqueira
Copy link
Collaborator

Thanks a lot for this PR!
Merged to the unstable branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants