Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions db/core/migrations/V1__create_core_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,20 @@ CREATE TABLE certification (

-- auth_id referencia auth_user.id no banco do api-auth (outro
-- microsservico/banco) -- sem FK real aqui de proposito, so o valor solto.
-- username = handle publico do usuario (ex.: "@joaosilva"), sempre minusculo
-- e distinto do nome legal armazenado em person.name.
CREATE TABLE users (
id UUID NOT NULL DEFAULT gen_random_uuid(),
auth_id UUID NOT NULL,
avatar VARCHAR(255),
active BOOLEAN NOT NULL DEFAULT true,
id UUID NOT NULL DEFAULT gen_random_uuid(),
auth_id UUID NOT NULL,
username VARCHAR(30) NOT NULL,
avatar VARCHAR(255),
banner VARCHAR(255),
active BOOLEAN NOT NULL DEFAULT true,

CONSTRAINT pk_users PRIMARY KEY (id),
CONSTRAINT uq_users_auth_id UNIQUE (auth_id)
CONSTRAINT uq_users_auth_id UNIQUE (auth_id),
CONSTRAINT uq_users_username UNIQUE (username),
CONSTRAINT ck_users_username_format CHECK (username ~ '^[a-z0-9_]{3,30}$')
);

-- -----------------------------------------------------------------------------
Expand Down
16 changes: 11 additions & 5 deletions db/core/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,20 @@ CREATE TABLE certification (

-- auth_id referencia auth_user.id no banco do api-auth (outro
-- microsservico/banco) -- sem FK real aqui de proposito, so o valor solto.
-- username = handle publico do usuario (ex.: "@joaosilva"), sempre minusculo
-- e distinto do nome legal armazenado em person.name.
CREATE TABLE users (
id UUID NOT NULL DEFAULT gen_random_uuid(),
auth_id UUID NOT NULL,
avatar VARCHAR(255),
active BOOLEAN NOT NULL DEFAULT true,
id UUID NOT NULL DEFAULT gen_random_uuid(),
auth_id UUID NOT NULL,
username VARCHAR(30) NOT NULL,
avatar VARCHAR(255),
banner VARCHAR(255),
active BOOLEAN NOT NULL DEFAULT true,

CONSTRAINT pk_users PRIMARY KEY (id),
CONSTRAINT uq_users_auth_id UNIQUE (auth_id)
CONSTRAINT uq_users_auth_id UNIQUE (auth_id),
CONSTRAINT uq_users_username UNIQUE (username),
CONSTRAINT ck_users_username_format CHECK (username ~ '^[a-z0-9_]{3,30}$')
);

-- -----------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions db/core/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
-- =============================================================================

-- Usuarios de plataforma, ligados aos auth_user do banco do api-auth.
INSERT INTO users (id, auth_id, active)
VALUES ('33333333-3333-3333-3333-333333333333', '11111111-1111-1111-1111-111111111111', true);
INSERT INTO users (id, auth_id, username, active)
VALUES ('33333333-3333-3333-3333-333333333333', '11111111-1111-1111-1111-111111111111', 'requester_demo', true);

INSERT INTO users (id, auth_id, active)
VALUES ('44444444-4444-4444-4444-444444444444', '22222222-2222-2222-2222-222222222222', true);
INSERT INTO users (id, auth_id, username, active)
VALUES ('44444444-4444-4444-4444-444444444444', '22222222-2222-2222-2222-222222222222', 'supplier_demo', true);

-- Empresa fornecedora.
INSERT INTO address (id, state, city, zip_code, street, number)
Expand Down
28 changes: 17 additions & 11 deletions scripts/dataload.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv, json, random, string, sys, uuid
import csv, json, random, re, string, sys, uuid

from faker import Faker
from datetime import timedelta
Expand All @@ -22,6 +22,14 @@
def maybe(seq, p=0.7): return pick(seq) if random.random() < p else None


def unique_username() -> str:
"""Handle publico unico (@user), sempre minusculo -- combina com
ck_users_username_format em db/core/schema.sql."""
raw = re.sub(r"[^a-z0-9_]", "_", FAKE.unique.user_name().lower())
raw = trunc(raw, 30)
return raw if len(raw) >= 3 else raw.ljust(3, "0")


def media_pool(name: str) -> list[tuple[str, str]]:
"""Le (e cacheia) as linhas fixas de scripts/data/cloudinary/<name>.csv como (url, public_id)."""
if name not in _media_pools:
Expand Down Expand Up @@ -295,9 +303,14 @@
rows = []
for auth_id in self.ids["auth_user"]:
row_id = new_id()
rows.append((row_id, auth_id, maybe([FAKE.image_url()], p=0.5), random.random() < 0.95))
rows.append((
row_id, auth_id, unique_username(),
maybe([pick(media_pool("profile"))[0]], p=0.5),
maybe([pick(media_pool("hero"))[0]], p=0.3),
random.random() < 0.95,

Check warning on line 310 in scripts/dataload.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure that using this pseudorandom number generator is safe here.

See more on https://sonarcloud.io/project/issues?id=Solierrr_database-console&issues=AaCQpstFhDbl_24INeDK&open=AaCQpstFhDbl_24INeDK&pullRequest=12
))
self.ids.setdefault("users", []).extend(r[0] for r in rows)
self.insert("users", ["id", "fk_auth_user", "avatar", "active"], rows)
self.insert("users", ["id", "auth_id", "username", "avatar", "banner", "active"], rows)

def seed_person(self):
rows = []
Expand Down Expand Up @@ -342,13 +355,6 @@
rows.append((new_id(), position_id, permission_id))
self.insert("position_permission", ["id", "fk_position", "fk_permission"], rows)

def seed_user_photo(self):
profile_ids = self.seed_media_assets(self.n(20), "profile")
banner_ids = self.seed_media_assets(self.n(20), "hero")
rows = [(mid, pick(self.ids["users"]), "PROFILE") for mid in profile_ids]
rows += [(mid, pick(self.ids["users"]), "BANNER") for mid in banner_ids]
self.insert("user_photo", ["id", "fk_user", "type"], rows)

def seed_business_contact(self):
rows = []
for _ in range(self.n(80)):
Expand Down Expand Up @@ -810,7 +816,7 @@
seeder.seed_refresh_token, seeder.seed_totp_factor, seeder.seed_security_event,
seeder.seed_outbox_event,
seeder.seed_position, seeder.seed_permission,
seeder.seed_position_permission, seeder.seed_user_photo,
seeder.seed_position_permission,
seeder.seed_business_contact, seeder.seed_company, seeder.seed_company_photo,
seeder.seed_company_plans, seeder.seed_company_positions, seeder.seed_user_company,
seeder.seed_supplier, seeder.seed_subscription, seeder.seed_charge,
Expand Down