[1015] | 1 | -- |
---|
[3259] | 2 | -- PyKota - Print Quotas for CUPS |
---|
[1015] | 3 | -- |
---|
[3561] | 4 | -- (c) 2003-2013 Jerome Alet <alet@librelogiciel.com> |
---|
[3259] | 5 | -- This program is free software: you can redistribute it and/or modify |
---|
[1015] | 6 | -- it under the terms of the GNU General Public License as published by |
---|
[3259] | 7 | -- the Free Software Foundation, either version 3 of the License, or |
---|
[1015] | 8 | -- (at your option) any later version. |
---|
[3413] | 9 | -- |
---|
[1015] | 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. |
---|
[3413] | 14 | -- |
---|
[1015] | 15 | -- You should have received a copy of the GNU General Public License |
---|
[3259] | 16 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1015] | 17 | -- |
---|
| 18 | -- $Id$ |
---|
| 19 | -- |
---|
[2054] | 20 | -- |
---|
[1015] | 21 | |
---|
| 22 | -- |
---|
| 23 | -- PyKota Database creation script for PostgreSQL |
---|
| 24 | -- |
---|
| 25 | -- Launch this as PostgreSQL administrator with \i |
---|
| 26 | -- |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | -- |
---|
| 30 | -- Create the print quota database |
---|
| 31 | -- |
---|
[2954] | 32 | CREATE DATABASE pykota WITH ENCODING='UTF-8'; |
---|
[1015] | 33 | |
---|
| 34 | -- |
---|
| 35 | -- Create the print quota database users |
---|
[2817] | 36 | -- NOTE: Change the password values to the passwords you would like. |
---|
[3413] | 37 | -- |
---|
[2817] | 38 | CREATE USER pykotauser WITH UNENCRYPTED PASSWORD 'readonlypw' NOCREATEDB NOCREATEUSER; |
---|
| 39 | CREATE USER pykotaadmin WITH UNENCRYPTED PASSWORD 'readwritepw' NOCREATEDB NOCREATEUSER; |
---|
[1015] | 40 | |
---|
[3413] | 41 | -- |
---|
[1015] | 42 | -- Now connect to the new database |
---|
[3413] | 43 | -- |
---|
[1015] | 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, |
---|
[3413] | 51 | email TEXT, |
---|
[1015] | 52 | balance FLOAT DEFAULT 0.0, |
---|
| 53 | lifetimepaid FLOAT DEFAULT 0.0, |
---|
[1994] | 54 | limitby TEXT DEFAULT 'quota', |
---|
[2317] | 55 | description TEXT, |
---|
[2054] | 56 | overcharge FLOAT NOT NULL DEFAULT 1.0); |
---|
[3413] | 57 | |
---|
[1015] | 58 | -- |
---|
| 59 | -- Create the groups table |
---|
| 60 | -- |
---|
| 61 | CREATE TABLE groups(id SERIAL PRIMARY KEY NOT NULL, |
---|
| 62 | groupname TEXT UNIQUE NOT NULL, |
---|
[2317] | 63 | description TEXT, |
---|
[1015] | 64 | limitby TEXT DEFAULT 'quota'); |
---|
[3413] | 65 | |
---|
[1015] | 66 | -- |
---|
| 67 | -- Create the printers table |
---|
| 68 | -- |
---|
| 69 | CREATE TABLE printers(id SERIAL PRIMARY KEY NOT NULL, |
---|
| 70 | printername TEXT UNIQUE NOT NULL, |
---|
[1556] | 71 | description TEXT, |
---|
[1015] | 72 | priceperpage FLOAT DEFAULT 0.0, |
---|
[2317] | 73 | priceperjob FLOAT DEFAULT 0.0, |
---|
[2457] | 74 | passthrough BOOLEAN DEFAULT FALSE, |
---|
[2317] | 75 | maxjobsize INT4); |
---|
[3413] | 76 | |
---|
[1015] | 77 | -- |
---|
| 78 | -- Create the print quota table for users |
---|
| 79 | -- |
---|
| 80 | CREATE TABLE userpquota(id SERIAL PRIMARY KEY NOT NULL, |
---|
| 81 | userid INT4 REFERENCES users(id), |
---|
| 82 | printerid INT4 REFERENCES printers(id), |
---|
| 83 | lifepagecounter INT4 DEFAULT 0, |
---|
| 84 | pagecounter INT4 DEFAULT 0, |
---|
| 85 | softlimit INT4, |
---|
| 86 | hardlimit INT4, |
---|
[1994] | 87 | datelimit TIMESTAMP, |
---|
[2317] | 88 | maxjobsize INT4, |
---|
[3413] | 89 | warncount INT4 DEFAULT 0); |
---|
[2037] | 90 | CREATE INDEX userpquota_u_id_ix ON userpquota (userid); |
---|
| 91 | CREATE INDEX userpquota_p_id_ix ON userpquota (printerid); |
---|
[1200] | 92 | CREATE UNIQUE INDEX userpquota_up_id_ix ON userpquota (userid, printerid); |
---|
[3413] | 93 | |
---|
[1015] | 94 | -- |
---|
| 95 | -- Create the job history table |
---|
| 96 | -- |
---|
| 97 | CREATE TABLE jobhistory(id SERIAL PRIMARY KEY NOT NULL, |
---|
| 98 | jobid TEXT, |
---|
[1240] | 99 | userid INT4, |
---|
| 100 | printerid INT4, |
---|
[1015] | 101 | pagecounter INT4 DEFAULT 0, |
---|
[1520] | 102 | jobsizebytes INT8, |
---|
[1015] | 103 | jobsize INT4, |
---|
[1203] | 104 | jobprice FLOAT, |
---|
[1015] | 105 | action TEXT, |
---|
[1200] | 106 | filename TEXT, |
---|
| 107 | title TEXT, |
---|
| 108 | copies INT4, |
---|
| 109 | options TEXT, |
---|
[1520] | 110 | hostname TEXT, |
---|
[1994] | 111 | md5sum TEXT, |
---|
| 112 | pages TEXT, |
---|
[2024] | 113 | billingcode TEXT, |
---|
[2317] | 114 | precomputedjobsize INT4, |
---|
| 115 | precomputedjobprice FLOAT, |
---|
[1240] | 116 | jobdate TIMESTAMP DEFAULT now(), |
---|
| 117 | CONSTRAINT checkUserPQuota FOREIGN KEY (userid, printerid) REFERENCES userpquota(userid, printerid)); |
---|
[2037] | 118 | CREATE INDEX jobhistory_u_id_ix ON jobhistory (userid); |
---|
[1200] | 119 | CREATE INDEX jobhistory_p_id_ix ON jobhistory (printerid); |
---|
| 120 | CREATE INDEX jobhistory_pd_id_ix ON jobhistory (printerid, jobdate); |
---|
[1520] | 121 | CREATE INDEX jobhistory_hostname_ix ON jobhistory (hostname); |
---|
[3413] | 122 | |
---|
[1015] | 123 | -- |
---|
| 124 | -- Create the print quota table for groups |
---|
| 125 | -- |
---|
| 126 | CREATE TABLE grouppquota(id SERIAL PRIMARY KEY NOT NULL, |
---|
| 127 | groupid INT4 REFERENCES groups(id), |
---|
| 128 | printerid INT4 REFERENCES printers(id), |
---|
| 129 | softlimit INT4, |
---|
| 130 | hardlimit INT4, |
---|
| 131 | datelimit TIMESTAMP); |
---|
[2054] | 132 | CREATE INDEX grouppquota_g_id_ix ON grouppquota (groupid); |
---|
| 133 | CREATE INDEX grouppquota_p_id_ix ON grouppquota (printerid); |
---|
[1200] | 134 | CREATE UNIQUE INDEX grouppquota_up_id_ix ON grouppquota (groupid, printerid); |
---|
[3413] | 135 | |
---|
| 136 | -- |
---|
[1015] | 137 | -- Create the groups/members relationship |
---|
| 138 | -- |
---|
| 139 | CREATE TABLE groupsmembers(groupid INT4 REFERENCES groups(id), |
---|
| 140 | userid INT4 REFERENCES users(id), |
---|
| 141 | PRIMARY KEY (groupid, userid)); |
---|
[3413] | 142 | |
---|
| 143 | -- |
---|
[1240] | 144 | -- Create the printer groups relationship |
---|
| 145 | -- |
---|
| 146 | CREATE TABLE printergroupsmembers(groupid INT4 REFERENCES printers(id), |
---|
| 147 | printerid INT4 REFERENCES printers(id), |
---|
| 148 | PRIMARY KEY (groupid, printerid)); |
---|
[1520] | 149 | -- |
---|
| 150 | -- Create the table for payments |
---|
[3413] | 151 | -- |
---|
[1520] | 152 | CREATE TABLE payments (id SERIAL PRIMARY KEY NOT NULL, |
---|
| 153 | userid INT4 REFERENCES users(id), |
---|
| 154 | amount FLOAT, |
---|
[2452] | 155 | description TEXT, |
---|
[1520] | 156 | date TIMESTAMP DEFAULT now()); |
---|
| 157 | CREATE INDEX payments_date_ix ON payments (date); |
---|
[1015] | 158 | |
---|
[3413] | 159 | -- |
---|
[1994] | 160 | -- Create the table for coefficients wrt paper sizes and the like |
---|
| 161 | -- |
---|
[3413] | 162 | CREATE TABLE coefficients (id SERIAL PRIMARY KEY NOT NULL, |
---|
| 163 | printerid INTEGER NOT NULL REFERENCES printers(id), |
---|
| 164 | label TEXT NOT NULL, |
---|
| 165 | coefficient FLOAT DEFAULT 1.0, |
---|
[1994] | 166 | CONSTRAINT coeffconstraint UNIQUE (printerid, label)); |
---|
| 167 | |
---|
[3413] | 168 | -- |
---|
[2317] | 169 | -- Create the table for the billing codes |
---|
| 170 | -- |
---|
| 171 | CREATE TABLE billingcodes (id SERIAL PRIMARY KEY NOT NULL, |
---|
[2342] | 172 | billingcode TEXT UNIQUE NOT NULL, |
---|
[2318] | 173 | description TEXT, |
---|
[2317] | 174 | balance FLOAT DEFAULT 0.0, |
---|
| 175 | pagecounter INT4 DEFAULT 0); |
---|
| 176 | |
---|
[1015] | 177 | -- |
---|
[3413] | 178 | -- Set some ACLs |
---|
| 179 | -- |
---|
[2317] | 180 | REVOKE ALL ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes FROM public; |
---|
| 181 | 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, billingcodes_id_seq FROM public; |
---|
[1087] | 182 | |
---|
[2317] | 183 | GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes TO pykotaadmin; |
---|
| 184 | 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, billingcodes_id_seq TO pykotaadmin; |
---|
| 185 | GRANT SELECT ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes TO pykotauser; |
---|