-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seed a sqlite db ready for migration
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
# db name | ||
DATABASE="moby.db" | ||
|
||
if [ "$#" -ne 3 ]; then | ||
echo "Usage: $0 <CONTENT_TYPE> <FILE_NAME> <BOOK_TYPE>" | ||
exit 1 | ||
fi | ||
|
||
# Read parameters | ||
CONTENT_TYPE=$1 | ||
FILE_NAME=$2 | ||
BOOK_TYPE=$3 | ||
|
||
echo $CONTENT_TYPE | ||
echo $FILE_NAME | ||
echo $BOOK_TYPE | ||
|
||
# Create the table if it doesn't exist | ||
sqlite3 $DATABASE <<EOF | ||
CREATE TABLE IF NOT EXISTS $CONTENT_TYPE ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
identifier INTEGER, | ||
bookType TEXT CHECK(bookType = 'moby-dick' or bookType = 'alice') DEFAULT 'moby-dick', | ||
content TEXT | ||
) | ||
EOF | ||
|
||
# Read and parse the JSON file, then insert data into the database | ||
jq-win64 -c '.[]' $FILE_NAME | while read -r row; do | ||
identifier=$(echo $row | jq-win64 -r '.id') | ||
content=$(echo $row | jq-win64 -r '.content') | ||
bookType=$(echo $BOOK_TYPE) | ||
|
||
sqlite3 $DATABASE <<EOF | ||
INSERT INTO $CONTENT_TYPE (identifier, content, bookType) VALUES ('$identifier', '$(echo "$content" | sed "s/'/''/g")', '$bookType'); | ||
EOF | ||
|
||
done | ||
|
||
echo "Data inserted successfully for file $FILE_NAME" |