feat: tipos Migration/MigrationStatus y funciones puras migration_parse + migration_validate
Fase 1 del issue 0015. Tipos Go en functions/infra/migration.go con metadata en types/infra/. Funciones puras: MigrationParse (parsea filename NNN_name.sql + bloques -- +up/-- +down) y MigrationValidate (verifica secuencia, huecos, duplicados, bloques vacios). 16 tests, todos pasan. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
---
|
||||
name: migration_validate
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: pure
|
||||
signature: "func MigrationValidate(migrations []Migration) []string"
|
||||
description: "Verifica que una secuencia de migraciones sea valida: versiones secuenciales sin huecos comenzando en 1, sin duplicados, con up_sql y nombre no vacios. Retorna lista de errores (vacia si todo OK)."
|
||||
tags: [migration, database, sql, schema, sqlite, validate]
|
||||
uses_functions: []
|
||||
uses_types: [migration_go_infra]
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: ""
|
||||
imports: ["fmt", "sort"]
|
||||
params:
|
||||
- name: migrations
|
||||
desc: "slice de Migration a validar, puede estar desordenado"
|
||||
output: "slice de strings con mensajes de error; slice vacio si todas las migraciones son validas"
|
||||
tested: true
|
||||
tests:
|
||||
- "secuencia valida retorna sin errores"
|
||||
- "secuencia vacia retorna sin errores"
|
||||
- "version duplicada reporta error"
|
||||
- "hueco en versiones reporta version faltante"
|
||||
- "up_sql vacio reporta error"
|
||||
- "nombre vacio reporta error"
|
||||
- "versiones que no empiezan en 1 reportan error"
|
||||
- "multiple errores se reportan todos"
|
||||
test_file_path: "functions/infra/migration_validate_test.go"
|
||||
file_path: "functions/infra/migration_validate.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
migrations := []Migration{
|
||||
{Version: 1, Name: "create_users", UpSQL: "CREATE TABLE users (...);", DownSQL: "DROP TABLE users;"},
|
||||
{Version: 3, Name: "add_roles", UpSQL: "CREATE TABLE roles (...);", DownSQL: "DROP TABLE roles;"},
|
||||
}
|
||||
errs := MigrationValidate(migrations)
|
||||
// errs = ["gap in versions: missing 2 (have 1 then 3)"]
|
||||
|
||||
valid := []Migration{
|
||||
{Version: 1, Name: "create_users", UpSQL: "CREATE TABLE users (id TEXT PRIMARY KEY);"},
|
||||
{Version: 2, Name: "add_email", UpSQL: "ALTER TABLE users ADD COLUMN email TEXT;"},
|
||||
}
|
||||
errs = MigrationValidate(valid)
|
||||
// errs = [] (empty — no errors)
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion pura — no hace I/O, no modifica el slice de entrada. Ordena internamente una copia para detectar huecos. Todos los errores encontrados se acumulan y retornan juntos (no falla al primer error). Util antes de llamar a `MigrationUp` para detectar problemas de forma anticipada.
|
||||
Reference in New Issue
Block a user