aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/state_impl/_operations/prune_orphans.py
blob: 7bb5eb52a257a383e771345ed6daae02cbb07fa0 (about) (plain)
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
# SPDX-License-Identifier: GPL-3.0-or-later

# Haketilo proxy data and configuration (removal of packages that are not used).
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use of this
# code in a proprietary program, I am not going to enforce this in
# court.

"""
....
"""

import sqlite3

from pathlib import Path


def _remove_item_versions(cursor: sqlite3.Cursor, with_installed: bool) -> None:
    cursor.execute(
        '''
        CREATE TEMPORARY TABLE __removed_versions(
                item_version_id INTEGER PRIMARY KEY
        );
        '''
    )

    condition = "iv.active != 'R'" if with_installed else "iv.installed != 'I'"

    cursor.execute(
        f'''
        INSERT INTO
                __removed_versions
        SELECT
                iv.item_version_id
        FROM
                     item_versions     AS iv
                JOIN orphan_iterations AS oi USING (repo_iteration_id)
        WHERE
                {condition};
        '''
    )

    cursor.execute(
        '''
        UPDATE
                mapping_statuses
        SET
                active_version_id = NULL
        WHERE
                active_version_id IN __removed_versions;
        '''
    )

    cursor.execute(
        '''
        DELETE FROM
                item_versions
        WHERE
                item_version_id IN __removed_versions;
        '''
    )

    cursor.execute('DROP TABLE __removed_versions;')

_remove_items_sql = '''
WITH removed_items AS (
        SELECT
                i.item_id
        FROM
                          items            AS i
                LEFT JOIN item_versions    AS iv USING (item_id)
                LEFT JOIN mapping_statuses AS ms USING (item_id)
        WHERE
                 iv.item_version_id IS NULL AND
                 (i.type = 'R' OR ms.enabled = 'N')
)
DELETE FROM
        items
WHERE
        item_id IN removed_items;
'''

_remove_files_sql = '''
WITH removed_files AS (
        SELECT
                f.file_id
        FROM
                          files     AS f
                LEFT JOIN file_uses AS fu USING (file_id)
        WHERE
                fu.file_use_id IS NULL
)
DELETE FROM
        files
WHERE
        file_id IN removed_files;
'''

_forget_files_data_sql = '''
WITH forgotten_files AS (
        SELECT
                f.file_id
        FROM
                          files         AS f
                     JOIN file_uses     AS fu
                        USING (file_id)
                LEFT JOIN item_versions AS iv
                        ON (fu.item_version_id = iv.item_version_id AND
                            iv.installed = 'I')
        GROUP BY
                f.file_id
        HAVING
                COUNT(iv.item_version_id) = 0
)
UPDATE
        files
SET
        data = NULL
WHERE
        file_id IN forgotten_files;
'''

_remove_repo_iterations_sql = '''
WITH removed_iterations AS (
        SELECT
                oi.repo_iteration_id
        FROM
                          orphan_iterations AS oi
                LEFT JOIN item_versions     AS iv USING (repo_iteration_id)
        WHERE
                iv.item_version_id IS NULL
)
DELETE FROM
        repo_iterations
WHERE
        repo_iteration_id IN removed_iterations;
'''

_remove_repos_sql = '''
WITH removed_repos AS (
        SELECT
                r.repo_id
        FROM
                          repos           AS r
                LEFT JOIN repo_iterations AS ri USING (repo_id)
        WHERE
                r.deleted AND ri.repo_iteration_id IS NULL AND r.repo_id != 1
)
DELETE FROM
        repos
WHERE
        repo_id IN removed_repos;
'''

def prune_orphans(cursor: sqlite3.Cursor, aggressive: bool = False) -> None:
    assert cursor.connection.in_transaction

    _remove_item_versions(cursor, with_installed=aggressive)
    cursor.execute(_remove_items_sql)
    cursor.execute(_remove_files_sql)
    cursor.execute(_forget_files_data_sql)
    cursor.execute(_remove_repo_iterations_sql)
    cursor.execute(_remove_repos_sql)