diff --git a/.gitignore b/.gitignore index c329722..ea63aaa 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,12 @@ # development environment .envrc .direnv +cmd/server/server # postgres postgres.db postgres.log .s.PGSQL.5432* +cmd/server/logfile + + diff --git a/README.md b/README.md index 1d771cd..051ef3f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,20 @@ The goal of this project is to be a crowd sourced resource to find out if a prod - moderation tooling? -Created database by +Troubleshooting: + +Error: +`2025/04/17 16:21:21 ERROR: relation "idx_user_claims_product_id" already exists (SQLSTATE 42P07)` +Explanation: The database tables already exist and the program was trying to create them again. + +Fix: Either don't create the databases again in the code, or run `dropdb veganDB` to allow it to recreate it. + +Error: +`psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: database "" does not exist` +Explanation: Postgres automatically tryes to connect to a database with the same name as the user. Specify user and database: +`psql -U username databaseName ` + + ## How to start database diff --git a/internal/api/routes.go b/internal/api/routes.go index 97eefc9..98f48e4 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "time" "vegan-barcode/internal/database" "vegan-barcode/internal/utils" @@ -46,6 +47,12 @@ func BindRoutes() { func (s *ApiService) runTest(c *gin.Context) { queryParam := c.Param("id") log.Debug("Test was successful.") + + err := s.db.Insert(c, database.ProductsTable, &database.Product{System: "upc", Barcode: "fubar", Created_at: time.Now()}) + if err != nil { + // TODO: Figure out correct status code. + c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to insert item to product table", "error": err.Error()}) + } c.JSON(http.StatusOK, gin.H{"message": "Hello World!", "query_param": queryParam}) } diff --git a/internal/database/database.go b/internal/database/database.go index 6bdf77d..112f888 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -10,11 +10,11 @@ import ( "github.com/vingarcia/ksql/adapters/kpgx" ) -var ProductsTable = ksql.NewTable("products", "product_id") +var ProductsTable = ksql.NewTable("products", "id") -var UserClaimsTable = ksql.NewTable("user_claims", "user_claim_id") +var UserClaimsTable = ksql.NewTable("user_claims", "id") -var AutomatedClaimsTable = ksql.NewTable("automated_claims", "automated_claim_id") +var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id") // initializeDatabase creates the database and calls createTables. func InitializeDatabase() *ksql.DB { @@ -36,9 +36,10 @@ func InitializeDatabase() *ksql.DB { // createTables adds the product, automated_claims, and user_claims tables to the initialized database. func createTables(ctx context.Context, db ksql.DB) { + _, err := db.Exec(ctx, ` CREATE TABLE IF NOT EXISTS products ( - id INTEGER PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, system TEXT, barcode TEXT, created_at TIMESTAMPTZ @@ -49,7 +50,7 @@ func createTables(ctx context.Context, db ksql.DB) { } _, err = db.Exec(ctx, ` CREATE TABLE IF NOT EXISTS user_claims ( - id INTEGER PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, product_id INTEGER, evidence_type INTEGER, evidence JSONB, @@ -61,14 +62,14 @@ func createTables(ctx context.Context, db ksql.DB) { CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id) ); - CREATE INDEX idx_user_claims_product_id ON user_claims USING HASH (product_id); + -- CREATE INDEX idx_user_claims_product_id ON user_claims USING HASH (product_id); `) if err != nil { log.Fatal(err) } _, err = db.Exec(ctx, ` CREATE TABLE IF NOT EXISTS automated_claims ( - id INTEGER PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, product_id INTEGER, worker_type INTEGER, evidence JSONB, @@ -79,7 +80,7 @@ func createTables(ctx context.Context, db ksql.DB) { CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id) ); - CREATE INDEX idx_automated_claims_product_id ON automated_claims USING HASH (product_id); + -- CREATE INDEX idx_automated_claims_product_id ON automated_claims USING HASH (product_id); `) if err != nil { log.Fatal(err) diff --git a/internal/database/models.go b/internal/database/models.go index 9a7ca59..3bbb2cb 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -5,10 +5,10 @@ import ( ) type Product struct { - id int `ksql:"id"` - system string `ksql:"system"` - barcode string `ksql:"barcode"` - created_at time.Time `ksql:"created_at,timeNowUTC"` + Id int `ksql:"id"` + System string `ksql:"system"` + Barcode string `ksql:"barcode"` + Created_at time.Time `ksql:"created_at,timeNowUTC"` } type WorkerType int