3 ### Database maintenance
5 ### (c) 2013 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Chopwood: a password-changing service.
12 ### Chopwood is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU Affero General Public License as
14 ### published by the Free Software Foundation; either version 3 of the
15 ### License, or (at your option) any later version.
17 ### Chopwood is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU Affero General Public License for more details.
22 ### You should have received a copy of the GNU Affero General Public
23 ### License along with Chopwood; if not, see
24 ### <http://www.gnu.org/licenses/>.
26 from __future__
import with_statement
28 import config
as CONF
; CFG
= CONF
.CFG
29 import subcommand
as SC
32 ###--------------------------------------------------------------------------
33 ### Opening the database.
35 ## Current database schema version.
39 """Open connections to the master database, updating it if necessary."""
44 dbmod
, dbargs
= CFG
.DB
45 DB
= U
.SimpleDBConnection(dbmod
, dbargs
)
47 ## Find out the current version.
49 DB
.execute("SELECT version FROM meta")
55 raise ExpectedError
, (500, "Database schema version not understood.")
57 ## In future, there will be an attempt to upgrade databases with old
58 ## schemata to the current version. But not yet.
61 ###--------------------------------------------------------------------------
64 @SC.subcommand('setup', ['admin'], 'Initialize the master database.')
68 user VARCHAR(32) PRIMARY KEY NOT NULL,
69 passwd TEXT NOT NULL DEFAULT('*'),
71 CREATE TABLE services (
72 user VARCHAR(32) NOT NULL,
73 service VARCHAR(32) NOT NULL,
75 PRIMARY KEY (user, service),
76 FOREIGN KEY (user) REFERENCES users(user)
80 version INTEGER NOT NULL);
81 CREATE TABLE secrets (
82 stamp INTEGER PRIMARY KEY NOT NULL,
83 secret TEXT NOT NULL);
86 for cmd
in script
.split(';'):
89 DB
.execute("INSERT INTO meta(version) VALUES ($version)",
92 ###----- That's all, folks --------------------------------------------------