From 9b31d11d2792cc9c02c2130f8b0874858604d5f7 Mon Sep 17 00:00:00 2001 From: Emminex Date: Thu, 7 Nov 2024 22:08:36 +0100 Subject: [PATCH 1/2] Updated the database schema --- Dockerfile | 1 - services/location/database.go | 31 +++++++++++++++++++------------ services/location/interface.go | 1 + 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5c1327b..eb3dbab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,3 @@ ARG TARGETPLATFORM COPY dist/$TARGETPLATFORM/bin/hotrod /app/hotrod ENTRYPOINT ["/app/hotrod"] - diff --git a/services/location/database.go b/services/location/database.go index d19a4ba..7a1476a 100644 --- a/services/location/database.go +++ b/services/location/database.go @@ -47,6 +47,7 @@ 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) @@ -58,26 +59,32 @@ 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", }, } @@ -136,11 +143,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) { @@ -155,7 +162,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) @@ -168,13 +175,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 } @@ -187,13 +194,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 } @@ -216,7 +223,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() @@ -230,7 +237,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()) { @@ -241,7 +248,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 diff --git a/services/location/interface.go b/services/location/interface.go index 5a218b8..c03bb67 100644 --- a/services/location/interface.go +++ b/services/location/interface.go @@ -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. From 1506ddc441af9baed9a215a14bbd9c9a7aeab0fa Mon Sep 17 00:00:00 2001 From: Emminex Date: Fri, 15 Nov 2024 11:09:49 +0100 Subject: [PATCH 2/2] Update the database schema --- services/location/database.go | 32 ++++++++++++++++++++++---------- services/location/interface.go | 2 +- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/services/location/database.go b/services/location/database.go index 7a1476a..034b229 100644 --- a/services/location/database.go +++ b/services/location/database.go @@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS locations 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; ` @@ -59,32 +59,31 @@ var seed = []Location{ ID: 1, Name: "My Home", Coordinates: "231,773", - Description: "Cozy two-bedroom apartment", // New field + Description: "Cozy two-bedroom apartment", // New field }, { ID: 123, Name: "Rachel's Floral Designs", Coordinates: "115,277", - Description: "Flower shop specializing in arrangements", + Description: "Flower shop specializing in arrangements", }, { ID: 567, Name: "Amazing Coffee Roasters", Coordinates: "211,653", - Description: "Local coffee roaster with a variety of blends", - + Description: "Local coffee roaster with a variety of blends", }, { ID: 392, Name: "Trom Chocolatier", Coordinates: "577,322", - Description: "Artisan chocolate and confectionery", + Description: "Artisan chocolate and confectionery", }, { ID: 731, Name: "Japanese Desserts", Coordinates: "728,326", - Description: "Traditional Japanese sweets and pastries", + Description: "Traditional Japanese sweets and pastries", }, } @@ -278,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) } } diff --git a/services/location/interface.go b/services/location/interface.go index c03bb67..d5959f9 100644 --- a/services/location/interface.go +++ b/services/location/interface.go @@ -24,7 +24,7 @@ type Location struct { ID int64 `json:"id"` Name string `json:"name"` Coordinates string `json:"coordinates"` - Description string `json:"description"` //New field + Description string `json:"description"` //New field } // Interface exposed by the Location service.