- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/initscripts/postgresql/upgrade-from-before-1.03.py
r3259 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 26 26 try : 27 27 import pg 28 except ImportError : 28 except ImportError : 29 29 sys.stderr.write("The PyGreSQL Python module doesn't seem to be available. ABORTED.\n") 30 30 sys.exit(-1) … … 32 32 def dump_old_database() : 33 33 """Dumps the existing PyKota database to a file, to avoir loosing data. 34 34 35 35 Returns 1 if dump is successfull, 0 if it isn't. 36 """ 36 """ 37 37 pipeinput = os.popen("pg_dump -C -D -N -U postgres -f pykota-dump.sql pykota") 38 38 dummy = pipeinput.read() … … 44 44 dummy = pipeinput.read() 45 45 return (pipeinput.close() is None) or 0 46 46 47 47 def restore_original_database() : 48 48 """Creates the empty database.""" … … 50 50 dummy = pipeinput.read() 51 51 return (pipeinput.close() is None) or 0 52 52 53 53 def open_database(dbname="pykota") : 54 54 """Returns the database object or None if we can't connect to it.""" 55 55 try : 56 56 pykotadb = pg.connect(host="localhost", port=5432, dbname=dbname, user="postgres") 57 except pg.error, msg : 57 except pg.error, msg : 58 58 sys.stderr.write("%s\n" % msg) 59 59 sys.stderr.write("Unable to connect to the local PostgreSQL server.\nPlease modify the open_database() method in %s\nto connect to the correct PostgreSQL server\nand relaunch the script.\n" % sys.argv[0]) 60 return 61 else : 60 return 61 else : 62 62 return pykotadb 63 64 63 64 65 65 def doQuote(field) : 66 66 """Quotes a field for use as a string in SQL queries.""" 67 67 if type(field) == type(0) : # TODO : do something safer 68 68 typ = "decimal" 69 else : 69 else : 70 70 typ = "text" 71 71 return pg._quote(field, typ) 72 72 73 73 def main() : 74 74 """Does the work.""" 75 75 76 76 # First we make a dump of the old database 77 77 print "Dumping old database for safety...", … … 80 80 return -1 81 81 print "Done." 82 83 # Second we try to connect to it 84 print "Extracting datas from old database...", 82 83 # Second we try to connect to it 84 print "Extracting datas from old database...", 85 85 db = open_database() 86 86 if db is None : 87 87 sys.stderr.write("Impossible to connect to old PyKota database. ABORTED.\nAre you sure you are upgrading an existing installation ?\n") 88 88 return -1 89 89 90 90 # Third we extract datas 91 91 oldprinters = db.query("SELECT * FROM printers ORDER BY id;") 92 92 oldusers = db.query("SELECT * FROM users ORDER BY id;") 93 93 oldquotas = db.query("SELECT * FROM userpquota ORDER BY printerid, userid;") 94 94 95 95 # Fourth close the database 96 96 db.close() 97 97 print "Done." 98 98 99 99 # Fifth we delete the old database ! 100 100 answer = raw_input("The old database will be deleted for the upgrade to take place.\nAre you sure you want to continue (y/N) ? ") … … 112 112 db.close() 113 113 return -1 114 else : 114 else : 115 115 db.close() 116 116 117 117 # Sixth we create the new database 118 118 print "Creating the new database...", … … 121 121 return -1 122 122 print "Done." 123 123 124 124 # Seventh we restore old data 125 125 print "Restoring old datas..." … … 131 131 sys.stderr.write("Shit ! A double-error occured !!!\nPlease report problem to your database administrator.\n") 132 132 sys.stderr.write("And file a bug report to alet@librelogiciel.com\n") 133 else : 133 else : 134 134 print "Done." 135 135 return -1 136 db.query("BEGIN;") 136 db.query("BEGIN;") 137 137 try : 138 138 newprinters = {} … … 141 141 newid = db.query("SELECT id FROM printers WHERE printername='%s';" % oldprinter["printername"]).dictresult()[0]["id"] 142 142 newprinters[oldprinter["id"]] = newid 143 newusers = {} 144 for olduser in oldusers.dictresult() : 143 newusers = {} 144 for olduser in oldusers.dictresult() : 145 145 db.query("INSERT INTO users (username) VALUES (%s);" % doQuote(olduser["username"])) 146 146 newid = db.query("SELECT id FROM users WHERE username='%s';" % olduser["username"]).dictresult()[0]["id"] 147 147 newusers[olduser["id"]] = newid 148 for oldquota in oldquotas.dictresult() : 149 db.query("INSERT INTO userpquota (userid, printerid, pagecounter, lifepagecounter, softlimit, hardlimit, datelimit) VALUES (%s, %s, %s, %s, %s, %s, %s);" % 148 for oldquota in oldquotas.dictresult() : 149 db.query("INSERT INTO userpquota (userid, printerid, pagecounter, lifepagecounter, softlimit, hardlimit, datelimit) VALUES (%s, %s, %s, %s, %s, %s, %s);" % 150 150 (doQuote(newusers[oldquota["userid"]]) , doQuote(newprinters[oldquota["printerid"]]), doQuote(oldquota["pagecounter"]), doQuote(oldquota["lifepagecounter"]), doQuote(oldquota["softlimit"]), doQuote(oldquota["hardlimit"]), doQuote(oldquota["datelimit"]))) 151 except pg.error, msg : 151 except pg.error, msg : 152 152 sys.stderr.write("ERROR : %s\nABORTED.\n" % msg) 153 153 db.query("ROLLBACK;") 154 db.close() 154 db.close() 155 155 return -1 156 except : 156 except : 157 157 sys.stderr.write("Unknown error ! ABORTED.\n") 158 158 db.query("ROLLBACK;") 159 db.close() 159 db.close() 160 160 return -1 161 else : 161 else : 162 162 db.query("COMMIT;") 163 163 db.close() … … 165 165 print "NB : Last job on each printer was lost. This is normal !" 166 166 return 0 167 167 168 168 if __name__ == "__main__" : 169 169 sys.exit(main())