Skip to content

Commit

Permalink
add new 'weekday' column to database
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Feb 22, 2024
1 parent d60b209 commit 7e05b3a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
33 changes: 28 additions & 5 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"fmt"
"strings"

_ "github.com/tursodatabase/libsql-client-go/libsql"
_ "modernc.org/sqlite"
Expand All @@ -21,6 +22,7 @@ type Dog struct {
Service string
Quantity int
Price float64
Weekday int
}

func Init() error {
Expand All @@ -32,6 +34,11 @@ func Init() error {
// No need to check for error here, if the connection can be made, the tables will be created
_ = createTables()

err = updateTables()
if err != nil {
return fmt.Errorf("error updating tables: %v", err)
}

return nil
}

Expand All @@ -58,6 +65,7 @@ func createTables() error {
service TEXT,
quantity INTEGER,
price INTEGER
weekday INTEGER
);
`)
if err != nil {
Expand All @@ -67,6 +75,17 @@ func createTables() error {
return nil
}

func updateTables() error {
_, err := db.Exec(`
ALTER TABLE dogs ADD COLUMN weekday INTEGER;
`)
if err == nil || strings.Contains(err.Error(), "duplicate column name") {
return nil
}

return fmt.Errorf("error updating dogs table: %v", err)
}

func getDogs() ([]Dog, error) {
rows, err := db.Query("SELECT * FROM dogs")
if err != nil {
Expand All @@ -86,6 +105,7 @@ func getDogs() ([]Dog, error) {
&dog.Service,
&dog.Quantity,
&dog.Price,
&dog.Weekday,
)
if err != nil {
rows.Close()
Expand All @@ -109,6 +129,7 @@ func getDog(id int) (Dog, error) {
&dog.Service,
&dog.Quantity,
&dog.Price,
&dog.Weekday,
)
if err != nil {
return Dog{}, fmt.Errorf("error getting dog: %v", err)
Expand All @@ -127,9 +148,10 @@ func addDog(dog Dog) error {
email,
service,
quantity,
price
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price)
price,
weekday
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.Weekday)
if err != nil {
return fmt.Errorf("error adding dog: %v", err)
}
Expand All @@ -147,9 +169,10 @@ func updateDog(dog Dog) error {
email = ?,
service = ?,
quantity = ?,
price = ?
price = ?,
weekday = ?
WHERE id = ?
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.ID)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.Weekday, dog.ID)
if err != nil {
return fmt.Errorf("error updating dog: %v", err)
}
Expand Down
29 changes: 26 additions & 3 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var testDog Dog = Dog{
Service: "walk",
Quantity: 1,
Price: 25,
Weekday: 2,
}

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -80,9 +81,10 @@ func TestCreateTables(t *testing.T) {
email,
service,
quantity,
price
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, testDog.Name, testDog.OwnerName, testDog.Address, testDog.City, testDog.Email, testDog.Service, testDog.Quantity, testDog.Price)
price,
weekday
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, testDog.Name, testDog.OwnerName, testDog.Address, testDog.City, testDog.Email, testDog.Service, testDog.Quantity, testDog.Price, testDog.Weekday)

if err != nil {
t.Errorf("createTables() error = %q", err)
Expand All @@ -105,6 +107,27 @@ func TestCreateTables(t *testing.T) {
})
}

func TestUpdateTables(t *testing.T) {
err := updateTables()
if err != nil {
t.Errorf("updateTables() error = %q", err)
}

// Force error
oldDbUrl := dbUrl
dbUrl = "someBadUrl"
_ = connect()
err = updateTables()
if err == nil {
t.Errorf("no error returned when expected")
}

t.Cleanup(func() {
dbUrl = oldDbUrl
_ = connect()
})
}

func TestAddDog(t *testing.T) {
err := addDog(testDog)
if err != nil {
Expand Down

0 comments on commit 7e05b3a

Please sign in to comment.