-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
2278 lines (2021 loc) · 64.2 KB
/
Copy pathqueries.sql
File metadata and controls
2278 lines (2021 loc) · 64.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- name: GetStoreById :one
SELECT * FROM stores WHERE id = ? LIMIT 1;
-- name: GetEnabledStoreById :one
SELECT * FROM stores WHERE id = ? AND enabled = TRUE LIMIT 1;
-- name: ListEnabledStores :many
SELECT * FROM stores WHERE enabled = TRUE ORDER BY id;
-- name: ListAllStores :many
SELECT * FROM stores ORDER BY id;
-- name: ListEnabledStoresForCompletion :many
SELECT id, display_name FROM stores WHERE enabled = TRUE ORDER BY id;
-- name: ListAllGameInstalls :many
SELECT * FROM game_installs
ORDER BY store_id, display_name, store_game_id, instance_id;
-- name: ListGameInstallsByStore :many
SELECT * FROM game_installs WHERE store_id = ?
ORDER BY display_name, store_game_id, instance_id;
-- name: MarkStoreInstallsNotPresent :exec
UPDATE game_installs
SET
is_present = FALSE,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE store_id = ?;
-- name: UpsertGameInstall :one
INSERT INTO game_installs (
store_id,
store_game_id,
instance_id,
canonical_game_id,
display_name,
install_root,
metadata,
last_seen_at,
is_present,
created_at,
updated_at
)
VALUES (
?, -- store_id
?, -- store_game_id
?, -- instance_id
?, -- canonical_game_id (nullable)
?, -- display_name
?, -- install_root (canonical)
?, -- metadata (json text, nullable)
?, -- last_seen_at (iso8601z, nullable)
?, -- is_present
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
ON CONFLICT (store_id, store_game_id, instance_id) DO UPDATE SET
canonical_game_id = excluded.canonical_game_id,
display_name = excluded.display_name,
install_root = excluded.install_root,
metadata = excluded.metadata,
last_seen_at = excluded.last_seen_at,
is_present = TRUE,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
RETURNING id;
-- name: GetTargetByName :one
SELECT * FROM targets WHERE game_install_id = ? AND name = ? LIMIT 1;
-- name: UpsertDiscoveredTarget :one
-- if a user_override exists for the same (game_install_id, name) the conflict
-- is silently ignored and the user's row is left untouched
INSERT INTO targets (
game_install_id, name, root_path, origin,
created_at,
updated_at
)
VALUES (
?, ?, ?, 'discovered',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
ON CONFLICT (game_install_id, name) DO UPDATE SET
root_path = excluded.root_path,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE targets.origin = 'discovered'
RETURNING *;
-- name: EnsureDefaultProfile :exec
INSERT INTO profiles (
game_install_id,
name,
description,
is_active,
created_at,
updated_at
)
SELECT
?1,
'default',
NULL,
TRUE,
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE NOT EXISTS (
SELECT 1 FROM profiles WHERE game_install_id = ?1
);
-- name: GetGameInstallByID :one
SELECT * FROM game_installs WHERE id = ? LIMIT 1;
-- name: GetGameInstallBySelector :one
SELECT * FROM game_installs
WHERE store_id = ? AND store_game_id = ? AND instance_id = ? LIMIT 1;
-- name: ListGameInstallsByStoreGameID :many
SELECT * FROM game_installs
WHERE store_id = ? AND store_game_id = ?
ORDER BY instance_id;
-- name: CompleteGameInstallsByPrefix :many
SELECT
id,
store_id,
store_game_id,
instance_id,
display_name,
is_present
FROM game_installs
WHERE
(lower(store_id || ':' || store_game_id || '#' || instance_id) LIKE lower(sqlc.arg(prefix)) ESCAPE '\')
OR (lower(display_name) LIKE lower(sqlc.arg(prefix)) ESCAPE '\')
ORDER BY
is_present DESC,
display_name,
store_id,
store_game_id,
instance_id
LIMIT 10;
-- name: ListTargetsForGameInstall :many
SELECT * FROM targets WHERE game_install_id = ? ORDER BY name;
-- name: GetProfilesForGameInstall :many
SELECT * FROM profiles WHERE game_install_id = ? ORDER BY name;
-- name: GetBlob :one
SELECT * FROM blobs WHERE sha256 = ? LIMIT 1;
-- name: InsertBlob :exec
INSERT INTO blobs (sha256, kind, size_bytes, original_name, verified_at)
VALUES (?, ?, ?, ?, ?);
-- name: ListBlobsByKind :many
SELECT * FROM blobs WHERE kind = ? ORDER BY created_at;
-- name: TouchBlobVerifiedAt :exec
UPDATE blobs
SET verified_at = ?
WHERE sha256 = ?;
-- name: CreateModPage :one
INSERT INTO mod_pages (
game_install_id, name, source_kind, source_url, source_ref,
nexus_game_domain, nexus_mod_id,
notes, metadata
) VALUES (
?, ?, ?, ?, ?,
?, ?,
?, ?
)
RETURNING id;
-- name: CreateModFile :one
INSERT INTO mod_files (
mod_page_id, label, is_primary, source_url, metadata
) VALUES (
?, ?, ?, ?, ?
)
RETURNING id;
-- name: CreateModFileVersion :one
INSERT INTO mod_file_versions (
mod_file_id, archive_sha256, original_name, version_string,
nexus_file_id, uploaded_at, upstream_notes, notes, metadata
) VALUES (
?, ?, ?, ?,
?, ?, ?, ?, ?
)
RETURNING id;
-- name: ListModsByGameInstall :many
WITH
per_page_counts AS (
SELECT
mp.id AS mod_page_id,
COUNT(DISTINCT mf.id) AS files_count,
COUNT(mfv.id) AS versions_count
FROM mod_pages mp
LEFT JOIN mod_files mf
ON mf.mod_page_id = mp.id
LEFT JOIN mod_file_versions mfv
ON mfv.mod_file_id = mf.id
WHERE mp.game_install_id = ?1
GROUP BY mp.id
),
joined AS (
SELECT
mp.id AS mod_page_id,
mp.name AS mod_name,
mp.source_kind,
mp.nexus_game_domain,
mp.nexus_mod_id,
mf.id AS mod_file_id,
mf.label AS mod_file_label,
mfv.id AS mod_file_version_id,
mfv.version_string,
mfv.nexus_file_id,
mfv.archive_sha256,
mfv.created_at AS imported_at,
COALESCE(ppc.files_count, 0) AS files_count,
COALESCE(ppc.versions_count, 0) AS versions_count,
ROW_NUMBER() OVER (
PARTITION BY mp.id
ORDER BY
(mfv.created_at IS NULL) ASC, -- prefer non-NULL versions
mfv.created_at DESC,
mfv.id DESC
) AS rn
FROM mod_pages mp
LEFT JOIN mod_files mf
ON mf.mod_page_id = mp.id
LEFT JOIN mod_file_versions mfv
ON mfv.mod_file_id = mf.id
LEFT JOIN per_page_counts ppc
ON ppc.mod_page_id = mp.id
WHERE mp.game_install_id = ?1
)
SELECT
mod_page_id,
mod_name,
source_kind,
nexus_game_domain,
nexus_mod_id,
files_count,
versions_count,
mod_file_id,
mod_file_label,
mod_file_version_id,
version_string,
archive_sha256,
imported_at
FROM joined
WHERE rn = 1
ORDER BY mod_name COLLATE NOCASE, mod_page_id;
-- name: ListModFilesByPage :many
SELECT id, mod_page_id, label, is_primary, source_url, created_at, updated_at
FROM mod_files
WHERE mod_page_id = ?
ORDER BY is_primary DESC, label COLLATE NOCASE, id;
-- name: ListModFileVersionsByFile :many
SELECT id, mod_file_id, archive_sha256, original_name, version_string, nexus_file_id, created_at
FROM mod_file_versions
WHERE mod_file_id = ?
ORDER BY created_at DESC, id DESC;
-- name: GetModPageForGame :one
SELECT id, game_install_id, name, source_kind, nexus_game_domain, nexus_mod_id
FROM mod_pages
WHERE id = ? AND game_install_id = ?;
-- name: GetModPageByNexus :one
SELECT id, game_install_id, name, source_kind, nexus_game_domain, nexus_mod_id
FROM mod_pages
WHERE game_install_id = ?
AND source_kind = 'nexus'
AND nexus_game_domain = ?
AND nexus_mod_id = ?;
-- name: GetModFileByLabel :one
SELECT id, mod_page_id, label, is_primary
FROM mod_files
WHERE mod_page_id = ? AND label = ?;
-- name: CountModFilesForPage :one
SELECT COUNT(1)
FROM mod_files
WHERE mod_page_id = ?;
-- name: CreateProfile :one
INSERT INTO profiles (game_install_id, name, description, is_active)
VALUES (?, ?, ?, ?)
RETURNING id;
-- name: GetProfileByName :one
SELECT * FROM profiles WHERE game_install_id = ? AND name = ? LIMIT 1;
-- name: ListProfilesByGameInstall :many
SELECT id, name, description, is_active, created_at, updated_at
FROM profiles
WHERE game_install_id = ?
ORDER BY is_active DESC, name COLLATE NOCASE, id;
-- name: RenameProfile :exec
UPDATE profiles
SET name = ?, updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE id = ?;
-- name: DeactivateProfilesForGame :exec
UPDATE profiles
SET is_active = FALSE,
updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE game_install_id = ? AND is_active = TRUE;
-- name: ActivateProfileByName :exec
UPDATE profiles
SET is_active = TRUE,
updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE game_install_id = ? AND name = ?;
-- name: ListProfilesForCompletion :many
SELECT name, is_active
FROM profiles
WHERE game_install_id = ?
ORDER BY is_active DESC, name COLLATE NOCASE;
-- name: IsPriorityTaken :one
SELECT TRUE
FROM profile_items
WHERE profile_id = ? AND priority = ?;
-- name: GetActiveProfileForGame :one
SELECT * FROM profiles WHERE game_install_id = ? AND is_active = TRUE LIMIT 1;
-- name: GetMaxPriorityForProfile :one
SELECT CAST(COALESCE(MAX(priority), 0) AS INTEGER) AS max_priority
FROM profile_items
WHERE profile_id = ?;
-- name: CreateProfileItem :one
INSERT INTO profile_items (
profile_id,
policy,
mod_file_version_id,
target_id,
enabled,
priority,
remap_config_id,
notes
) VALUES (?, 'pinned', ?, ?, ?, ?, NULL, NULL)
RETURNING id;
-- name: ExistsModFileVersion :one
SELECT 1
FROM mod_file_versions
WHERE id = ? LIMIT 1;
-- name: GetProfileItemByVersion :one
SELECT id, enabled
FROM profile_items
WHERE profile_id = ? AND mod_file_version_id = ? LIMIT 1;
-- name: SetProfileItemEnabled :exec
UPDATE profile_items
SET enabled = ?,
updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE id = ?;
-- name: GetAppliedProfileIDForGame :one
SELECT applied_profile_id
FROM game_installs
WHERE id = ? LIMIT 1;
-- name: DeleteProfileByID :exec
DELETE FROM profiles
WHERE id = ?;
-- name: GetProfileItemIDByVersion :one
SELECT id
FROM profile_items
WHERE profile_id = ? AND mod_file_version_id = ?;
-- name: DeleteProfileItemByID :exec
DELETE FROM profile_items
WHERE id = ?;
-- name: GetProfileItemByVersionForOrder :one
SELECT id, priority
FROM profile_items
WHERE profile_id = ? AND mod_file_version_id = ?;
-- name: SetProfileItemPriorityByID :exec
UPDATE profile_items
SET priority = ?,
updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE id = ?;
-- name: ListProfileItemsForOrder :many
SELECT id, mod_file_version_id, priority
FROM profile_items
WHERE profile_id = ?
ORDER BY priority ASC;
-- name: BumpPrioritiesForProfile :exec
UPDATE profile_items
SET priority = priority + sqlc.arg(offset),
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE profile_id = sqlc.arg(profile_id);
-- name: ListUnscannedArchives :many
SELECT
b.sha256,
b.original_name,
b.size_bytes
FROM blobs b
WHERE b.kind = 'archive'
AND NOT EXISTS (
SELECT TRUE
FROM archive_inventory_entries aie
WHERE aie.archive_sha256 = b.sha256
);
-- name: InsertArchiveInventoryEntry :exec
INSERT INTO archive_inventory_entries (
archive_sha256,
raw_path,
entry_type,
size_bytes,
link_target,
content_sha256,
position,
parse_error
) VALUES (?, ?, ?, ?, ?, ?, ?, ?);
-- name: MarkArchiveInventoryScanned :exec
UPDATE mod_file_versions
SET inventory_scanned_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE archive_sha256 = ?
AND inventory_scanned_at IS NULL;
-- name: IsArchiveInventoried :one
SELECT EXISTS (
SELECT TRUE
FROM archive_inventory_entries
WHERE archive_sha256 = ?
) AS inventoried;
-- name: GetProfileStatusItems :many
SELECT
pi.id AS item_id,
pi.priority,
pi.enabled,
pi.target_id,
pi.notes AS item_notes,
t.name AS target_name,
mp.id AS mod_page_id,
mp.name AS mod_page_name,
mp.source_kind,
mp.nexus_game_domain,
mp.nexus_mod_id,
mf.id AS mod_file_id,
mf.label AS file_label,
mfv.id AS mod_file_version_id,
mfv.version_string,
mfv.nexus_file_id,
mfv.archive_sha256,
mfv.inventory_scanned_at,
b.size_bytes,
CAST(COALESCE((
SELECT COUNT(*)
FROM remap_rules rr
WHERE rr.remap_config_id = pi.remap_config_id
), 0) AS INTEGER) AS remap_rule_count
FROM profile_items pi
JOIN targets t ON t.id = pi.target_id
JOIN mod_file_versions mfv ON mfv.id = pi.mod_file_version_id
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
JOIN blobs b ON b.sha256 = mfv.archive_sha256
WHERE pi.profile_id = ?
ORDER BY pi.priority ASC;
-- name: GetGameInstallAppliedState :one
SELECT
applied_profile_id,
applied_at,
applied_operation_id
FROM game_installs
WHERE id = ?;
-- name: GetIncompatibleModPairsForProfile :many
SELECT
mi.id,
mi.reason,
mpa.name AS mod_page_name_a,
mpb.name AS mod_page_name_b
FROM mod_incompatibilities mi
JOIN mod_pages mpa ON mpa.id = mi.mod_page_id_a
JOIN mod_pages mpb ON mpb.id = mi.mod_page_id_b
WHERE mpa.game_install_id = (SELECT game_install_id FROM profiles p WHERE p.id = sqlc.arg(profile_id))
AND mi.mod_page_id_a IN (
SELECT mp.id
FROM profile_items pi
JOIN mod_file_versions mfv ON mfv.id = pi.mod_file_version_id
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE pi.profile_id = sqlc.arg(profile_id)
AND pi.enabled = TRUE
)
AND mi.mod_page_id_b IN (
SELECT mp.id
FROM profile_items pi
JOIN mod_file_versions mfv ON mfv.id = pi.mod_file_version_id
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE pi.profile_id = sqlc.arg(profile_id)
AND pi.enabled = TRUE
);
-- name: UpdateModPageName :exec
UPDATE mod_pages
SET name = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: UpdateModFileLabel :exec
UPDATE mod_files
SET label = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: UpdateModFileVersionNexusFileID :exec
UPDATE mod_file_versions
SET nexus_file_id = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: UpdateModPageNexusInfo :exec
UPDATE mod_pages
SET
nexus_game_domain = ?,
nexus_mod_id = ?,
source_kind = 'nexus',
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: GetModFileVersionLinkState :one
SELECT
mfv.id,
mfv.nexus_file_id,
mfv.archive_sha256,
mf.id as mod_file_id,
mf.label,
mf.is_primary,
mf.mod_page_id,
mp.nexus_game_domain,
mp.nexus_mod_id,
mp.source_kind,
b.size_bytes as archive_size
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
JOIN blobs b ON b.sha256 = mfv.archive_sha256
WHERE mfv.id = ?
AND mp.game_install_id = ?;
-- name: GetUnlinkedNexusModFileVersions :many
SELECT
mfv.id as version_id,
mfv.original_name,
mfv.archive_sha256,
mf.id as mod_file_id,
mf.label,
mp.id as mod_page_id,
mp.name as mod_page_name,
mp.nexus_game_domain,
mp.nexus_mod_id,
b.size_bytes as archive_size
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
JOIN blobs b ON b.sha256 = mfv.archive_sha256
WHERE mp.game_install_id = ?
AND mp.source_kind = 'nexus'
AND mp.nexus_game_domain IS NOT NULL
AND mp.nexus_mod_id IS NOT NULL
AND mfv.nexus_file_id IS NULL;
-- name: GetSkippableModFileVersions :many
SELECT
mfv.id as version_id,
mf.label,
mp.name as mod_page_name
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE mp.game_install_id = ?
AND (
mp.source_kind != 'nexus'
OR mp.nexus_game_domain IS NULL
OR mp.nexus_mod_id IS NULL
)
AND mfv.nexus_file_id IS NULL;
-- name: GetModFileVersionNexusFileID :one
SELECT mfv.nexus_file_id
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE mfv.id = ?
AND mp.game_install_id = ?;
-- name: UnlinkModFileVersionNexus :exec
UPDATE mod_file_versions
SET nexus_file_id = NULL,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE mod_file_versions.id = ?
AND mod_file_versions.id IN (
SELECT mfv.id
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE mp.game_install_id = ?
);
-- name: GetNexusLinkedModPages :many
SELECT DISTINCT
mp.id as mod_page_id,
mp.name AS mod_page_name,
mp.nexus_game_domain,
mp.nexus_mod_id
FROM mod_pages mp
JOIN mod_files mf ON mf.mod_page_id = mp.id
JOIN mod_file_versions mfv ON mfv.mod_file_id = mf.id
WHERE mp.game_install_id = ?
AND mp.source_kind = 'nexus'
AND mp.nexus_game_domain IS NOT NULL
AND mp.nexus_mod_id IS NOT NULL
AND mfv.nexus_file_id IS NOT NULL;
-- name: GetLinkedModFileVersionsForPage :many
SELECT
mfv.id as version_id,
mfv.nexus_file_id,
mfv.version_string,
mf.id as mod_file_id,
mf.label as file_label,
mp.name as mod_page_name
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE mf.mod_page_id = ?
AND mfv.nexus_file_id IS NOT NULL;
-- name: GetModPageByID :one
SELECT
mp.id,
mp.name,
mp.source_kind,
mp.source_url,
mp.source_ref,
mp.nexus_game_domain,
mp.nexus_mod_id,
mp.notes,
mp.created_at,
mp.updated_at
FROM mod_pages mp
WHERE mp.id = ?
AND mp.game_install_id = ?;
-- name: GetModPage :one
SELECT * FROM mod_pages
WHERE id = ?;
-- name: GetModFilesWithVersions :many
SELECT
mf.id AS mod_file_id,
mf.label AS file_label,
mf.is_primary,
mfv.id AS mod_file_version_id,
mfv.version_string,
mfv.archive_sha256,
mfv.nexus_file_id,
mfv.inventory_scanned_at,
mfv.original_name,
b.size_bytes
FROM mod_files mf
JOIN mod_file_versions mfv ON mfv.mod_file_id = mf.id
JOIN blobs b ON b.sha256 = mfv.archive_sha256
WHERE mf.mod_page_id = ?
ORDER BY mf.id ASC, mfv.id ASC;
-- name: GetModFileVersionProfiles :many
SELECT
p.id AS profile_id,
p.name AS profile_name,
pi.enabled,
pi.priority
FROM profile_items pi
JOIN profiles p ON p.id = pi.profile_id
WHERE pi.mod_file_version_id = ?
AND p.game_install_id = ?
ORDER BY p.name ASC;
-- name: AddModIncompatibility :exec
INSERT INTO mod_incompatibilities (
mod_page_id_a,
mod_page_id_b,
reason,
source
) VALUES (
-- enforce canonical ordering so (A,B) and (B,A) are always the same row
MIN(CAST(sqlc.arg(mod_page_id_a) AS INTEGER), CAST(sqlc.arg(mod_page_id_b) AS INTEGER)),
MAX(CAST(sqlc.arg(mod_page_id_a) AS INTEGER), CAST(sqlc.arg(mod_page_id_b) AS INTEGER)),
sqlc.arg(reason),
'user'
);
-- name: RemoveModIncompatibility :execrows
DELETE FROM mod_incompatibilities
WHERE mod_page_id_a = MIN(sqlc.arg(mod_page_id_a), sqlc.arg(mod_page_id_b))
AND mod_page_id_b = MAX(sqlc.arg(mod_page_id_a), sqlc.arg(mod_page_id_b));
-- name: ListModIncompatibilities :many
SELECT
mi.id,
mi.mod_page_id_a,
mi.mod_page_id_b,
mpa.name AS mod_page_name_a,
mpb.name AS mod_page_name_b,
mi.reason,
mi.created_at
FROM mod_incompatibilities mi
JOIN mod_pages mpa ON mpa.id = mi.mod_page_id_a
JOIN mod_pages mpb ON mpb.id = mi.mod_page_id_b
WHERE mpa.game_install_id = ?
ORDER BY mi.created_at DESC;
-- name: GetProfileItemsForPlanning :many
SELECT
pi.id AS item_id,
pi.priority,
pi.enabled,
pi.target_id,
pi.remap_config_id,
mfv.id AS mod_file_version_id,
mfv.archive_sha256,
mfv.inventory_scanned_at,
mfv.version_string,
mf.label AS file_label,
mp.name AS mod_page_name
FROM profile_items pi
JOIN mod_file_versions mfv ON mfv.id = pi.mod_file_version_id
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE pi.profile_id = ?
AND pi.target_id = ?
AND pi.enabled = TRUE
ORDER BY pi.priority DESC;
-- name: GetInventoryEntriesForArchive :many
SELECT
id,
archive_sha256,
raw_path,
entry_type,
size_bytes,
position
FROM archive_inventory_entries
WHERE archive_sha256 = ?
AND entry_type = 'file'
AND parse_error IS NULL
ORDER BY position ASC;
-- name: GetRemapRulesForConfig :many
SELECT *
FROM remap_rules
WHERE remap_config_id = ?
ORDER BY position ASC;
-- name: GetInstalledFilesForTarget :many
SELECT * FROM installed_files
WHERE game_install_id = ?
AND target_id = ?;
-- name: GetBackupForPath :one
SELECT
id,
backup_blob_sha256,
original_content_sha256,
size_bytes
FROM backups
WHERE game_install_id = ?
AND target_id = ?
AND relpath = ?;
-- name: GetMaxRemapRulePosition :one
SELECT CAST(COALESCE(MAX(position), -1) AS INTEGER) AS max_position
FROM remap_rules
WHERE remap_config_id = ?;
-- name: CreateRemapConfig :one
INSERT INTO remap_configs (created_at, updated_at)
VALUES (
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
RETURNING id;
-- name: CreateRemapRule :one
INSERT INTO remap_rules (
remap_config_id,
position,
rule_type,
int_value,
text_value,
created_at,
updated_at
) VALUES (?, ?, ?, ?, ?, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
RETURNING id, position;
-- name: SetProfileItemRemapConfig :exec
UPDATE profile_items
SET remap_config_id = ?,
updated_at = (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE id = ?;
-- name: DeleteRemapRule :exec
DELETE FROM remap_rules
WHERE remap_config_id = ? AND position = ?;
-- name: DeleteRemapConfig :exec
DELETE FROM remap_configs
WHERE id = ?;
-- name: ListRemapRulesForProfileItem :many
SELECT
rr.id,
rr.position,
rr.rule_type,
rr.int_value,
rr.text_value
FROM remap_rules rr
JOIN profile_items pi ON pi.remap_config_id = rr.remap_config_id
WHERE pi.id = ?
ORDER BY rr.position ASC;
-- name: GetProfileItemRemapConfigID :one
SELECT remap_config_id
FROM profile_items
WHERE id = ?;
-- name: GetModFileVersionLabel :one
SELECT
mp.name AS mod_page_name,
mf.label AS file_label
FROM mod_file_versions mfv
JOIN mod_files mf ON mf.id = mfv.mod_file_id
JOIN mod_pages mp ON mp.id = mf.mod_page_id
WHERE mfv.id = ?;
-- name: SetInventoryEntryContentSha256 :exec
UPDATE archive_inventory_entries
SET content_sha256 = ?
WHERE archive_sha256 = ? AND position = ?;
-- name: UpsertBackup :exec
INSERT OR REPLACE INTO backups (
game_install_id,
target_id,
relpath,
backup_blob_sha256,
original_content_sha256,
size_bytes,
created_by_operation_id,
created_at
) VALUES (
?, ?, ?, ?, ?, ?,
?,
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
);
-- name: CreateOperation :one
INSERT INTO operations (
game_install_id,
profile_id,
op_type,
status,
started_at
) VALUES (
?,
?,
?,
'running',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
RETURNING id, started_at;
-- name: FinishOperation :exec
UPDATE operations
SET status = ?,
finished_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
message = ?
WHERE id = ?;
-- name: InsertOperationChange :exec
INSERT INTO operation_changes (
operation_id,
game_install_id,
target_id,
relpath,
action,
old_content_sha256,
new_content_sha256,
old_size_bytes,
new_size_bytes,
mod_file_version_id,
owner_override_id,
backup_blob_sha256,
notes
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
-- name: UpsertInstalledFile :exec
INSERT INTO installed_files (
game_install_id,
target_id,
relpath,
content_sha256,
size_bytes,
owner_mod_file_version_id,
owner_override_id,
owner_profile_id,
last_operation_id,
installed_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
ON CONFLICT(game_install_id, target_id, relpath) DO UPDATE SET
content_sha256 = excluded.content_sha256,
size_bytes = excluded.size_bytes,
owner_mod_file_version_id = excluded.owner_mod_file_version_id,
owner_override_id = excluded.owner_override_id,
owner_profile_id = excluded.owner_profile_id,
last_operation_id = excluded.last_operation_id,
installed_at = excluded.installed_at;
-- name: DeleteInstalledFile :exec
DELETE FROM installed_files
WHERE game_install_id = ? AND target_id = ? AND relpath = ?;
-- name: UpdateGameInstallAppliedState :exec
UPDATE game_installs
SET applied_profile_id = ?,
applied_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
applied_operation_id = ?,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: ClearGameInstallAppliedState :exec
UPDATE game_installs
SET applied_profile_id = NULL,
applied_at = NULL,
applied_operation_id = NULL,
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?;
-- name: UpdateInventoryEntryContentSha256 :exec
UPDATE archive_inventory_entries
SET content_sha256 = ?
WHERE archive_sha256 = ? AND position = ?
AND content_sha256 IS NULL;
-- name: GetLastOperationForGameInstall :one
SELECT id, op_type, status, started_at, finished_at, message
FROM operations
WHERE game_install_id = ?
ORDER BY started_at DESC
LIMIT 1;
-- name: GetCompletedPathsForOperation :many
SELECT relpath
FROM operation_changes
WHERE operation_id = ?
AND action != 'noop';
-- name: DeleteBackup :exec
DELETE FROM backups
WHERE game_install_id = ? AND target_id = ? AND relpath = ?;
-- name: GetProfileByID :one
SELECT *
FROM profiles
WHERE id = ?;
-- name: GetProfileHasPendingChanges :one
WITH desired AS (
SELECT mod_file_version_id
FROM profile_items
WHERE profile_id = ?
AND enabled = TRUE
),
current AS (