From 0d1420ca9bea24b39b39d4acf72a55e350a52923 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 26 Nov 2024 18:24:07 -0600 Subject: [PATCH] Moving totals_meta meta_type to enum --- analytics/helpers.go | 2 +- migrations/0001_initial.down.sql | 1 + migrations/0001_initial.up.sql | 13 ++++++++++++- models/models.go | 2 +- models/schema.sql | 10 +++++++++- models/totals_meta.go | 8 ++++---- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/analytics/helpers.go b/analytics/helpers.go index 7d69457..9aff073 100644 --- a/analytics/helpers.go +++ b/analytics/helpers.go @@ -160,7 +160,7 @@ func AddMetaAnalytics(ctx context.Context, req *http.Request, dailyTotalID int, } // Helper function to create an TotalsMeta entry based on the metaType -func AddMeta(ctx context.Context, id, mType int, mVal string, isUnique bool) error { +func AddMeta(ctx context.Context, id int, mType string, mVal string, isUnique bool) error { meta := &models.TotalsMeta{ DailyTotalID: id, MetaType: mType, diff --git a/migrations/0001_initial.down.sql b/migrations/0001_initial.down.sql index 2c17ff0..8743faa 100644 --- a/migrations/0001_initial.down.sql +++ b/migrations/0001_initial.down.sql @@ -42,3 +42,4 @@ DROP TYPE IF EXISTS org_user_perm; DROP TYPE IF EXISTS org_type; DROP TYPE IF EXISTS subscription_type; DROP TYPE IF EXISTS qr_code_type; +DROP TYPE IF EXISTS totals_meta_type; diff --git a/migrations/0001_initial.up.sql b/migrations/0001_initial.up.sql index ffd99ba..02b9579 100644 --- a/migrations/0001_initial.up.sql +++ b/migrations/0001_initial.up.sql @@ -107,6 +107,17 @@ EXCEPTION WHEN duplicate_object THEN null; END $$; +DO $$ BEGIN +CREATE TYPE totals_meta_type AS ENUM ( + 'REFERRER', + 'COUNTRY', + 'CITY', + 'DEVICE' +); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; + CREATE TABLE users ( id SERIAL PRIMARY KEY, @@ -521,7 +532,7 @@ CREATE INDEX daily_totals_qr_id_idx ON daily_totals (qr_id); CREATE TABLE totals_meta ( id SERIAL PRIMARY KEY, daily_total_id INT REFERENCES daily_totals (id) ON DELETE CASCADE NOT NULL, - meta_type INT DEFAULT 0, + meta_type totals_meta_type DEFAULT 'REFERRER', meta_value VARCHAR(1024) DEFAULT '', clicks INT DEFAULT 0, unique_clicks INT DEFAULT 0, diff --git a/models/models.go b/models/models.go index 004a8ed..22a74c8 100644 --- a/models/models.go +++ b/models/models.go @@ -319,7 +319,7 @@ type DailyTotal struct { type TotalsMeta struct { ID int `db:"id"` DailyTotalID int `db:"daily_total_id"` - MetaType int `db:"meta_type"` + MetaType string `db:"meta_type"` MetaValue string `db:"meta_value"` Clicks int `db:"clicks"` UniqueClicks int `db:"unique_clicks"` diff --git a/models/schema.sql b/models/schema.sql index 569c1ac..730a514 100644 --- a/models/schema.sql +++ b/models/schema.sql @@ -66,6 +66,14 @@ CREATE TYPE qr_code_type AS ENUM ( 'SHORT' ); +CREATE TYPE totals_meta_type AS ENUM ( + 'REFERRER', + 'COUNTRY', + 'CITY', + 'DEVICE' +); + + CREATE TABLE users ( id SERIAL PRIMARY KEY, full_name VARCHAR ( 150 ) NOT NULL, @@ -479,7 +487,7 @@ CREATE INDEX daily_totals_qr_id_idx ON daily_totals (qr_id); CREATE TABLE totals_meta ( id SERIAL PRIMARY KEY, daily_total_id INT REFERENCES daily_totals (id) ON DELETE CASCADE NOT NULL, - meta_type INT DEFAULT 0, + meta_type totals_meta_type DEFAULT 'REFERRER', meta_value VARCHAR(1024) DEFAULT '', clicks INT DEFAULT 0, unique_clicks INT DEFAULT 0, diff --git a/models/totals_meta.go b/models/totals_meta.go index b30d5cf..bcf3ada 100644 --- a/models/totals_meta.go +++ b/models/totals_meta.go @@ -12,10 +12,10 @@ import ( ) const ( - MetaTypeReferrer = iota - MetaTypeCountry - MetaTypeCity - MetaTypeDevice + MetaTypeReferrer string = "REFERRER" + MetaTypeCountry string = "COUNTRY" + MetaTypeCity string = "CITY" + MetaTypeDevice string = "DEVICE" ) func GetTotalsMetas(ctx context.Context, opts *database.FilterOptions) ([]*TotalsMeta, error) { -- 2.45.2