Define table schema and CSVs to populate the database

TL;DR:
- Created src/init.sql, which defines the tables
- Created a data/ directory to house .csv data to populate our db
- Updated the README with usage instructions
This commit is contained in:
William Carroll 2020-07-27 11:16:26 +01:00
parent 722205b081
commit df13b761ff
5 changed files with 82 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
*.db *.db
*.sqlite3
*.db-shm *.db-shm
*.db-wal *.db-wal

View file

@ -0,0 +1,44 @@
# TopTal take-home #2
All of the commands defined herein should be run from the top-level directory of
this repository (i.e. the directory in which this file exists).
## Database
Create a new database named `db.sqlite3` with:
```shell
$ sqlite3 db.sqlite3
```
Initialize the schema with:
```
sqlite> .read src/init.sql
```
You can verify that you successfully initialized the database by running:
```
sqlite> .tables
sqlite> .schema Accounts
sqlite> .schema Trips
```
Populate the database with some dummy values using the following:
```
sqlite> PRAGMA foreign_keys = on;
sqlite> .mode csv
sqlite> .import data/accounts.csv Accounts
sqlite> .import data/trips.csv Trips
```
You can verify you successfully populated the tables with:
```
sqlite> .mode columns
sqlite> .headers on
sqlite> SELECT * FROM Accounts;
sqlite> SELECT * FROM Trips;
```

3
data/accounts.csv Normal file
View file

@ -0,0 +1,3 @@
mimi,testing,miriamwright@google.com,user,
bill,testing,wpcarro@gmail.com,manager,
wpcarro,testing,wpcarro@google.com,admin,
1 mimi testing miriamwright@google.com user
2 bill testing wpcarro@gmail.com manager
3 wpcarro testing wpcarro@google.com admin

3
data/trips.csv Normal file
View file

@ -0,0 +1,3 @@
mimi,Rome,2020-08-10,2020-15-30,Heading home before the upcoming trip with Panarea.
mimi,Panarea,2020-08-15,2020-05-30,Exciting upcoming trip with Matt and Sarah!
mimi,London,2020-08-30,2020-08-30,Heading back to London...
1 mimi Rome 2020-08-10 2020-15-30 Heading home before the upcoming trip with Panarea.
2 mimi Panarea 2020-08-15 2020-05-30 Exciting upcoming trip with Matt and Sarah!
3 mimi London 2020-08-30 2020-08-30 Heading back to London...

31
src/init.sql Normal file
View file

@ -0,0 +1,31 @@
-- Run `.read init.sql` from within a SQLite3 REPL to initialize the tables we
-- need for this application. This will erase all current entries, so use with
-- caution.
-- Make sure to set `PRAGMA foreign_keys = on;` when transacting with the
-- database.
BEGIN TRANSACTION;
DROP TABLE IF EXISTS Accounts;
DROP TABLE IF EXISTS Trips;
CREATE TABLE Accounts (
username TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
role TEXT NOT NULL,
profilePicture BLOB,
PRIMARY KEY (username)
);
CREATE TABLE Trips (
username TEXT NOT NULL,
destination TEXT NOT NULL,
startDate TEXT NOT NULL, -- YYYY-MM-DD
endDate TEXT NOT NULL, -- YYYY-MM-DD
comment TEXT NOT NULL,
PRIMARY KEY (username, destination, startDate),
FOREIGN KEY (username) REFERENCES Accounts ON DELETE CASCADE
);
COMMIT;