diff --git a/api/chalicelib/core/telemetry.py b/api/chalicelib/core/telemetry.py index e12200809..8098c9cd7 100644 --- a/api/chalicelib/core/telemetry.py +++ b/api/chalicelib/core/telemetry.py @@ -8,7 +8,8 @@ def process_data(data): 'edition': license.EDITION, 'tracking': data["opt_out"], 'version': data["version_number"], - 'user_id': data["user_id"], + 'user_id': data["tenant_key"], + 'tenant_key': data["tenant_key"], 'owner_email': None if data["opt_out"] else data["email"], 'organization_name': None if data["opt_out"] else data["name"], 'users_count': data["t_users"], @@ -28,7 +29,7 @@ def compute(): t_projects=COALESCE((SELECT COUNT(*) FROM public.projects WHERE deleted_at ISNULL), 0), t_sessions=COALESCE((SELECT COUNT(*) FROM public.sessions), 0), t_users=COALESCE((SELECT COUNT(*) FROM public.users WHERE deleted_at ISNULL), 0) - RETURNING name,t_integrations,t_projects,t_sessions,t_users,user_id,opt_out, + RETURNING name,t_integrations,t_projects,t_sessions,t_users,tenant_key,opt_out, (SELECT openreplay_version()) AS version_number,(SELECT email FROM public.users WHERE role = 'owner' LIMIT 1);""" ) data = cur.fetchone() @@ -40,6 +41,7 @@ def new_client(): cur.execute( f"""SELECT *, (SELECT email FROM public.users WHERE role='owner' LIMIT 1) AS email - FROM public.tenants;""") + FROM public.tenants + LIMIT 1;""") data = cur.fetchone() requests.post('https://api.openreplay.com/os/signup', json=process_data(data)) diff --git a/ee/api/chalicelib/core/telemetry.py b/ee/api/chalicelib/core/telemetry.py index 51fd55787..a002f8501 100644 --- a/ee/api/chalicelib/core/telemetry.py +++ b/ee/api/chalicelib/core/telemetry.py @@ -8,7 +8,8 @@ def process_data(data): 'edition': license.EDITION, 'tracking': data["opt_out"], 'version': data["version_number"], - 'user_id': data["user_id"], + 'user_id': data["tenant_key"], + 'tenant_key': data["tenant_key"], 'owner_email': None if data["opt_out"] else data["email"], 'organization_name': None if data["opt_out"] else data["name"], 'users_count': data["t_users"], @@ -51,7 +52,7 @@ def compute(): FROM public.tenants ) AS all_tenants WHERE tenants.tenant_id = all_tenants.tenant_id - RETURNING name,t_integrations,t_projects,t_sessions,t_users,user_id,opt_out, + RETURNING name,t_integrations,t_projects,t_sessions,t_users,tenant_key,opt_out, (SELECT openreplay_version()) AS version_number, (SELECT email FROM public.users WHERE role = 'owner' AND users.tenant_id=tenants.tenant_id LIMIT 1);""" ) @@ -64,8 +65,9 @@ def new_client(tenant_id): with pg_client.PostgresClient() as cur: cur.execute( cur.mogrify(f"""SELECT *, - (SELECT email FROM public.users WHERE tenant_id=%(tenant_id)s) AS email + (SELECT email FROM public.users WHERE tenant_id=%(tenant_id)s AND role='owner' LIMIT 1) AS email FROM public.tenants - WHERE tenant_id=%(tenant_id)s;""", {"tenant_id": tenant_id})) + WHERE tenant_id=%(tenant_id)s + LIMIT 1;""", {"tenant_id": tenant_id})) data = cur.fetchone() requests.post('https://api.openreplay.com/os/signup', json=process_data(data)) diff --git a/ee/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql b/ee/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql index 1fb572626..6569ef682 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql @@ -36,6 +36,20 @@ ALTER TABLE tenants ALTER TABLE dashboards ALTER COLUMN user_id DROP NOT NULL; +DO +$$ + BEGIN + IF EXISTS(SELECT * + FROM information_schema.columns + WHERE table_name = 'tenants' + and column_name = 'user_id') + THEN + ALTER TABLE tenants + RENAME COLUMN user_id TO tenant_key; + END IF; + END +$$; + COMMIT; ALTER TYPE metric_type ADD VALUE IF NOT EXISTS 'funnel'; diff --git a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql index d044f1636..50cd912fa 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -142,7 +142,7 @@ $$ CREATE TABLE IF NOT EXISTS tenants ( tenant_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, - user_id text NOT NULL DEFAULT generate_api_key(20), + tenant_key text NOT NULL DEFAULT generate_api_key(20), name text NOT NULL, api_key text UNIQUE default generate_api_key(20) not null, created_at timestamp without time zone NOT NULL DEFAULT (now() at time zone 'utc'), diff --git a/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql b/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql index b0275a3a8..8f07edf0f 100644 --- a/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql +++ b/scripts/helm/db/init_dbs/postgresql/1.7.0/1.7.0.sql @@ -23,6 +23,20 @@ ALTER TABLE tenants ALTER TABLE dashboards ALTER COLUMN user_id DROP NOT NULL; +DO +$$ + BEGIN + IF EXISTS(SELECT * + FROM information_schema.columns + WHERE table_name = 'tenants' + and column_name = 'user_id') + THEN + ALTER TABLE tenants + RENAME COLUMN user_id TO tenant_key; + END IF; + END +$$; + COMMIT; ALTER TYPE metric_type ADD VALUE IF NOT EXISTS 'predefined'; diff --git a/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/scripts/helm/db/init_dbs/postgresql/init_schema.sql index f870b7824..b26483e9b 100644 --- a/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -117,7 +117,7 @@ $$ CREATE TABLE tenants ( tenant_id integer NOT NULL DEFAULT 1, - user_id text NOT NULL DEFAULT generate_api_key(20), + tenant_key text NOT NULL DEFAULT generate_api_key(20), name text NOT NULL, api_key text NOT NULL DEFAULT generate_api_key(20), created_at timestamp without time zone NOT NULL DEFAULT (now() at time zone 'utc'),