Skip to content
Merged
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
57 changes: 45 additions & 12 deletions server/migrations/community/bd1ec73db389_create_file_diff_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ def upgrade():
b.basefile_version
FROM diffs d
LEFT OUTER JOIN basefiles b ON b.file_path_id = d.file_path_id AND b.basefile_version < d.project_version_name
)
),
file_diffs AS (
SELECT DISTINCT
d.file_path_id,
FIRST_VALUE(rb.basefile_id) OVER (PARTITION BY rb.id ORDER BY rb.basefile_version DESC) as basefile_id,
0 AS rank,
d.project_version_name AS version,
(d.diff ->> 'path') AS path,
(d.diff ->> 'size')::bigint AS size,
d.diff ->> 'checksum' AS checksum,
d.diff ->> 'location' AS location
FROM diffs d
LEFT OUTER JOIN relevant_basefiles rb ON rb.id = d.id
)
INSERT INTO file_diff (file_path_id, basefile_id, rank, version, path, size, checksum, location)
SELECT DISTINCT
d.file_path_id,
FIRST_VALUE(rb.basefile_id) OVER (PARTITION BY rb.id ORDER BY rb.basefile_version DESC) as basefile_id,
0 AS rank,
d.project_version_name AS version,
(d.diff ->> 'path') AS path,
(d.diff ->> 'size')::bigint AS size,
d.diff ->> 'checksum' AS checksum,
d.diff ->> 'location' AS location
FROM diffs d
LEFT OUTER JOIN relevant_basefiles rb ON rb.id = d.id;
-- it seems that some projects / files might be broken so we need to play it safe here
SELECT * FROM file_diffs WHERE basefile_id IS NOT NULL;
"""
)

Expand Down Expand Up @@ -132,6 +136,35 @@ def downgrade():
"""
)

# if there were any broken gpkg files (ommited in upgrade), let's add there a dummy diff
conn.execute(
"""
UPDATE file_history fh
SET diff = jsonb_build_object(
'path', 'missing-diff',
'size', 0,
'checksum', '',
'location', ''
)
WHERE fh.change = 'update_diff' AND fh.diff IS NULL;
"""
)

# add back consistency constraint
conn.execute(
sa.text(
"""
ALTER TABLE file_history
ADD CONSTRAINT ck_file_history_changes_with_diff CHECK (
CASE
WHEN (change = 'update_diff') THEN diff IS NOT NULL
ELSE diff IS NULL
END
);
"""
)
)

op.drop_index(op.f("ix_file_diff_version"), table_name="file_diff")
op.drop_index(op.f("ix_file_diff_rank"), table_name="file_diff")
op.drop_index(op.f("ix_file_diff_path"), table_name="file_diff")
Expand Down
Loading