Add validations for npm vregs models

Context

The new three models for NPM virtual registries were added in Add Npm VRegs models for registries and upstreams (!213405 - merged)

What does this MR do and why?

Add validations for Registry, Upstream and RegistryUpstream npm virtual registries models.

These are the same validations that we currently have for Maven Virtual registry: Registry, Upstream and RegistryUpstream.

References

Create models for Registries and Upstreams (#581343)

Screenshots or screen recordings

No.

Database analysis

postgres.ai setup
-- Step 1: Insert the known registry with explicit ID (e.g., 1)
exec INSERT INTO virtual_registries_packages_npm_registries (id, group_id, name, created_at, updated_at)
VALUES (1, 9970, 'test-registry-9970', NOW(), NOW());

-- Step 2: Create upstreams for group 9970
exec INSERT INTO virtual_registries_packages_npm_upstreams (group_id, url, name, created_at, updated_at)
SELECT 
  9970,
  'https://registry.npmjs.org/9970/' || n,
  'upstream-9970-' || n,
  NOW(),
  NOW()
FROM generate_series(1, 3) AS n;

-- Step 3: Link them using the known registry_id = 1
exec INSERT INTO virtual_registries_packages_npm_registry_upstreams 
  (group_id, registry_id, upstream_id, position, created_at, updated_at)
SELECT 
  9970,
  1,  -- our known registry_id
  id,
  ROW_NUMBER() OVER (ORDER BY id),
  NOW(),
  NOW()
FROM virtual_registries_packages_npm_upstreams
WHERE group_id = 9970 AND name LIKE 'upstream-9970-%';

-- Step 4: Fix the sequence so future inserts don't conflict
exec SELECT setval('virtual_registries_packages_npm_registries_id_seq', 
  (SELECT MAX(id) FROM virtual_registries_packages_npm_registries));

-- Step 5: Add remaining 999 groups (same as before)
exec WITH selected_groups AS (
  SELECT id FROM namespaces 
  WHERE type = 'Group' AND id != 9970 
  ORDER BY random() 
  LIMIT 999
),
inserted_registries AS (
  INSERT INTO virtual_registries_packages_npm_registries (group_id, name, created_at, updated_at)
  SELECT id, 'test-registry-' || id, NOW(), NOW()
  FROM selected_groups
  RETURNING id, group_id
),
inserted_upstreams AS (
  INSERT INTO virtual_registries_packages_npm_upstreams (group_id, url, name, created_at, updated_at)
  SELECT 
    sg.id,
    'https://registry.npmjs.org/' || sg.id || '/' || n,
    'upstream-' || sg.id || '-' || n,
    NOW(),
    NOW()
  FROM selected_groups sg
  CROSS JOIN generate_series(1, 3) AS n
  RETURNING id, group_id
)
INSERT INTO virtual_registries_packages_npm_registry_upstreams 
  (group_id, registry_id, upstream_id, position, created_at, updated_at)
SELECT 
  ir.group_id,
  ir.id,
  iu.id,
  ROW_NUMBER() OVER (PARTITION BY ir.id ORDER BY iu.id),
  NOW(),
  NOW()
FROM inserted_registries ir
JOIN inserted_upstreams iu ON ir.group_id = iu.group_id;
VirtualRegistries::Packages::Npm::RegistryUpstream#set_position
SELECT MAX("virtual_registries_packages_npm_registry_upstreams"."position")
FROM "virtual_registries_packages_npm_registry_upstreams"
WHERE "virtual_registries_packages_npm_registry_upstreams"."registry_id" = XXX
  AND "virtual_registries_packages_npm_registry_upstreams"."group_id" = XXX;

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/46386/commands/141500

VirtualRegistries::Packages::Npm::Upstream#ensure_local_project_or_local_group
SELECT 1 AS one
FROM "projects"
WHERE "projects"."id" = 1
LIMIT 1;

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/46386/commands/141520

SELECT 1 AS one
FROM "namespaces"
WHERE "namespaces"."type" = 'Group'
  AND "namespaces"."id" = 8
LIMIT 1;

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/46386/commands/141517

VirtualRegistries::Packages::Npm::Upstream#credentials_uniqueness_for_group
SELECT "virtual_registries_packages_npm_upstreams"."username",
       "virtual_registries_packages_npm_upstreams"."password"
FROM "virtual_registries_packages_npm_upstreams"
WHERE "virtual_registries_packages_npm_upstreams"."group_id" = XXX
  AND "virtual_registries_packages_npm_upstreams"."url" = 'XXX';

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/46386/commands/141507

SELECT "virtual_registries_packages_npm_upstreams"."username",
       "virtual_registries_packages_npm_upstreams"."password"
FROM "virtual_registries_packages_npm_upstreams"
WHERE "virtual_registries_packages_npm_upstreams"."group_id" = XXX
  AND "virtual_registries_packages_npm_upstreams"."id" != XXX
  AND "virtual_registries_packages_npm_upstreams"."url" = 'XXX';

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/46386/commands/141514

How to set up and validate locally

I think having 🍏 tests should be sufficient. However, all validations can be tried manually.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #581343

Edited by Dzmitry (Dima) Meshcharakou

Merge request reports

Loading