Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

0014_capability_flag_maps.sql

60 lines2.1 KB

1-- Custom SQL migration file, put your code below! --
2-- `models.capabilities` becomes a yes/no flag map on every row, or null.
3--
4-- 1 and 2: a provider's native listing object (fal, byteplus, elevenlabs,
5-- replicate, reactor) moves to `provider_metadata`. Such an object is told
6-- from a flag map by a value that is not a boolean, or by one of the native
7-- boolean keys those listings use. Every key moves but `asyncapi`, the one
8-- flag sync writes beside them; it stays behind as the row's flag map.
9--
10-- 3: a flag list becomes a map: `["tools","seed"]` is
11-- `{"tools":true,"seed":true}`. A list only ever said yes, so every key is
12-- true; the poller adds the stated noes on its next pass. An empty list said
13-- nothing a null does not, so it becomes null.
14--
15-- Each statement matches only the old shape, so a second run changes nothing.
16UPDATE `models`
17SET `provider_metadata` = (
18 SELECT json_group_object(`key`, json(`models`.`capabilities` -> `fullkey`))
19 FROM json_each(`models`.`capabilities`)
20 WHERE `key` <> 'asyncapi'
22WHERE json_type(`capabilities`) = 'object'
23 AND EXISTS (
24 SELECT 1 FROM json_each(`models`.`capabilities`)
25 WHERE `key` <> 'asyncapi'
26 AND (
27 `type` NOT IN ('true', 'false')
28 OR `key` IN ('canDoTextToSpeech', 'canDoVoiceConversion', 'official', 'structuredOutputProbed')
29 )
30 );
31--> statement-breakpoint
32UPDATE `models`
33SET `capabilities` = CASE
34 WHEN json_extract(`capabilities`, '$.asyncapi') = 1
35 THEN json_object('asyncapi', json('true'))
36 ELSE NULL
37END
38WHERE json_type(`capabilities`) = 'object'
39 AND EXISTS (
40 SELECT 1 FROM json_each(`models`.`capabilities`)
41 WHERE `key` <> 'asyncapi'
42 AND (
43 `type` NOT IN ('true', 'false')
44 OR `key` IN ('canDoTextToSpeech', 'canDoVoiceConversion', 'official', 'structuredOutputProbed')
45 )
46 );
47--> statement-breakpoint
48UPDATE `models`
49SET `capabilities` = (
50 SELECT CASE
51 WHEN count(*) = 0 THEN NULL
52 ELSE json_group_object(`flag`, json('true'))
53 END
54 FROM (
55 SELECT DISTINCT `value` AS `flag`
56 FROM json_each(`models`.`capabilities`)
57 WHERE `type` = 'text'
58 )
60WHERE json_type(`capabilities`) = 'array';