You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
1.1 KiB
22 lines
1.1 KiB
CREATE TABLE jukebox (
|
|
id text PRIMARY KEY,
|
|
usuario_id text NOT NULL REFERENCES usuario(id) ON DELETE CASCADE,
|
|
numero integer NOT NULL,
|
|
zona_horaria text NOT NULL,
|
|
franjas jsonb NOT NULL,
|
|
revision integer NOT NULL DEFAULT 1,
|
|
actualizada_en timestamptz NOT NULL DEFAULT now(),
|
|
CONSTRAINT jukebox_franjas_array CHECK (jsonb_typeof(franjas) = 'array'),
|
|
CONSTRAINT jukebox_revision_positiva CHECK (revision > 0)
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE UNIQUE INDEX jukebox_usuario_numero ON jukebox(usuario_id, numero);
|
|
--> statement-breakpoint
|
|
-- La programación antigua pasa a Jukebox 1; la tabla original se conserva.
|
|
INSERT INTO jukebox(id, usuario_id, numero, zona_horaria, franjas, revision, actualizada_en)
|
|
SELECT gen_random_uuid()::text, p.usuario_id, 1, p.zona_horaria,
|
|
COALESCE((SELECT jsonb_agg((f.valor - 'nombre') || jsonb_build_object('orden', 'secuencial', 'catalogo', false) ORDER BY f.posicion)
|
|
FROM jsonb_array_elements(p.franjas) WITH ORDINALITY AS f(valor, posicion)), '[]'::jsonb),
|
|
p.revision, p.actualizada_en
|
|
FROM programacion_jukebox p WHERE jsonb_array_length(p.franjas) > 0;
|