1 | -- |
---|
2 | -- PyKota - Print Quotas for CUPS and LPRng |
---|
3 | -- |
---|
4 | -- (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
5 | -- This program is free software; you can redistribute it and/or modify |
---|
6 | -- it under the terms of the GNU General Public License as published by |
---|
7 | -- the Free Software Foundation; either version 2 of the License, or |
---|
8 | -- (at your option) any later version. |
---|
9 | -- |
---|
10 | -- This program is distributed in the hope that it will be useful, |
---|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | -- GNU General Public License for more details. |
---|
14 | -- |
---|
15 | -- You should have received a copy of the GNU General Public License |
---|
16 | -- along with this program; if not, write to the Free Software |
---|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | -- |
---|
19 | -- $Id$ |
---|
20 | -- |
---|
21 | -- |
---|
22 | |
---|
23 | -- |
---|
24 | -- PyKota Database creation script for PostgreSQL |
---|
25 | -- |
---|
26 | -- Launch this as PostgreSQL administrator with \i |
---|
27 | -- |
---|
28 | |
---|
29 | |
---|
30 | -- |
---|
31 | -- Create the print quota database |
---|
32 | -- |
---|
33 | CREATE DATABASE pykota; |
---|
34 | |
---|
35 | -- |
---|
36 | -- Create the print quota database users |
---|
37 | -- |
---|
38 | CREATE USER pykotaadmin; |
---|
39 | CREATE USER pykotauser; |
---|
40 | |
---|
41 | -- |
---|
42 | -- Now connect to the new database |
---|
43 | -- |
---|
44 | \connect pykota |
---|
45 | |
---|
46 | -- |
---|
47 | -- Create the users table |
---|
48 | -- |
---|
49 | CREATE TABLE users(id SERIAL PRIMARY KEY NOT NULL, |
---|
50 | username TEXT UNIQUE NOT NULL, |
---|
51 | email TEXT, |
---|
52 | balance FLOAT DEFAULT 0.0, |
---|
53 | lifetimepaid FLOAT DEFAULT 0.0, |
---|
54 | limitby TEXT DEFAULT 'quota', |
---|
55 | overcharge FLOAT NOT NULL DEFAULT 1.0); |
---|
56 | |
---|
57 | -- |
---|
58 | -- Create the groups table |
---|
59 | -- |
---|
60 | CREATE TABLE groups(id SERIAL PRIMARY KEY NOT NULL, |
---|
61 | groupname TEXT UNIQUE NOT NULL, |
---|
62 | limitby TEXT DEFAULT 'quota'); |
---|
63 | |
---|
64 | -- |
---|
65 | -- Create the printers table |
---|
66 | -- |
---|
67 | CREATE TABLE printers(id SERIAL PRIMARY KEY NOT NULL, |
---|
68 | printername TEXT UNIQUE NOT NULL, |
---|
69 | description TEXT, |
---|
70 | priceperpage FLOAT DEFAULT 0.0, |
---|
71 | priceperjob FLOAT DEFAULT 0.0); |
---|
72 | |
---|
73 | -- |
---|
74 | -- Create the print quota table for users |
---|
75 | -- |
---|
76 | CREATE TABLE userpquota(id SERIAL PRIMARY KEY NOT NULL, |
---|
77 | userid INT4 REFERENCES users(id), |
---|
78 | printerid INT4 REFERENCES printers(id), |
---|
79 | lifepagecounter INT4 DEFAULT 0, |
---|
80 | pagecounter INT4 DEFAULT 0, |
---|
81 | softlimit INT4, |
---|
82 | hardlimit INT4, |
---|
83 | datelimit TIMESTAMP, |
---|
84 | warncount INT4 DEFAULT 0); |
---|
85 | CREATE INDEX userpquota_u_id_ix ON userpquota (userid); |
---|
86 | CREATE INDEX userpquota_p_id_ix ON userpquota (printerid); |
---|
87 | CREATE UNIQUE INDEX userpquota_up_id_ix ON userpquota (userid, printerid); |
---|
88 | |
---|
89 | -- |
---|
90 | -- Create the job history table |
---|
91 | -- |
---|
92 | CREATE TABLE jobhistory(id SERIAL PRIMARY KEY NOT NULL, |
---|
93 | jobid TEXT, |
---|
94 | userid INT4, |
---|
95 | printerid INT4, |
---|
96 | pagecounter INT4 DEFAULT 0, |
---|
97 | jobsizebytes INT8, |
---|
98 | jobsize INT4, |
---|
99 | jobprice FLOAT, |
---|
100 | action TEXT, |
---|
101 | filename TEXT, |
---|
102 | title TEXT, |
---|
103 | copies INT4, |
---|
104 | options TEXT, |
---|
105 | hostname TEXT, |
---|
106 | md5sum TEXT, |
---|
107 | pages TEXT, |
---|
108 | billingcode TEXT, |
---|
109 | jobdate TIMESTAMP DEFAULT now(), |
---|
110 | CONSTRAINT checkUserPQuota FOREIGN KEY (userid, printerid) REFERENCES userpquota(userid, printerid)); |
---|
111 | CREATE INDEX jobhistory_u_id_ix ON jobhistory (userid); |
---|
112 | CREATE INDEX jobhistory_p_id_ix ON jobhistory (printerid); |
---|
113 | CREATE INDEX jobhistory_pd_id_ix ON jobhistory (printerid, jobdate); |
---|
114 | CREATE INDEX jobhistory_hostname_ix ON jobhistory (hostname); |
---|
115 | |
---|
116 | -- |
---|
117 | -- Create the print quota table for groups |
---|
118 | -- |
---|
119 | CREATE TABLE grouppquota(id SERIAL PRIMARY KEY NOT NULL, |
---|
120 | groupid INT4 REFERENCES groups(id), |
---|
121 | printerid INT4 REFERENCES printers(id), |
---|
122 | softlimit INT4, |
---|
123 | hardlimit INT4, |
---|
124 | datelimit TIMESTAMP); |
---|
125 | CREATE INDEX grouppquota_g_id_ix ON grouppquota (groupid); |
---|
126 | CREATE INDEX grouppquota_p_id_ix ON grouppquota (printerid); |
---|
127 | CREATE UNIQUE INDEX grouppquota_up_id_ix ON grouppquota (groupid, printerid); |
---|
128 | |
---|
129 | -- |
---|
130 | -- Create the groups/members relationship |
---|
131 | -- |
---|
132 | CREATE TABLE groupsmembers(groupid INT4 REFERENCES groups(id), |
---|
133 | userid INT4 REFERENCES users(id), |
---|
134 | PRIMARY KEY (groupid, userid)); |
---|
135 | |
---|
136 | -- |
---|
137 | -- Create the printer groups relationship |
---|
138 | -- |
---|
139 | CREATE TABLE printergroupsmembers(groupid INT4 REFERENCES printers(id), |
---|
140 | printerid INT4 REFERENCES printers(id), |
---|
141 | PRIMARY KEY (groupid, printerid)); |
---|
142 | -- |
---|
143 | -- Create the table for payments |
---|
144 | -- |
---|
145 | CREATE TABLE payments (id SERIAL PRIMARY KEY NOT NULL, |
---|
146 | userid INT4 REFERENCES users(id), |
---|
147 | amount FLOAT, |
---|
148 | date TIMESTAMP DEFAULT now()); |
---|
149 | CREATE INDEX payments_date_ix ON payments (date); |
---|
150 | |
---|
151 | -- |
---|
152 | -- Create the table for coefficients wrt paper sizes and the like |
---|
153 | -- |
---|
154 | CREATE TABLE coefficients (id SERIAL PRIMARY KEY NOT NULL, |
---|
155 | printerid INTEGER NOT NULL REFERENCES printers(id), |
---|
156 | label TEXT NOT NULL, |
---|
157 | coefficient FLOAT DEFAULT 1.0, |
---|
158 | CONSTRAINT coeffconstraint UNIQUE (printerid, label)); |
---|
159 | |
---|
160 | -- |
---|
161 | -- Set some ACLs |
---|
162 | -- |
---|
163 | REVOKE ALL ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients FROM public; |
---|
164 | REVOKE ALL ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq, payments_id_seq, coefficients_id_seq FROM public; |
---|
165 | |
---|
166 | GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients TO pykotaadmin; |
---|
167 | GRANT SELECT, UPDATE ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq, payments_id_seq, coefficients_id_seq TO pykotaadmin; |
---|
168 | GRANT SELECT ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients TO pykotauser; |
---|