fix: ampliar CHECK constraint para soportar tipo ventas
Las tablas queries y envios tenían CHECK constraints que solo permitían terceros/transaccion. Se agrega ventas al constraint vía migración automática en _migrate para bases de datos existentes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
6b84ecfd90
commit
6fdca61636
+51
-2
@@ -53,6 +53,55 @@ def _migrate(conn):
|
||||
conn.execute("ALTER TABLE contratos ADD COLUMN excluir INTEGER NOT NULL DEFAULT 0")
|
||||
if "sin_contrato" not in contrato_cols:
|
||||
conn.execute("ALTER TABLE contratos ADD COLUMN sin_contrato INTEGER NOT NULL DEFAULT 0")
|
||||
# Ampliar CHECK constraint de queries para incluir 'ventas'
|
||||
q_sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='queries'").fetchone()
|
||||
if q_sql and "'ventas'" not in q_sql[0]:
|
||||
conn.execute("PRAGMA foreign_keys = OFF")
|
||||
conn.execute("""CREATE TABLE queries_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
query_type TEXT NOT NULL CHECK(query_type IN ('terceros','transaccion','ventas')),
|
||||
query_text TEXT NOT NULL,
|
||||
description TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
)""")
|
||||
conn.execute("INSERT INTO queries_new SELECT * FROM queries")
|
||||
conn.execute("DROP TABLE queries")
|
||||
conn.execute("ALTER TABLE queries_new RENAME TO queries")
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
conn.commit()
|
||||
|
||||
# Ampliar CHECK constraint de envios para incluir 'ventas'
|
||||
e_sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='envios'").fetchone()
|
||||
if e_sql and "'ventas'" not in e_sql[0]:
|
||||
conn.execute("PRAGMA foreign_keys = OFF")
|
||||
conn.execute("""CREATE TABLE envios_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
tipo TEXT NOT NULL CHECK(tipo IN ('terceros','transaccion','ventas')),
|
||||
factura TEXT,
|
||||
fecha_inicio TEXT,
|
||||
fecha_fin TEXT,
|
||||
pacientes_count INTEGER DEFAULT 0,
|
||||
servicios_count INTEGER DEFAULT 0,
|
||||
status TEXT NOT NULL DEFAULT 'pendiente',
|
||||
json_enviado TEXT,
|
||||
respuesta_api TEXT,
|
||||
codigo_cuv TEXT,
|
||||
idrecepcion INTEGER,
|
||||
contrato TEXT,
|
||||
mensaje_tns TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
)""")
|
||||
conn.execute("INSERT INTO envios_new SELECT id,user_id,tipo,factura,fecha_inicio,fecha_fin,"
|
||||
"pacientes_count,servicios_count,status,json_enviado,respuesta_api,codigo_cuv,"
|
||||
"idrecepcion,contrato,mensaje_tns,created_at FROM envios")
|
||||
conn.execute("DROP TABLE envios")
|
||||
conn.execute("ALTER TABLE envios_new RENAME TO envios")
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
conn.commit()
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
@@ -76,7 +125,7 @@ def init_db():
|
||||
CREATE TABLE IF NOT EXISTS queries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
query_type TEXT NOT NULL CHECK(query_type IN ('terceros','transaccion')),
|
||||
query_type TEXT NOT NULL CHECK(query_type IN ('terceros','transaccion','ventas')),
|
||||
query_text TEXT NOT NULL,
|
||||
description TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
@@ -95,7 +144,7 @@ def init_db():
|
||||
CREATE TABLE IF NOT EXISTS envios (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
tipo TEXT NOT NULL CHECK(tipo IN ('terceros','transaccion')),
|
||||
tipo TEXT NOT NULL CHECK(tipo IN ('terceros','transaccion','ventas')),
|
||||
factura TEXT,
|
||||
fecha_inicio TEXT,
|
||||
fecha_fin TEXT,
|
||||
|
||||
Reference in New Issue
Block a user