tb/main #1
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -2,8 +2,12 @@
|
|||
# development environment
|
||||
.envrc
|
||||
.direnv
|
||||
cmd/server/server
|
||||
|
||||
# postgres
|
||||
postgres.db
|
||||
postgres.log
|
||||
.s.PGSQL.5432*
|
||||
cmd/server/logfile
|
||||
|
||||
|
||||
|
|
15
README.md
15
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 "<user>" 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
|
||||
|
|
|
@ -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})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue