~netlandish/links

0d1420ca9bea24b39b39d4acf72a55e350a52923 — Peter Sanchez 30 days ago a2c0846
Moving totals_meta meta_type to enum
M analytics/helpers.go => analytics/helpers.go +1 -1
@@ 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,

M migrations/0001_initial.down.sql => migrations/0001_initial.down.sql +1 -0
@@ 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;

M migrations/0001_initial.up.sql => migrations/0001_initial.up.sql +12 -1
@@ 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,

M models/models.go => models/models.go +1 -1
@@ 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"`

M models/schema.sql => models/schema.sql +9 -1
@@ 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,

M models/totals_meta.go => models/totals_meta.go +4 -4
@@ 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) {