Skip to main content

Developer Scripts

The Visita codebase includes a suite of utility scripts located in the /scripts directory. These scripts are essential for maintaining database integrity, seeding data, and verifying features without manual UI testing.
Execution: Most scripts are TypeScript and should be run using npx tsx scripts/script-name.ts. Bash scripts (.sh) should be run directly.

๐Ÿ” Database Integrity & Checks

These scripts verify the structural integrity of the database, check for missing data, or validate schema compliance.
ScriptPurpose
check-tables.jsVerifies the existence and basic record counts of core tables (Provinces, Municipalities, Wards).
check-civic-tables.tsValidates the structure and content of civic engagement tables.
check-columns-vds.tsChecks specific columns in Voting District tables.
check-constraint.tsVerifies database constraints are active and valid.
check-links.tsValidates foreign key relationships and link integrity.
check-mapping.tsVerifies logical mappings between different geographic entities.
check-myward-schema.tsspecific validation for the MyWard feature schema.
check-nulls.tsScans for unexpected NULL values in critical columns.
check-policies.tsAudits Row Level Security (RLS) policies.
check-vd-link.tsChecks links between Wards and Voting Districts.
cross-check.tsPerforms multi-table cross-reference validation.
final-check.tsA pre-deployment or pre-migration final sanity check.
validate-2026-ward-data.shBash script to validate MDB spatial data for the 2026 ward release.
codebase-audit.jsGeneral audit tool for the codebase structure.

Usage Example

# Check basic table health
node scripts/check-tables.js

# Audit RLS policies
npx tsx scripts/check-policies.ts

๐Ÿงช Feature Testing

Scripts designed to simulate user actions or test specific feature flows end-to-end.
ScriptPurpose
test-voting-system.tsComprehensive E2E test of the tender voting logic (Residency logic, Vote counting, Price deviation).
test-rag-pipeline.tsTests the RAG (Retrieval-Augmented Generation) pipeline for intelligence query accuracy.
test-db-query.tsGeneric database query performance and correctness test.
test-db-query-civic.tsSpecific queries for civic data structures.
test-anon-access.tsVerifies public/anonymous access permissions (RLS verification).
test-weather-cron.jsSimulates the weather logic cron job.

Usage Example

# Run the voting system test suite
npx tsx scripts/test-voting-system.ts

๐ŸŒฑ Migration & Seeding

Tools for populating the database with initial data or migrating between versions.
ScriptPurpose
seed-community-feed.tsPopulates the community feed with dummy/staging data.
seed-marketplace.tsSeeds the seller dashboard and marketplace with products/sellers.
seed-algolia.tsPushes database records to the Algolia search index.
sync-algolia.tsSynchronizes updates to Algolia.
run-civic-migration.tsOrchestrator for civic data migrations.
import-legacy-users.tsMigration script for legacy user data.
ingest-docs.tsIngestion pipeline for documentation into the knowledge base.
ingest-tenders.tsIngestion script for tender data.
migrate-vd-data.tsSpecialized migration for Voting District data.

๐Ÿ› ๏ธ Maintenance & Repair

Utilities for fixing data anomalies or managing administrative tasks.
ScriptPurpose
repair-wards-safe.tsSafely attempts to repair corrupted ward data.
repair-ward-data.jsLegacy repair script (JS).
admin_reset.jsDANGER: Resets admin state or data (Check script before running).
manage-moderators.tsTooling for adding/removing moderator privileges.
organize-supabase-types.tsHelper to reorganize generated Supabase type definitions.
search-schemas.tsInspects database schemas for specific patterns.
find-ward-by-code.tsUtility to lookup ward details by unique code.
find-user-row.tsLocates specific user records for debugging.

Best Practices

  1. Read Before Run: Always inspect the script source before running against production.
  2. Use tsx: Prefer npx tsx for TypeScript files to avoid compilation steps.
  3. Environment Variables: Ensure local .env files are configured, as most scripts utilize dotenv.
  4. Gold Standard: Use test-voting-system.ts as a template for writing new feature tests.