commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r8225 - in trunk/gnue-common/src/datasources: . drivers/sql/postg


From: johannes
Subject: [gnue] r8225 - in trunk/gnue-common/src/datasources: . drivers/sql/postgresql
Date: Mon, 13 Mar 2006 09:57:36 -0600 (CST)

Author: johannes
Date: 2006-03-13 09:57:35 -0600 (Mon, 13 Mar 2006)
New Revision: 8225

Modified:
   trunk/gnue-common/src/datasources/GConnections.py
   trunk/gnue-common/src/datasources/drivers/sql/postgresql/Behavior.py
   trunk/gnue-common/src/datasources/readgsd.py
Log:
Added a --owner/password option to gnue-schema. The driver for 
PostgreSQL honors this new option for database-creation.


Modified: trunk/gnue-common/src/datasources/GConnections.py
===================================================================
--- trunk/gnue-common/src/datasources/GConnections.py   2006-03-13 01:38:31 UTC 
(rev 8224)
+++ trunk/gnue-common/src/datasources/GConnections.py   2006-03-13 15:57:35 UTC 
(rev 8225)
@@ -310,7 +310,6 @@
     if self.__openConnections.has_key (connection_name):
       conn = self.__openConnections [connection_name]
     else:
-
       # Support for multiple open connections to the same database.
       # Specify as 'gnue:1', 'gnue:2', etc, to open two actual connections to
       # 'gnue', each with their own transactions, etc.
@@ -506,6 +505,9 @@
       except KeyError:
         self.__authenticatedUsers [connection] = None
 
+      # Ok, since everything worked fine, add the connection to the dictionary
+      # of open connections
+      self.__openConnections [connection_name] = connection
 
       if self._eventHandler:
         self._eventHandler.dispatchEvent ('Connections:Connect',
@@ -558,3 +560,5 @@
     for (key, value) in self.__openConnections.items ():
       if value == connection:
         del self.__openConnections [key]
+
+    connection.__connected = False

Modified: trunk/gnue-common/src/datasources/drivers/sql/postgresql/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/postgresql/Behavior.py        
2006-03-13 01:38:31 UTC (rev 8224)
+++ trunk/gnue-common/src/datasources/drivers/sql/postgresql/Behavior.py        
2006-03-13 15:57:35 UTC (rev 8225)
@@ -94,6 +94,8 @@
     password = self.__connection.parameters.get ('password')
     host     = self.__connection.parameters.get ('host')
     port     = self.__connection.parameters.get ('port')
+    owner    = self.__connection.parameters.get ('owner', username)
+    ownerpwd = self.__connection.parameters.get ('ownerpwd')
 
     site = ""
     if host is not None:
@@ -101,35 +103,41 @@
     if port is not None:
       site += " --port=%s" % port
 
-    # TODO: use a connection object to the template1 database instead of the
-    # shell-scripts. Note: CREATE DATABASE statements must NOT run within a
-    # transaction block, so we cannot use the default connection mechanisms.
+    # First, let's connect to template1 using the given username and password
+    self.__connection.parameters ['dbname'] = 'template1'
+    self.__connection.manager.loginToConnection (self.__connection)
 
-    try:
-      os.system (u"dropuser %s%s 2>/dev/null" % (username, site))
+    # Then have a look wether the requested owner is already available
+    result = self.__connection.sql ('SELECT usesysid FROM pg_user ' \
+                               'WHERE usename = %(owner)s', {'owner': owner})
+    if not result:
+      cmd = 'CREATE USER %s' % owner
+      if ownerpwd:
+        cmd += " WITH PASSWORD '%s'" % ownerpwd
 
-    except:
-      pass
+      self.__connection.sql0 (cmd)
+      self.__connection.commit ()
 
-    try:
-      createuser = u"createuser %s --createdb --adduser %s" % (site, username)
-      os.system (createuser)
-    except:
-      pass
+    # Now go and create that new database
+    cmd = "ABORT; CREATE DATABASE %s WITH OWNER %s ENCODING = 'UNICODE'; 
BEGIN" 
+    self.__connection.sql0 (cmd % (dbname, owner))
+    self.__connection.commit ()
 
-    createdb = u"createdb %s --owner=%s --encoding=UNICODE %s" \
-        % (site, username, dbname)
+    self.__connection.close ()
 
-    if os.system (createdb):
-      raise errors.ApplicationError, u_("Database creation failed")
+    # Since the newly created database should be available now, connect to it
+    # using the given owner
+    self.__connection.parameters ['dbname'] = dbname
+    self.__connection.parameters ['username'] = owner
 
+    if ownerpwd:
+      self.__connection.parameters ['password'] = ownerpwd
+    else:
+      if 'password' in self.__connection.parameters:
+        del self.__connection.parameters ['password']
 
     self.__connection.manager.loginToConnection (self.__connection)
 
-    if password is not None and password:
-      alterUser = u"ALTER USER %s WITH PASSWORD '%s';" % (username, password)
-      self.__connection.makecursor (alterUser)
-      self.__connection.commit ()
 
 
   # ---------------------------------------------------------------------------

Modified: trunk/gnue-common/src/datasources/readgsd.py
===================================================================
--- trunk/gnue-common/src/datasources/readgsd.py        2006-03-13 01:38:31 UTC 
(rev 8224)
+++ trunk/gnue-common/src/datasources/readgsd.py        2006-03-13 15:57:35 UTC 
(rev 8225)
@@ -150,15 +150,24 @@
                  "is done."))
 
     self.addCommandOption ('username', 'u', argument="user",
-        help = _("Set the username for the database. If the database is to be "
-                 "created, this username will be it's owner."))
+        help = _("Set the username to connect to the database. If the "
+                 "database is to be created and no owner (--owner) is "
+                 "specified, this username will be it's owner."))
 
     self.addCommandOption ('password', 'p', argument="password",
-        help = _("Set the password for the database."))
+        help = _("Set the password to connect to the database."))
 
+    self.addCommandOption ('owner', 'O', argument="owner",
+        help = _("If the database is to be created this will be its owner."))
+
+    self.addCommandOption ('ownerpassword', 'P', argument="ownerpwd",
+        help = _("If the database is to be created this will be the password "
+                 "used for the database owner."))
+
     self.addCommandOption ('createdb', 'd', default = False,
         help = _("If this option is set, the database will be created before "
-                 "any schema creation is done. There must be a username "
+                 "any schema creation is done. There must be an owner or a "
+                 "username "
                  "either from the given connection-configuration or from the "
                  "command line. This user becomes the owner of the database "
                  "and will be implicitly created."))
@@ -255,6 +264,15 @@
 
     self.connection.parameters ['username'] = username
     self.connection.parameters ['password'] = password
+
+    owner = self.OPTIONS.get ('owner', self.connection.parameters.get 
('owner'))
+    if not owner:
+      owner = username
+
+    self.connection.parameters ['owner'] = owner
+    if self.OPTIONS.get ('ownerpassword'):
+      self.connection.parameters ['ownerpwd'] = self.OPTIONS ['ownerpassword']
+
     
 
   # ---------------------------------------------------------------------------





reply via email to

[Prev in Thread] Current Thread [Next in Thread]