Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

improved sql command #778

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions migrations/20231116112800_side_types_overhaul.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE INDEX version_fields_version_id ON version_fields (version_id);
CREATE INDEX hashes_file_id ON hashes (file_id);

INSERT INTO loader_fields (field, field_type, optional) SELECT 'singleplayer', 'boolean', false;
INSERT INTO loader_fields (field, field_type, optional) SELECT 'client_and_server', 'boolean', false;
Expand All @@ -14,41 +16,57 @@ ALTER TABLE versions ADD COLUMN server_only boolean;
UPDATE versions v SET singleplayer = true
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.id AND vf.enum_value = lfev.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
WHERE v.id = vf.version_id
AND (lf.field = 'client_side' OR lf.field = 'server_side') AND (lfev.value = 'required' OR lfev.value = 'optional');

-- Set client and server to be true if either client_side or server_side is 'required' OR 'optional'
UPDATE versions v SET client_and_server = true
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.id AND vf.enum_value = lfev.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
WHERE v.id = vf.version_id
AND (lf.field = 'client_side' OR lf.field = 'server_side') AND (lfev.value = 'required' OR lfev.value = 'optional');

-- Set client_only to be true if client_side is 'required' or 'optional', and server_side is 'optional', 'unsupported', or 'unknown'
UPDATE versions v SET client_only = true
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
CROSS JOIN version_fields vf2
INNER JOIN loader_fields lf2 ON vf2.field_id = lf2.id
INNER JOIN loader_field_enum_values lfev2 ON lf2.enum_type = lfev2.enum_id AND vf2.enum_value = lfev2.id
WHERE v.id = vf.version_id AND v.id = vf2.version_id
AND lf.field = 'client_side' AND (lfev.value = 'required' OR lfev.value = 'optional')
AND lf2.field = 'server_side' AND (lfev2.value = 'optional' OR lfev2.value = 'unsupported' OR lfev2.value = 'unknown');
-- -- Set client_only to be true if client_side is 'required' or 'optional', and server_side is 'optional', 'unsupported', or 'unknown'
UPDATE versions v
SET client_only = true
WHERE EXISTS (
SELECT 1
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
WHERE v.id = vf.version_id
AND lf.field = 'client_side' AND (lfev.value = 'required' OR lfev.value = 'optional')
)
AND EXISTS (
SELECT 1
FROM version_fields vf2
INNER JOIN loader_fields lf2 ON vf2.field_id = lf2.id
INNER JOIN loader_field_enum_values lfev2 ON lf2.enum_type = lfev2.enum_id AND vf2.enum_value = lfev2.id
WHERE v.id = vf2.version_id
AND lf2.field = 'server_side' AND (lfev2.value = 'optional' OR lfev2.value = 'unsupported' OR lfev2.value = 'unknown')
);

-- Set server_only to be true if server_side is 'required' or 'optional', and client_side is 'optional', 'unsupported', or 'unknown'
UPDATE versions v SET server_only = true
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
CROSS JOIN version_fields vf2
INNER JOIN loader_fields lf2 ON vf2.field_id = lf2.id
INNER JOIN loader_field_enum_values lfev2 ON lf2.enum_type = lfev2.enum_id AND vf2.enum_value = lfev2.id
WHERE v.id = vf.version_id AND v.id = vf2.version_id
AND lf.field = 'server_side' AND (lfev.value = 'required' OR lfev.value = 'optional')
AND lf2.field = 'client_side' AND (lfev2.value = 'optional' OR lfev2.value = 'unsupported' OR lfev2.value = 'unknown');
-- -- Set server_only to be true if server_side is 'required' or 'optional', and client_side is 'optional', 'unsupported', or 'unknown'
UPDATE versions v
SET server_only = true
WHERE EXISTS (
SELECT 1
FROM version_fields vf
INNER JOIN loader_fields lf ON vf.field_id = lf.id
INNER JOIN loader_field_enum_values lfev ON lf.enum_type = lfev.enum_id AND vf.enum_value = lfev.id
WHERE v.id = vf.version_id
AND lf.field = 'server_side' AND (lfev.value = 'required' OR lfev.value = 'optional')
)
AND EXISTS (
SELECT 1
FROM version_fields vf2
INNER JOIN loader_fields lf2 ON vf2.field_id = lf2.id
INNER JOIN loader_field_enum_values lfev2 ON lf2.enum_type = lfev2.enum_id AND vf2.enum_value = lfev2.id
WHERE v.id = vf2.version_id
AND lf2.field = 'client_side' AND (lfev2.value = 'optional' OR lfev2.value = 'unsupported' OR lfev2.value = 'unknown')
);

-- Insert the values into the version_fields table
INSERT INTO version_fields (version_id, field_id, int_value)
Expand Down
2 changes: 0 additions & 2 deletions migrations/20231130102300_additional_indices.sql

This file was deleted.

Loading