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

Updated the location database schema #250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ ARG TARGETPLATFORM
COPY dist/$TARGETPLATFORM/bin/hotrod /app/hotrod

ENTRYPOINT ["/app/hotrod"]

51 changes: 35 additions & 16 deletions services/location/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ CREATE TABLE IF NOT EXISTS locations
id bigint unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
coordinates varchar(255) NOT NULL,
description varchar(255), -- New column

PRIMARY KEY (id),
UNIQUE KEY name (name)
UNIQUE KEY name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
`

Expand All @@ -58,26 +59,31 @@ var seed = []Location{
ID: 1,
Name: "My Home",
Coordinates: "231,773",
Description: "Cozy two-bedroom apartment", // New field
},
{
ID: 123,
Name: "Rachel's Floral Designs",
Coordinates: "115,277",
Description: "Flower shop specializing in arrangements",
},
{
ID: 567,
Name: "Amazing Coffee Roasters",
Coordinates: "211,653",
Description: "Local coffee roaster with a variety of blends",
},
{
ID: 392,
Name: "Trom Chocolatier",
Coordinates: "577,322",
Description: "Artisan chocolate and confectionery",
},
{
ID: 731,
Name: "Japanese Desserts",
Coordinates: "728,326",
Description: "Traditional Japanese sweets and pastries",
},
}

Expand Down Expand Up @@ -136,11 +142,11 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
semconv.PeerServiceKey.String("mysql"),
attribute.
Key("sql.query").
String("SELECT id, name, coordinates FROM locations"),
String("SELECT id, name, coordinates, description FROM locations"),
)
defer span.End()

query := "SELECT id, name, coordinates FROM locations"
query := "SELECT id, name, coordinates, description FROM locations"
rows, err := d.db.Query(query)
if err != nil {
if !d.shouldRetry(err) {
Expand All @@ -155,7 +161,7 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
var cs []Location
for rows.Next() {
c := Location{}
if err := rows.Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
if err := rows.Scan(&c.ID, &c.Name, &c.Coordinates, &c.Description); err != nil {
return nil, err
}
cs = append(cs, c)
Expand All @@ -168,13 +174,13 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
}

func (d *database) Create(ctx context.Context, location *Location) (int64, error) {
query := "INSERT INTO locations SET name = ?, coordinates = ?"
res, err := d.db.Exec(query, location.Name, location.Coordinates)
query := "INSERT INTO locations SET name = ?, coordinates = ?, description = ?"
res, err := d.db.Exec(query, location.Name, location.Coordinates, location.Description)
if err != nil {
if !d.shouldRetry(err) {
return 0, err
}
res, err = d.db.Exec(query, location.Name, location.Coordinates)
res, err = d.db.Exec(query, location.Name, location.Coordinates, location.Description)
if err != nil {
return 0, err
}
Expand All @@ -187,13 +193,13 @@ func (d *database) Create(ctx context.Context, location *Location) (int64, error
}

func (d *database) Update(ctx context.Context, location *Location) error {
query := "UPDATE locations SET name = ?, coordinates = ? WHERE id = ?"
res, err := d.db.Exec(query, location.Name, location.Coordinates, location.ID)
query := "UPDATE locations SET name = ?, coordinates = ?, description = ? WHERE id = ?"
res, err := d.db.Exec(query, location.Name, location.Coordinates, location.Description, location.ID)
if err != nil {
if !d.shouldRetry(err) {
return err
}
res, err = d.db.Exec(query, location.Name, location.Coordinates, location.ID)
res, err = d.db.Exec(query, location.Name, location.Coordinates, location.Description, location.ID)
if err != nil {
return err
}
Expand All @@ -216,7 +222,7 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
semconv.PeerServiceKey.String("mysql"),
attribute.
Key("sql.query").
String(fmt.Sprintf("SELECT id, name, coordinates from locations WHERE id = %d", locationID)),
String(fmt.Sprintf("SELECT id, name, coordinates, description FROM locations WHERE id = %d", locationID)),
)
defer span.End()

Expand All @@ -230,7 +236,7 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
delay.Sleep(config.GetMySQLGetDelay(), config.GetMySQLGetDelayStdDev())

var c Location
query := "SELECT id, name, coordinates FROM locations WHERE id = ?"
query := "SELECT id, name, coordinates, description FROM locations WHERE id = ?"
row := d.db.QueryRow(query, locationID)
if row.Err() != nil {
if !d.shouldRetry(row.Err()) {
Expand All @@ -241,7 +247,7 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
return nil, row.Err()
}
}
if err := row.Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
if err := row.Scan(&c.ID, &c.Name, &c.Coordinates, &c.Description); err != nil {
return nil, err
}
return &c, nil
Expand Down Expand Up @@ -271,22 +277,35 @@ func (d *database) shouldRetry(err error) bool {
return false
}

func (d *database) Init() error {
// Initialize the database schema
d.setupDB()
return nil
}

func (d *database) setupDB() {
// Create the table
// Create the table if it doesn't exist
fmt.Println("creating locations table")
_, err := d.db.Exec(tableSchema)
if err != nil {
panic(err)
}

// Alter the table to add the description column if it doesn't exist
fmt.Println("ensuring description column exists")
_, err = d.db.Exec("ALTER TABLE locations ADD COLUMN IF NOT EXISTS description VARCHAR(255)")
if err != nil {
panic(err)
}

fmt.Println("seeding database")
stmt, err := d.db.Prepare("INSERT INTO locations (id, name, coordinates) VALUES (?, ?, ?)")
stmt, err := d.db.Prepare("INSERT INTO locations (id, name, coordinates, description) VALUES (?, ?, ?, ?)")
if err != nil {
panic(err)
}
for i := range seed {
c := &seed[i]
if _, err := stmt.Exec(c.ID, c.Name, c.Coordinates); err != nil {
if _, err := stmt.Exec(c.ID, c.Name, c.Coordinates, c.Description); err != nil {
panic(err)
}
}
Expand Down
1 change: 1 addition & 0 deletions services/location/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Location struct {
ID int64 `json:"id"`
Name string `json:"name"`
Coordinates string `json:"coordinates"`
Description string `json:"description"` //New field
}

// Interface exposed by the Location service.
Expand Down