1
# Copyright (C) 2007 by the Free Software Foundation, Inc.
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18
from __future__ import with_statement
34
from storm import database
35
from storm.locals import create_database, Store
36
from string import Template
37
from urlparse import urlparse
39
import Mailman.Version
41
from Mailman import constants
42
from Mailman.Errors import SchemaVersionMismatchError
43
from Mailman.configuration import config
44
from Mailman.database.model.address import Address
45
from Mailman.database.model.language import Language
46
from Mailman.database.model.mailinglist import MailingList
47
from Mailman.database.model.member import Member
48
from Mailman.database.model.message import Message
49
from Mailman.database.model.pending import Pendings
50
from Mailman.database.model.preferences import Preferences
51
from Mailman.database.model.requests import Requests
52
from Mailman.database.model.user import User
53
from Mailman.database.model.version import Version
57
def initialize(debug):
58
# Calculate the engine url.
59
url = Template(config.DEFAULT_DATABASE_URL).safe_substitute(config.paths)
60
# XXX By design of SQLite, database file creation does not honor
61
# umask. See their ticket #1193:
62
# http://www.sqlite.org/cvstrac/tktview?tn=1193,31
64
# This sucks for us because the mailman.db file /must/ be group writable,
65
# however even though we guarantee our umask is 002 here, it still gets
66
# created without the necessary g+w permission, due to SQLite's policy.
67
# This should only affect SQLite engines because its the only one that
68
# creates a little file on the local file system. This kludges around
69
# their bug by "touch"ing the database file before SQLite has any chance
70
# to create it, thus honoring the umask and ensuring the right
71
# permissions. We only try to do this for SQLite engines, and yes, we
72
# could have chmod'd the file after the fact, but half dozen and all...
74
database = create_database(url)
75
store = Store(database)
76
database.DEBUG = (config.DEFAULT_DATABASE_ECHO if debug is None else debug)
77
# XXX Storm does not currently have schema creation. This is not an ideal
78
# way to handle creating the database, but it's cheap and easy for now.
79
import Mailman.database.model
80
schema_file = os.path.join(
81
os.path.dirname(Mailman.database.model.__file__),
83
with open(schema_file) as fp:
85
for statement in sql.split(';'):
86
store.execute(statement + ';')
87
# Validate schema version.
88
v = store.find(Version, component=u'schema').one()
90
# Database has not yet been initialized
91
v = Version(component=u'schema',
92
version=Mailman.Version.DATABASE_SCHEMA_VERSION)
94
elif v.version <> Mailman.Version.DATABASE_SCHEMA_VERSION:
96
raise SchemaVersionMismatchError(v.version)
101
parts = urlparse(url)
102
if parts.scheme <> 'sqlite':
104
path = os.path.normpath(parts.path)
105
fd = os.open(path, os.O_WRONLY | os.O_NONBLOCK | os.O_CREAT, 0666)