Mercurial > p > roundup > code
comparison roundup/backends/back_postgresql.py @ 2634:f47ca4541770
Both RDBMS backends now use the same config.ini section, [rdbms].
Comments on each option clearly state that the options only apply to
postgresql or mysql.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 27 Jul 2004 01:18:25 +0000 |
| parents | a9e1fff1e793 |
| children | 921780161fb9 |
comparison
equal
deleted
inserted
replaced
| 2633:a9e1fff1e793 | 2634:f47ca4541770 |
|---|---|
| 12 import psycopg | 12 import psycopg |
| 13 | 13 |
| 14 from roundup import hyperdb, date | 14 from roundup import hyperdb, date |
| 15 from roundup.backends import rdbms_common | 15 from roundup.backends import rdbms_common |
| 16 | 16 |
| 17 from roundup import configuration | |
| 18 | |
| 19 configuration.SETTINGS += ( | |
| 20 ("postgresql", ( | |
| 21 (configuration.Option, 'database', 'roundup'), | |
| 22 (configuration.NullableOption, 'host', 'localhost'), | |
| 23 (configuration.NullableOption, 'port', '5432'), | |
| 24 (configuration.NullableOption, 'user', 'roundup'), | |
| 25 (configuration.NullableOption, 'password', 'roundup'), | |
| 26 )), | |
| 27 ) | |
| 28 | |
| 29 def connection_dict(config): | |
| 30 d = { | |
| 31 'database': config.POSTGRESQL_DATABASE, | |
| 32 } | |
| 33 for name in 'host', 'port', 'password', 'user': | |
| 34 cvar = 'POSTGRESQL_'+name.upper() | |
| 35 if config[cvar] is not None: | |
| 36 d[name] = config[cvar] | |
| 37 return d | |
| 38 | |
| 39 def db_create(config): | 17 def db_create(config): |
| 40 """Clear all database contents and drop database itself""" | 18 """Clear all database contents and drop database itself""" |
| 41 command = 'CREATE DATABASE %s'%config.POSTGRESQL_DATABASE | 19 command = 'CREATE DATABASE %s'%config.RDBMS_NAME |
| 42 config.logging.getLogger('hyperdb').info(command) | 20 config.logging.getLogger('hyperdb').info(command) |
| 43 db_command(config, command) | 21 db_command(config, command) |
| 44 | 22 |
| 45 def db_nuke(config, fail_ok=0): | 23 def db_nuke(config, fail_ok=0): |
| 46 """Clear all database contents and drop database itself""" | 24 """Clear all database contents and drop database itself""" |
| 47 command = 'DROP DATABASE %s'% config.POSTGRESQL_DATABASE | 25 command = 'DROP DATABASE %s'% config.RDBMS_NAME |
| 48 config.logging.getLogger('hyperdb').info(command) | 26 config.logging.getLogger('hyperdb').info(command) |
| 49 db_command(config, command) | 27 db_command(config, command) |
| 50 | 28 |
| 51 if os.path.exists(config.DATABASE): | 29 if os.path.exists(config.DATABASE): |
| 52 shutil.rmtree(config.DATABASE) | 30 shutil.rmtree(config.DATABASE) |
| 53 | 31 |
| 54 def db_command(config, command): | 32 def db_command(config, command): |
| 55 '''Perform some sort of database-level command. Retry 10 times if we | 33 '''Perform some sort of database-level command. Retry 10 times if we |
| 56 fail by conflicting with another user. | 34 fail by conflicting with another user. |
| 57 ''' | 35 ''' |
| 58 template1 = connection_dict(config) | 36 template1 = rdbms_common.connection_dict(config) |
| 59 template1['database'] = 'template1' | 37 template1['database'] = 'template1' |
| 60 | 38 |
| 61 try: | 39 try: |
| 62 conn = psycopg.connect(**template1) | 40 conn = psycopg.connect(**template1) |
| 63 except psycopg.OperationalError, message: | 41 except psycopg.OperationalError, message: |
| 90 return 0 | 68 return 0 |
| 91 return 1 | 69 return 1 |
| 92 | 70 |
| 93 def db_exists(config): | 71 def db_exists(config): |
| 94 """Check if database already exists""" | 72 """Check if database already exists""" |
| 95 db = connection_dict(config) | 73 db = rdbms_common.connection_dict(config, 'database') |
| 96 try: | 74 try: |
| 97 conn = psycopg.connect(**db) | 75 conn = psycopg.connect(**db) |
| 98 conn.close() | 76 conn.close() |
| 99 return 1 | 77 return 1 |
| 100 except: | 78 except: |
| 102 | 80 |
| 103 class Database(rdbms_common.Database): | 81 class Database(rdbms_common.Database): |
| 104 arg = '%s' | 82 arg = '%s' |
| 105 | 83 |
| 106 def sql_open_connection(self): | 84 def sql_open_connection(self): |
| 107 db = connection_dict(config) | 85 db = rdbms_common.connection_dict(config, 'database') |
| 108 self.config.logging.getLogger('hyperdb').info('open database %r'%db) | 86 self.config.logging.getLogger('hyperdb').info('open database %r'%( |
| 87 db['database'],)) | |
| 109 try: | 88 try: |
| 110 conn = psycopg.connect(**db) | 89 conn = psycopg.connect(**db) |
| 111 except psycopg.OperationalError, message: | 90 except psycopg.OperationalError, message: |
| 112 raise hyperdb.DatabaseError, message | 91 raise hyperdb.DatabaseError, message |
| 113 | 92 |
