Rearrange folders, and separate functionality
This commit is contained in:
parent
dc58253ad9
commit
d26e7b8345
5 changed files with 76 additions and 59 deletions
87
internal/database/database.go
Normal file
87
internal/database/database.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/vingarcia/ksql"
|
||||
"github.com/vingarcia/ksql/adapters/kpgx"
|
||||
)
|
||||
|
||||
var ProductsTable = ksql.NewTable("products", "product_id")
|
||||
|
||||
var UserClaimsTable = ksql.NewTable("user_claims", "user_claim_id")
|
||||
|
||||
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "automated_claim_id")
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
||||
host := os.Getenv("PGHOST")
|
||||
database := os.Getenv("DB_NAME")
|
||||
|
||||
connectString :=
|
||||
fmt.Sprintf("postgres:///?host=%s&database=%s", host, database)
|
||||
|
||||
db, err := kpgx.New(ctx, connectString, ksql.Config{})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
}
|
||||
|
||||
func createTables(db) {
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY,
|
||||
system TEXT,
|
||||
barcode TEXT,
|
||||
created_at TIMESTAMPTZ
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS user_claims (
|
||||
id INTEGER PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
evidence_type INTEGER,
|
||||
evidence JSONB,
|
||||
claim INTEGER,
|
||||
counter_claim INTEGER,
|
||||
created_at TIMESTAMPTZ,
|
||||
created_by TEXT,
|
||||
|
||||
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);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS automated_claims (
|
||||
id INTEGER PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
worker_type INTEGER,
|
||||
evidence JSONB,
|
||||
claim INTEGER,
|
||||
counter_claim INTEGER,
|
||||
created_at TIMESTAMPTZ,
|
||||
|
||||
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);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue