-
Notifications
You must be signed in to change notification settings - Fork 89
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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.
etc/db_files/tables.sql
Outdated
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 | ||
); |
There was a problem hiding this comment.
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
Outdated
DB_NAME='kw.db' | ||
DB_PATH="$KW_ETC_DIR/db_files/$DB_NAME" |
There was a problem hiding this comment.
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
Outdated
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" | ||
} |
There was a problem hiding this comment.
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.
- Find a better name for this function
- Validate each parameter
- Add documentation
- Add unit test
src/kw_db.sh
Outdated
function apply_command() | ||
{ | ||
local cmd="$1" | ||
local db="${2:-$DB_PATH}" | ||
|
||
sqlite3 "$db" -batch "$cmd" | ||
} |
There was a problem hiding this comment.
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.
829c111
to
15fc604
Compare
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
file_exists "$sql_path" || exit_msg "Could not find the file: $sql_path" 2 | ||
file_exists "$db_path" || warning "Creating database: $db_path" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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" | ||
|
There was a problem hiding this comment.
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 ';') |
There was a problem hiding this comment.
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
value="${value//\'/\'\'}" # escapes single quotes | ||
values+="'$value'" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
tests/kw_db_test.sh
Outdated
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') |
There was a problem hiding this comment.
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?
tests/kw_db_test.sh
Outdated
cd "$FAKE_DATA" || { | ||
ret="$?" | ||
fail "($LINENO): Failed to move to fake data dir" | ||
exit "$ret" | ||
} |
There was a problem hiding this comment.
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''')" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Btw, I don't think this is a draft anymore. |
src/kw_db.sh
Outdated
file_exists "$sql_path" || exit_msg "Could not find the file: $sql_path" 2 | ||
file_exists "$db_path" || warning "Creating database: $db_path" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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>
Thanks a lot for this PR! |
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 andfolders found in
KW_DATA_DIR
with a unified database.Signed-off-by: Rubens Gomes Neto rubens.gomes.neto@usp.br