root / pykota / trunk / initscripts / mysql / pykota-mysql.sql @ 2817

Revision 2817, 7.7 kB (checked in by jerome, 18 years ago)

Improved documentation for PostgreSQL and MySQL backends setup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1--
2-- PyKota - Print Quotas for CUPS and LPRng
3--
4-- (c) 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18--
19-- $Id$
20--
21--
22
23--
24-- PyKota Database creation script for MySQL
25--
26-- Launch this as MySQL administrator with \.
27--
28
29
30--
31-- Create the print quota database
32--
33CREATE DATABASE pykota;
34
35--
36-- Create the print quota database users
37-- NOTE: Change the "IDENTIFIED BY" strings to the passwords you would like.
38--
39GRANT USAGE ON *.* TO 'pykotauser'@'localhost' IDENTIFIED BY 'readonlypw';
40GRANT USAGE ON *.* TO 'pykotaadmin'@'localhost' IDENTIFIED BY 'readwritepw';
41
42--
43-- Now connect to the new database
44--
45USE pykota;
46
47--
48-- Create the users table
49--
50CREATE TABLE users (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
51                   username VARCHAR(255) UNIQUE NOT NULL,
52                   email TEXT, 
53                   balance FLOAT DEFAULT 0.0,
54                   lifetimepaid FLOAT DEFAULT 0.0,
55                   limitby VARCHAR(30) DEFAULT 'quota',
56                   description TEXT,
57                   overcharge FLOAT NOT NULL DEFAULT 1.0) TYPE=INNODB;
58                   
59--
60-- Create the groups table
61--
62CREATE TABLE groups (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
63                    groupname VARCHAR(255) UNIQUE NOT NULL,
64                    description TEXT,
65                    limitby VARCHAR(30) DEFAULT 'quota') TYPE=INNODB;
66                   
67--
68-- Create the printers table
69--
70CREATE TABLE printers (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
71                      printername VARCHAR(255) UNIQUE NOT NULL,
72                      description TEXT,
73                      priceperpage FLOAT DEFAULT 0.0,
74                      priceperjob FLOAT DEFAULT 0.0,
75                      passthrough ENUM('t','f') DEFAULT 'f',
76                      maxjobsize INT4) TYPE=INNODB;
77                   
78--
79-- Create the print quota table for users
80--
81CREATE TABLE userpquota (id INT8 PRIMARY KEY NOT NULL AUTO_INCREMENT,
82                        userid INT4, 
83                        printerid INT4, 
84                        lifepagecounter INT4 DEFAULT 0,
85                        pagecounter INT4 DEFAULT 0,
86                        softlimit INT4,
87                        hardlimit INT4,
88                        datelimit TIMESTAMP,
89                        maxjobsize INT4,
90                        warncount INT4 DEFAULT 0, 
91                        FOREIGN KEY (userid) REFERENCES users(id),
92                        FOREIGN KEY (printerid) REFERENCES printers(id)) 
93                        TYPE=INNODB;
94CREATE INDEX userpquota_u_id_ix ON userpquota (userid);
95CREATE INDEX userpquota_p_id_ix ON userpquota (printerid);
96CREATE UNIQUE INDEX userpquota_up_id_ix ON userpquota (userid, printerid);
97                       
98--
99-- Create the job history table
100--
101CREATE TABLE jobhistory(id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
102                        jobid TEXT,
103                        userid INT4,
104                        printerid INT4,
105                        pagecounter INT4 DEFAULT 0,
106                        jobsizebytes INT8,
107                        jobsize INT4,
108                        jobprice FLOAT,
109                        action TEXT,
110                        filename TEXT,
111                        title TEXT,
112                        copies INT4,
113                        options TEXT,
114                        hostname VARCHAR(255),
115                        md5sum TEXT,
116                        pages TEXT,
117                        billingcode TEXT,
118                        precomputedjobsize INT4,
119                        precomputedjobprice FLOAT,
120                        jobdate TIMESTAMP DEFAULT now(),
121                        CONSTRAINT checkUserPQuota FOREIGN KEY (userid, printerid) REFERENCES userpquota (userid, printerid)
122                        ) TYPE=INNODB;
123CREATE INDEX jobhistory_u_id_ix ON jobhistory (userid);
124CREATE INDEX jobhistory_p_id_ix ON jobhistory (printerid);
125CREATE INDEX jobhistory_pd_id_ix ON jobhistory (printerid, jobdate);
126CREATE INDEX jobhistory_hostname_ix ON jobhistory (hostname);
127                       
128--
129-- Create the print quota table for groups
130--
131CREATE TABLE grouppquota(id INT8 PRIMARY KEY NOT NULL AUTO_INCREMENT,
132                         groupid INT4, 
133                         printerid INT4,
134                         softlimit INT4,
135                         hardlimit INT4,
136                         maxjobsize INT4,
137                         datelimit TIMESTAMP,
138                         FOREIGN KEY (groupid) REFERENCES groups(id),
139                         FOREIGN KEY (printerid) REFERENCES printers(id))
140                         TYPE=INNODB;
141CREATE INDEX grouppquota_g_id_ix ON grouppquota (groupid);
142CREATE INDEX grouppquota_p_id_ix ON grouppquota (printerid);
143CREATE UNIQUE INDEX grouppquota_up_id_ix ON grouppquota (groupid, printerid);
144                       
145--                         
146-- Create the groups/members relationship
147--
148CREATE TABLE groupsmembers(groupid INT4,
149                           userid INT4,
150                           FOREIGN KEY (groupid) REFERENCES groups(id),
151                           FOREIGN KEY (userid) REFERENCES users(id),
152                           PRIMARY KEY (groupid, userid)) TYPE=INNODB;
153                           
154--                         
155-- Create the printer groups relationship
156--
157CREATE TABLE printergroupsmembers(groupid INT4,
158                           printerid INT4,
159                           FOREIGN KEY (groupid) REFERENCES groups(id),
160                           FOREIGN KEY (printerid) REFERENCES printers(id),
161                           PRIMARY KEY (groupid, printerid)) TYPE=INNODB;
162--
163-- Create the table for payments
164--
165CREATE TABLE payments (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
166                       userid INT4,
167                       amount FLOAT,
168                       description TEXT,
169                       date TIMESTAMP DEFAULT now(),
170                       FOREIGN KEY (userid) REFERENCES users(id)) TYPE=INNODB;
171CREATE INDEX payments_date_ix ON payments (date);
172
173--
174-- Create the table for coefficients wrt paper sizes and the like
175--
176CREATE TABLE coefficients (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
177                           printerid INT4 NOT NULL,
178                           label VARCHAR(255) NOT NULL,
179                           coefficient FLOAT DEFAULT 1.0,
180                           FOREIGN KEY (printerid) REFERENCES printers(id),
181                           CONSTRAINT coeffconstraint UNIQUE (printerid, label)
182                           ) TYPE=INNODB;
183
184--
185-- Create the table for the billing codes
186--
187CREATE TABLE billingcodes (id INT4 PRIMARY KEY NOT NULL AUTO_INCREMENT,
188                           billingcode VARCHAR(255) UNIQUE NOT NULL,
189                           description TEXT,
190                           balance FLOAT DEFAULT 0.0,
191                           pagecounter INT4 DEFAULT 0) TYPE=INNODB;
192--                       
193-- Set some ACLs                       
194--
195GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES ON `pykota`.* TO 'pykotaadmin'@'localhost';
196GRANT SELECT ON `pykota`.* TO 'pykotauser'@'localhost';
Note: See TracBrowser for help on using the browser.