Skip to content

Commit

Permalink
[#642] Simplify check for debug mode, only create _foo once
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Mar 28, 2013
1 parent b2f477f commit 1a9566b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ckanext/datastore/plugin.py
Expand Up @@ -55,7 +55,7 @@ def configure(self, config):
if not self._is_read_only_database():
# Make sure that the right permissions are set
# so that no harmful queries can be made
if not ('debug' in config and config['debug']):
if self.config.get('debug'):
if self._same_read_and_write_url():
raise DatastoreException("The write and read-only database "
"connection url are the same.")
Expand All @@ -66,7 +66,7 @@ def configure(self, config):
log.warn("Legacy mode active. "
"The sql search will not be available.")
elif not self._read_connection_has_correct_privileges():
if 'debug' in self.config and self.config['debug']:
if self.config.get('debug'):
log.critical("We have write permissions "
"on the read-only database.")
else:
Expand Down Expand Up @@ -154,22 +154,23 @@ def _read_connection_has_correct_privileges(self):
'''
write_connection = db._get_engine(None,
{'connection_url': self.write_url}).connect()
write_connection.execute(
u"DROP TABLE IF EXISTS public._foo;",
u"CREATE TABLE public._foo ()")

read_connection = db._get_engine(None,
{'connection_url': self.read_url}).connect()

drop_foo_sql = u"DROP TABLE IF EXISTS _foo"

write_connection.execute(drop_foo_sql)

try:
write_connection.execute(u"CREATE TABLE public._foo ()")
write_connection.execute(u"CREATE TABLE _foo ()")
for privilege in ['INSERT', 'UPDATE', 'DELETE']:
sql = u"SELECT has_table_privilege('_foo', '{privilege}')".format(privilege=privilege)
test_privilege_sql = u"SELECT has_table_privilege('_foo', '{privilege}')"
sql = test_privilege_sql.format(privilege=privilege)
have_privilege = read_connection.execute(sql).first()[0]
if have_privilege:
return False
finally:
write_connection.execute("DROP TABLE _foo")
write_connection.execute(drop_foo_sql)
return True

def _create_alias_table(self):
Expand Down

0 comments on commit 1a9566b

Please sign in to comment.