Maintenance & Setup Scripts
Relevant source files
The following files were used as context for generating this wiki page:
Maintenance and setup scripts in the ElmecV2 project are designed to automate the configuration, validation, and cleanup of the backend infrastructure, specifically targeting Supabase services. These scripts ensure that the database schema, authentication users, and storage buckets are correctly initialized and remain consistent across different environments (development, staging, and production).
The scope of these scripts includes connection verification, execution of SQL migrations, creation of demo users, and comprehensive CRUD testing for core modules such as requests, chat, and storage. They serve as a critical bridge between the codebase and the Supabase Backend-as-a-Service (BaaS) provider.
Sources: scripts/setup-supabase-complete.js:1-12, README.md:120-135
Backend Initialization and Configuration
The primary setup logic is encapsulated in a central script that orchestrates the entire backend initialization process. This flow is divided into distinct steps to ensure that prerequisites (like a successful connection) are met before proceeding to more complex operations like data migration or user creation.
Supabase Setup Workflow
The configuration process follows a sequential path:
- Connection Testing: Validates that environment variables (URL and Service Role Key) are correct and the database is reachable.
- Migration Execution: Attempts to run SQL scripts located in the migrations directory.
- Identity Management: Creates default administrative, agent, and customer accounts in Supabase Auth.
- Final Verification: Checks the existence of tables and lists active users to confirm a successful deployment.
Sources: scripts/setup-supabase-complete.js:84-230
The following diagram illustrates the automated setup sequence:
The flowchart shows the logic used to verify credentials before attempting destructive or creative operations on the database.
Sources: scripts/setup-supabase-complete.js:232-269
Demo User Configuration
The project defines a standard set of demo users with specific roles and metadata to facilitate immediate testing after setup.
| Role | Metadata Fields | |
|---|---|---|
i.pineda@elmec.com.mx | admin | nombre, apellido_paterno, rol |
j.gonzalez@elmec.com.mx | agent | nombre, apellido_paterno, rol |
cliente@gmail.com | customer | nombre, apellido_paterno, rol |
| Sources: scripts/setup-supabase-complete.js:46-77 |
Storage Infrastructure Management
Storage maintenance involves the creation and policy verification of the request-files bucket. This bucket is essential for the file upload functionality in the requests module.
Bucket Creation and RLS Policies
Scripts are provided to ensure the storage bucket exists and has the correct Row Level Security (RLS) policies. These policies govern how users interact with stored objects based on their authentication status.
Sources: RESUMEN_FINAL.md:75-95, scripts/create-storage-bucket.js
The sequence diagram details the interaction between setup scripts and Supabase Storage to establish a secure file environment.
Sources: RESUMEN_FINAL.md:80-100, RESUMEN_CONFIGURACION.md:15-35
Validation and Module Testing
Maintenance includes a suite of test scripts used to validate module integrity. These scripts perform automated CRUD (Create, Read, Update, Delete) operations and check for database check constraints.
Core Module Tests
Tests are executed across several modules to ensure backend reliability:
- Authentication: Verifies login/logout flows and session persistence.
- Requests: Validates the creation of tickets, state transitions (e.g., 'nuevo' to 'resuelto'), and field constraints.
- Chat: Ensures room creation and message delivery.
Sources: REPORTE-EJECUCION-PRUEBAS.md:15-110
Database Constraint Validation
During maintenance, scripts often identify and log violations of database check constraints. For example, the requests table enforces specific values for the estatus column.
| Table | Column | Valid Values |
|---|---|---|
requests | estatus | nuevo, asignado, en_proceso, pausado, resuelto |
chat_rooms | tipo | support, sales, general |
notifications | type | request_update, new_message, assignment, system |
| Sources: REPORTE-EJECUCION-PRUEBAS.md:120-165 |
Execution Environment
The scripts are primarily executed via Node.js and require specific environment variables to interact with the Supabase API using the Service Role Key for administrative bypass of RLS during setup.
// Example of administrative client initialization
const supabase = createClient(supabaseUrl, supabaseServiceKey, {
auth: {
autoRefreshToken: false,
persistSession: false
}
});
Sources: scripts/setup-supabase-complete.js:33-40
Conclusion
The maintenance and setup scripts of ElmecV2 provide a robust framework for managing backend state. By automating user creation, storage configuration, and module validation, these scripts reduce the risk of environment drift and ensure that the application core—spanning authentication, chat, and request management—remains functional and secure across deployments.
Sources: REPORTE_FINAL_VALIDACION.md:150-170, scripts/setup-supabase-complete.js:270-280