]> pd.if.org Git - zpackage/blob - db.sql
add metadata string to packagefiles view
[zpackage] / db.sql
1 begin;
2
3 PRAGMA application_id = 0x5a504442;
4 PRAGMA user_version = 1;
5
6 -- should be faster with rowid due to the blob content
7 -- these are really just blobs of data
8 -- TODO copyright and license information should probably
9 -- go here
10 CREATE TABLE files (
11         hash text primary key, -- sha256 of content
12         size integer, -- bigint?  certainly need > 2GB
13         compression text, -- always xz?
14         content blob
15 )
16 ;
17
18 -- information about packages
19 -- a package is identified by a package,version,release triple
20 create table packages (
21         -- primary key columns
22         package text not null,
23         version text not null, -- the upstream version string
24         release integer not null, -- the local release number
25 --      pkgid   text, -- the three above joined with '-'
26
27         -- metadata columns
28         description     text,
29         architecture    text,
30         url     text,
31         status  text,
32         licenses        text, -- hash of actual license?  need table for more than one?
33         packager        text,
34         build_time      integer default (strftime('%s', 'now')),
35         install_time    integer,
36         checksum        text, -- checksum of package contents.  null for incompleted packages
37         primary key (package,version,release),
38         check (typeof(package) = 'text'),
39         check (typeof(version) = 'text'),
40         check (typeof(release) = 'integer'),
41         check (release > 0)
42         -- TODO enforce name and version conventions
43         -- check(instr(version,'-') = 0)
44         -- check(instr(package,'/') = 0)
45         -- check(instr(package,'/') = 0)
46         -- check(instr(version,' ') = 0)
47         -- check(instr(package,' ') = 0)
48         -- check(instr(package,' ') = 0)
49         -- check(length(package) < 64)
50         -- check(length(version) < 32)
51 )
52 without rowid
53 ;
54
55 create index package_status_index on packages (status);
56
57 create view packages_pkgid as
58 select printf('%s-%s-%s', package, version, release) as pkgid, *
59 from packages;
60
61 create trigger packages_update_trigger instead of
62 update on packages_pkgid
63 begin
64         update packages
65         set package = NEW.package,
66         version = NEW.version,
67         release = NEW.release,
68         description = NEW.description,
69         architecture = NEW.architecture,
70         url = NEW.url,
71         status = NEW.status,
72         licenses = NEW.licenses,
73         packager = NEW.packager,
74         build_time = NEW.build_time,
75         install_time = NEW.install_time,
76         checksum = NEW.checksum
77         where package = OLD.package
78         and version = OLD.version
79         and release = OLD.release
80         ;
81 end
82 ;
83
84 -- handle package status history with a logging trigger.
85 create trigger logpkgstatus after update of status on packages
86 begin insert into zpmlog (action,target,info)
87         values (printf('status change %s %s', OLD.status, NEW.status),
88                 printf('%s-%s-%s', NEW.package, NEW.version, NEW.release),
89                 NULL); END;
90
91 create table packagetags (
92         -- package id triple
93         package text,
94         version text,
95         release integer,
96         tag     text,
97         set_time integer default (strftime('%s', 'now')),
98         primary key (package,version,release,tag),
99         foreign key (package,version,release) references packages (package,version,release) on delete cascade on update cascade
100 );
101
102 -- packagefile hash is columns as text, joined with null bytes, then
103 -- sha256 sum of that
104 -- package checksum is package columns as text, joined with null bytes,
105 -- other than the checksum and install_time column
106 -- then that hashed.  finally, that hash, plus the ascii sorted
107 -- hashes of the package files all joined with newlines, hashed.
108 -- really don't like this.
109
110 -- files contained in a package
111 create table packagefiles (
112         -- package id triple
113         package text,
114         version text,
115         release integer,
116
117         path    text, -- filesystem path
118         mode    text not null, -- perms, use text for octal rep?
119         username        text not null, -- name of owner
120         groupname       text not null, -- group of owner
121         uid     integer, -- numeric uid, generally ignored
122         gid     integer, -- numeric gid, generally ignored
123         configuration integer not null default 0, -- boolean if config file
124         filetype varchar not null default 'r',
125         -- r regular file
126         -- d directory
127         -- s symlink
128         -- h hard link -- not supported
129         -- c character special and b device special files add dev number column
130         -- b block special
131         -- p fifos (i.e. pipe)
132         target  text, -- link target for links
133         -- device file dev numbers, should probably be a separate table
134         devmajor        integer,
135         devminor        integer,
136         hash    text, -- null if no actual content, i.e. anything but a regular file
137         mtime   integer, -- seconds since epoch, finer resolution probably not needed
138         primary key (package,version,release,path),
139         foreign key (package,version,release) references packages (package,version,release) on delete cascade on update cascade,
140         check (not (filetype = 'l' and target is null)),
141         check (not (filetype = 'r' and hash is null)),
142         check (not (filetype = 'c' and (devmajor is null or devminor is null)))
143 )
144 without rowid
145 ;
146
147 create view packagefiles_pkgid as
148 select printf('%s-%s-%s', package, version, release) as pkgid, *,
149 printf('%s:%o:%s:%s', filetype, mode, username, groupname) as mds
150 from packagefiles
151 ;
152
153 create view installed_ref_count as
154 select I.path, count(*) as refcount
155 from installedfiles I
156 group by I.path
157 ;
158
159 create view sync_status_ref_count as
160 select path, status, count(*) as refcount
161 from packagefiles_status
162 where status in ('installed', 'installing', 'removing')
163 group by path, status
164 ;
165
166 create view packagefiles_status as
167 select P.status, PF.*
168 from packagefiles_pkgid PF
169 left join packages_pkgid P on P.pkgid = PF.pkgid
170 ;
171
172 create view installedfiles as
173 select * from packagefiles_status
174 where status = 'installed'
175 ;
176
177 create view install_status as
178
179 select 'new' as op, PN.*
180 from packagefiles_status PN
181 left join installed_ref_count RC on RC.path = PN.path
182 where RC.refcount is null
183 and PN.status = 'installing'
184
185 union all
186
187 select 'update' as op, PN.*
188 from packagefiles_status PN
189 inner join installedfiles PI on PI.path = PN.path and PI.package = PN.package
190 left join installed_ref_count RC on RC.path = PN.path
191 where RC.refcount = 1
192 and PN.status = 'installing'
193 and PI.hash is not PN.hash
194
195 union all
196
197 select 'conflict' as op, PI.*
198 from packagefiles_status PN
199 inner join installedfiles PI on PI.path = PN.path and PI.package != PN.package
200 where PN.status = 'installing'
201
202 union all
203 select 'remove' as op, PI.*
204 from installedfiles PI
205 left join packagefiles_status PN
206     on PI.path = PN.path and PI.package = PN.package
207         and PI.pkgid != PN.pkgid
208 where PN.path is null
209 and PI.package in (select package from packages where status = 'installing')
210
211 union all
212 -- remove files in removing, but not installing
213 select distinct 'remove' as op, PR.*
214 from packagefiles_status PR
215 left join packagefiles_status PN
216 on PR.path = PN.path
217 and PR.pkgid != PN.pkgid and PN.status in ('installing', 'installed')
218 where PN.path is null
219 and PR.status = 'removing'
220 ;
221
222 create table pathtags (
223         -- package id triple
224         package text,
225         version text,
226         release integer,
227
228         path    text, -- filesystem path
229         tag     text,
230         primary key (package,version,release,path,tag),
231         foreign key (package,version,release,path)
232         references packagefiles on delete cascade on update cascade
233 )
234 without rowid
235 ;
236
237 create table elfinfo (
238         file    text primary key, -- hash of blob
239         elftype text,
240         foreign key (file) references files on delete cascade
241 )
242 without rowid
243 ;
244
245 create table elfdeps (
246         file    text,
247         soname  text,
248         dependency text,
249         primary key (file, soname, dependency),
250         foreign key (file) references files on delete cascade
251 )
252 without rowid
253 ;
254
255 -- TODO just elf information?
256 -- and just hash, not package?
257 create table elflibraries (
258         file    text primary key,
259         soname  text,
260         foreign key (file) references files on delete cascade
261 )
262 without rowid
263 ;
264
265 create table elfneeded (
266         file    text,
267         needed  text, -- soname of dependency
268         primary key (file, needed),
269         foreign key (file) references files on delete cascade
270 )
271 without rowid
272 ;
273
274 -- package scripts: table of package, stage, file
275 create table scripts (
276         package text,
277         version text,
278         release integer,
279         stage   text,
280         hash    text,
281         primary key (package,version,release,stage),
282         foreign key (package,version,release) references packages (package,version,release) on delete cascade on update cascade
283 );
284
285 create view scripts_pkgid as
286 select printf('%s-%s-%s', package, version, release) as pkgid, *
287 from scripts
288 ;
289
290 -- package dependencies: table of package, dependency, dep type (package, soname)
291 create table packagedeps (
292         package text,
293         version text,
294         release integer,
295         requires text, -- package name (only)
296         minimum text,
297         maximum text,
298         primary key (package,version,release,package),
299         foreign key (package,version,release) references packages (package,version,release) on delete cascade on update cascade
300 );
301
302 -- capability labels
303 create table provides (
304         package text,
305         subpackage      text,
306         label   text -- a capability label
307 );
308
309 create table requires (
310         package text,
311         subpackage      text,
312         label   text -- a capability label
313 );
314
315 create table packagegroups (
316         package text,
317         "group" text
318 );
319
320 -- zpm actions
321 -- not sure how machine readable this needs to be,
322 -- do not at all for now, figure it out later
323 -- could be worth logging all commands in a history table,
324 -- the zpm driver could do that and capture the exit status
325 -- as well
326 -- might want the history table to note a "group" to tie together
327 -- sub-invocations, probably an environment variable set if not
328 -- already set by zpm, probably a uuid or a timestamp
329 create table zpmlog (
330         ts      text default (strftime('%Y-%m-%d %H:%M:%f', 'now')),
331         -- timestamp of action
332         action  text,
333         target  text, -- packagename, repo name, etc
334         info    text -- human readable
335 );
336
337 create table history (
338         ts      integer, -- again, probably needs timestamp sub second
339         cmd     text,
340         args    text,
341         status  integer
342 );
343
344 create table repository (
345         name    text primary key, -- our name for a repo
346         url     text not null,
347         priority        integer not null default 1,
348         refreshed       integer -- last refresh time
349 );
350
351 -- urls for downloading packages.  possibly unneeded
352 create table repository_packages (
353         repo    text,
354         pkg     text, -- glob pattern?  in which case others not needed
355         version text,
356         release text,
357         url     text
358 );
359
360 -- track which repository a package was cloned from, i.e. where we got it
361 create table packagesource (
362         name    text,
363         version text,
364         release integer,
365         repository      text references repository
366 );
367
368 commit;