]> pd.if.org Git - zpackage/blob - schema/syncinfo.sql
separate syncinfo from main schema
[zpackage] / schema / syncinfo.sql
1 create view syncinfo as
2 with
3 -- paths to libraries we need to keep around
4 preserve as (
5 select distinct PFL.*
6 from packagefiles PFL
7 join elflibraries EL on EL.file = PFL.hash
8 join elfneeded EN on EN.needed = EL.soname
9 join packagefiles PFN on EN.file = PFN.hash
10 join packages PN
11 on PFN.package = PN.package and PFN.version = PN.version and PFN.release = PN.release
12 where
13 PN.status = 'installing' or PN.status = 'installed'
14 and not (PFN.package = PFL.package and PFN.version = PFN.version and PFN.release = PFL.release)
15 ),
16 waspreserved as (
17         select PF.path
18         from packagefiles PF
19         join elflibraries EL on EL.file = PF.hash
20         join elfneeded EN on EN.needed = EL.soname
21         join packagefiles PL on EN.file = PL.hash
22         join packages P
23         on PL.path = P.package and PL.version = P.version and PL.release = P.release
24         where
25         P.status = 'removing' or P.status = 'installed' or P.status = 'updating'
26 )
27
28 -- every path in 'installing' is either new or update, or no-op
29 select
30 case
31 when PFC.path is null and PFC.path not in (select path from waspreserved) then 'new'
32 when 
33         printf('%s:%s:%s:%s', PFI.filetype, PFI.mode, PFI.username, PFI.groupname) is
34         printf('%s:%s:%s:%s', PFC.filetype, PFC.mode, PFC.username, PFC.groupname)
35         and PFI.hash is PFC.hash
36         then 'noop'
37 else 'update'
38 end as op,
39         printf('%s-%s-%s', PFI.package, PFI.version, PFI.release) as pkgid,
40         PFI.path, 
41         PFI.username, PFI.uid, PFI.groupname, PFI.gid, PFI.mode, PFI.filetype,
42         PFI.mtime, PFI.hash,
43         PFI.configuration + case when PFC.configuration = 1 then 2 else 0 end
44         as configuration,
45         PFI.target, PFI.device,
46         PFC.hash as ohash,
47         printf('%s:%s:%s:%s', PFI.filetype, PFI.mode, PFI.username, PFI.groupname) as mds,
48         printf('%s:%s:%s:%s', PFC.filetype, PFC.mode, PFC.username, PFC.groupname) as omds
49 from
50 packagefiles PFI
51 join packages PI
52 on PFI.package = PI.package and PFI.version = PI.version and PFI.release = PI.release
53 left join packages PC
54 on PC.package = PI.package and PC.status in ('installed','removing','updating')
55 left join packagefiles PFC
56         on PFC.package = PC.package
57         and PFC.version = PC.version
58         and PFC.release = PC.release
59         and PFC.path = PFI.path
60 where
61 PI.status = 'installing'
62
63 -- every path in updating is either remove or no-op
64 union
65 select 'remove',
66         printf('%s-%s-%s', PFU.package, PFU.version, PFU.release) as pkgid,
67         PFU.path, 
68         PFU.username, PFU.uid, PFU.groupname, PFU.gid, PFU.mode, PFU.filetype,
69         PFU.mtime, PFU.hash,
70         PFU.configuration + case when PFC.configuration = 1 then 2 else 0 end
71         as configuration,
72         PFU.target, PFU.device,
73         null as ohash,
74         printf('%s:%s:%s:%s', PFU.filetype, PFU.mode, PFU.username, PFU.groupname) as mds,
75         null as omds
76 from packagefiles PFU
77 join packages PU
78 on PFU.package = PU.package and PFU.version = PU.version and PFU.release = PU.release
79 -- inner join because the installing package must exist or this shouldn't
80 -- be an 'updating' package
81 join packages P on PU.package = P.package and P.status = 'installing'
82 left join packagefiles PFI on
83         PFI.package = P.package
84         and PFI.version = P.version
85         and PFI.release = P.release
86         and PFI.path = PFU.path
87
88 -- handle paths owned by other installed packages
89 left join packages PI on PI.status = 'installed'
90 left join packagefiles PFC
91 on PFC.package = PI.package and PFC.version = PI.version and PFC.release = PI.release and PFC.path = PFU.path
92
93 where
94 PU.status in ('updating')
95 and PFI.path is null
96 and PFC.path is null
97 and PFU.path not in (select path from preserve)
98
99 -- every path in removing is either remove or no-op
100 -- not true, could be an update, but should be handled above
101 union
102 select 'remove',
103         printf('%s-%s-%s', PFR.package, PFR.version, PFR.release) as pkgid,
104         PFR.path, 
105         PFR.username, PFR.uid, PFR.groupname, PFR.gid, PFR.mode, PFR.filetype,
106         PFR.mtime, PFR.hash,
107         PFR.configuration + case when PFC.configuration = 1 then 2 else 0 end
108         as configuration,
109         PFR.target, PFR.device,
110         null as ohash,
111         printf('%s:%s:%s:%s', PFR.filetype, PFR.mode, PFR.username, PFR.groupname) as mds,
112         null as omds
113 from packagefiles PFR
114 join packages PU
115 on PFR.package = PU.package and PFR.version = PU.version and PFR.release = PU.release
116 left join packages P on PU.package = P.package and P.status = 'installing'
117 left join packagefiles PFI on PFI.path = PFR.path
118 and PFI.package = P.package and PFI.version = P.version and PFI.release = P.release
119
120 -- handle paths owned by other installed packages
121 left join packages PI on PI.status = 'installed'
122 left join packagefiles PFC
123 on PFC.package = PI.package and PFC.version = PI.version and PFC.release = PI.release and PFC.path = PFR.path
124
125 where
126 PU.status in ('removing')
127 and PFI.path is null
128 and PFC.path is null
129 and PFR.path not in (select path from preserve)
130
131 -- paths in 'installed' or 'updated' are no-ops
132
133 union
134 select 'preserve',
135         printf('%s-%s-%s', PFP.package, PFP.version, PFP.release) as pkgid,
136         PFP.path, 
137         PFP.username, PFP.uid, PFP.groupname, PFP.gid, PFP.mode, PFP.filetype,
138         PFP.mtime, PFP.hash,
139         PFP.configuration as configuration,
140         PFP.target, PFP.device,
141         null as ohash,
142         printf('%s:%s:%s:%s', PFP.filetype, PFP.mode, PFP.username, PFP.groupname) as mds,
143         null as omds
144 from preserve PFP
145         -- TODO don't include installed or installing
146         -- left join packages P on P.package = PFP.package ...
147         -- where P.status not in ('installed','installing')
148 ;