本文整理汇总了Python中psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT属性的典型用法代码示例。如果您正苦于以下问题:Python extensions.ISOLATION_LEVEL_AUTOCOMMIT属性的具体用法?Python extensions.ISOLATION_LEVEL_AUTOCOMMIT怎么用?Python extensions.ISOLATION_LEVEL_AUTOCOMMIT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.ISOLATION_LEVEL_AUTOCOMMIT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Starting to drop DB..'))
dbname = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']
self.stdout.write(self.style.SUCCESS('Connecting to host..'))
con = connect(dbname='postgres', user=user, host=host, password=password)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
self.stdout.write(self.style.SUCCESS("Dropping database '{}'".format(dbname)))
cur = con.cursor()
cur.execute('DROP DATABASE ' + dbname)
cur.close()
con.close()
self.stdout.write(self.style.SUCCESS('All done!'))
示例2: handle
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Starting DB creation..'))
dbname = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']
self.stdout.write(self.style.SUCCESS('Connecting to host..'))
con = connect(dbname='postgres', user=user, host=host, password=password)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
self.stdout.write(self.style.SUCCESS('Creating database'))
cur = con.cursor()
cur.execute('CREATE DATABASE ' + dbname)
cur.close()
con.close()
self.stdout.write(self.style.SUCCESS('All done!'))
示例3: handle
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Starting schema deletion...'))
dbname = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']
con = connect(dbname=dbname, user=user, host=host, password=password)
self.stdout.write(self.style.SUCCESS('Removing schema {schema} from database {dbname}'
.format(schema=settings.SCHEMA, dbname=dbname)))
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
cur.execute('DROP SCHEMA {schema} CASCADE;'.format(schema=settings.SCHEMA))
cur.close()
con.close()
self.stdout.write(self.style.SUCCESS('All done.'))
示例4: handle
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Starting Schema creation..'))
dbname = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']
con = connect(dbname=dbname, user=user, host=host, password=password)
self.stdout.write(self.style.SUCCESS('Adding schema {schema} to database {dbname}'
.format(schema=settings.SCHEMA, dbname=dbname)))
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
cur.execute('CREATE SCHEMA {schema};'.format(schema=settings.SCHEMA))
cur.close()
con.close()
self.stdout.write(self.style.SUCCESS('All Done!'))
示例5: random_database
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def random_database():
"""
Creates a random database in the testing Postgres instance and returns the
name of the database.
"""
# Setup connection with default credentials for testing.
with connect(dbname='holo', user='holocleanuser', password='abcd1234', host='localhost') as conn:
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cur:
while True:
# Generate a random DB name that is not already in Postgres.
db_name = 'test_holo_{}'.format(random.randint(0, 1e6))
cur.execute("""
SELECT EXISTS(
SELECT datname FROM pg_catalog.pg_database
WHERE datname = '{db_name}'
);
""".format(db_name=db_name))
if cur.fetchall()[0][0]:
continue
cur.execute("CREATE DATABASE {db_name}".format(db_name=db_name))
return db_name
示例6: connectToPostgres
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def connectToPostgres():
"""
If globusmonitor database does not exist yet:
$ initdb /home/globusmonitor/postgres/data
$ pg_ctl -D /home/globusmonitor/postgres/data -l /home/globusmonitor/postgres/log
$ createdb globusmonitor
"""
try:
conn = psycopg2.connect(dbname='globusmonitor')
except:
# Attempt to create database if not found
conn = psycopg2.connect(dbname='postgres')
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute('CREATE DATABASE globusmonitor;')
curs.close()
conn.commit()
conn.close()
conn = psycopg2.connect(dbname='globusmonitor')
initializeDatabase(conn)
return conn
示例7: __cleanup_schema
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def __cleanup_schema(self):
with psycopg2.connect(os.environ['GREASE_TEST_DSN_ORIGINAL']) as conn:
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cursor:
cursor.execute("""
SELECT
pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname='test_data'
""")
try:
cursor.execute("""
DROP DATABASE test_data;
""")
except psycopg2.ProgrammingError as e:
print("Schema Does Not Exist: {0}".format(e.pgerror))
os.environ['GREASE_TEST_DSN'] = os.environ['GREASE_TEST_DSN_ORIGINAL']
示例8: notify
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def notify(self, name, sec=0, payload=None):
"""Send a notification to the database, eventually after some time."""
if payload is None:
payload = ''
else:
payload = ", %r" % payload
script = ("""\
import time
time.sleep(%(sec)s)
import %(module)s as psycopg2
import %(module)s.extensions as ext
conn = psycopg2.connect(%(dsn)r)
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
print conn.get_backend_pid()
curs = conn.cursor()
curs.execute("NOTIFY " %(name)r %(payload)r)
curs.close()
conn.close()
""" % {
'module': psycopg2.__name__,
'dsn': dsn, 'sec': sec, 'name': name, 'payload': payload})
return Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE)
示例9: runAutoCommit
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def runAutoCommit(self, func, *args, **kwargs):
"""Call the given function in auto-commit mode. Needed to execute
statements that cannot be run in a transaction, like CREATE
DATABASE.
WARNING: This will commit any open transaction!
"""
old_level = self.dbh.isolation_level
try:
if self.inTransaction():
self.dbh.commit()
self.dbh.set_isolation_level(psy_ext.ISOLATION_LEVEL_AUTOCOMMIT)
return func(*args, **kwargs)
finally:
self.dbh.set_isolation_level(old_level)
# resetting the auto increment values of primary keys
示例10: create_init_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def create_init_db():
"""
create the initial role to own the db
create the new db as the db super user assign owner to new role
:return: new db with new owner
"""
con = connect(dbname='postgres', user=db_superuser, host=dbhost, port=dbport, password=db_superuser_pwd)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
cur.execute('CREATE ROLE \"{0}\" LOGIN ENCRYPTED PASSWORD \'{1}\''.format(dbowner_name, dbowner_pwd))
print("creating role")
cur.execute('''CREATE DATABASE \"{0}\" WITH OWNER = \'{1}\'
ENCODING = \'UTF8\' TEMPLATE=template0 CONNECTION LIMIT = -1;'''.format(db_name, dbowner_name))
print("creating database")
cur.close()
con.close()
示例11: create_db_if_needed
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def create_db_if_needed(cls, db):
if cls.SCHEMA == 'postgresql':
con = psycopg2.connect(user='postgres', host='localhost', password='postgres')
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
try:
cur.execute(sql.SQL("CREATE DATABASE {}").format(
sql.Identifier(db))
)
except Exception:
pass
finally:
cur.close()
con.close()
elif cls.SCHEMA == 'mysql':
con = mysql.connector.connect(user='root', host='localhost')
cur = con.cursor()
cur.execute('CREATE DATABASE IF NOT EXISTS {}'.format(db))
con.commit()
cur.close()
con.close()
示例12: _isolation_lookup
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def _isolation_lookup(self):
extensions = self._psycopg2_extensions()
return {
'AUTOCOMMIT': extensions.ISOLATION_LEVEL_AUTOCOMMIT,
'READ COMMITTED': extensions.ISOLATION_LEVEL_READ_COMMITTED,
'READ UNCOMMITTED': extensions.ISOLATION_LEVEL_READ_UNCOMMITTED,
'REPEATABLE READ': extensions.ISOLATION_LEVEL_REPEATABLE_READ,
'SERIALIZABLE': extensions.ISOLATION_LEVEL_SERIALIZABLE
}
示例13: prepare_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def prepare_db():
"""
Create a database with name in .env
"""
try:
con = psycopg2.connect(dbname='postgres', user=USER, password=PASSWORD)
except psycopg2.Error as e:
raise e
logging.info('Connected to database postgres')
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
try:
cur.execute('CREATE DATABASE ' + DB_NAME)
except psycopg2.Error as e:
logging.info('DROP OLD DATABASE')
logging.info('CREATE NEW DATABASE')
cur.execute('DROP DATABASE ' + DB_NAME)
cur.execute('CREATE DATABASE ' + DB_NAME)
cur.close()
con.close()
con = psycopg2.connect(dbname=DB_NAME, user=USER, password=PASSWORD)
cur = con.cursor()
cur.execute('CREATE EXTENSION CUBE')
cur.execute('CREATE TABLE images (id serial, name text, url text, vector cube);')
con.commit()
cur.close()
con.close()
示例14: autocommit
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def autocommit(self, on_off=1):
"""autocommit(on_off=1) -> switch autocommit on (1) or off (0)"""
if on_off > 0:
self.set_isolation_level(_ext.ISOLATION_LEVEL_AUTOCOMMIT)
else:
self.set_isolation_level(_ext.ISOLATION_LEVEL_READ_COMMITTED)
示例15: delete_database
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT [as 别名]
def delete_database(db_name):
with connect(dbname='holo', user='holocleanuser', password='abcd1234', host='localhost') as conn:
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cur:
# Terminate un-closed connections.
cur.execute("""
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = '{db_name}' AND pid <> pg_backend_pid();""".format(db_name=db_name))
# Drop the database.
cur.execute("DROP DATABASE IF EXISTS {db_name}".format(db_name=db_name))