本文整理汇总了Python中trac.db.api.DatabaseManager._get_connector方法的典型用法代码示例。如果您正苦于以下问题:Python DatabaseManager._get_connector方法的具体用法?Python DatabaseManager._get_connector怎么用?Python DatabaseManager._get_connector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trac.db.api.DatabaseManager
的用法示例。
在下文中一共展示了DatabaseManager._get_connector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: upgrade_environment
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
def upgrade_environment(self, db):
"""Each schema version should have its own upgrade module, named
upgrades/dbN.py, where 'N' is the version number (int).
"""
db_mgr = DatabaseManager(self.env)
schema_ver = self.get_schema_version(db)
cursor = db.cursor()
# Is this a new installation?
if not schema_ver:
# Perform a single-step install: Create plugin schema and
# insert default data into the database.
connector = db_mgr._get_connector()[0]
for table in db_default.schema:
for stmt in connector.to_sql(table):
cursor.execute(stmt)
for table, cols, vals in db_default.get_data(db):
cursor.executemany("INSERT INTO %s (%s) VALUES (%s)" % (table,
','.join(cols),
','.join(['%s' for c in cols])), vals)
else:
# Perform incremental upgrades.
for i in range(schema_ver + 1, db_default.schema_version + 1):
name = 'db%i' % i
try:
upgrades = __import__('tractags.upgrades', globals(),
locals(), [name])
script = getattr(upgrades, name)
except AttributeError:
raise TracError(_("No upgrade module for version %(num)i "
"(%(version)s.py)", num=i, version=name))
script.do_upgrade(self.env, i, cursor)
cursor.execute("""
UPDATE system
SET value=%s
WHERE name='tags_version'
""", (db_default.schema_version,))
self.log.info("Upgraded TracTags db schema from version %d to %d"
% (schema_ver, db_default.schema_version))
TicketTagProvider(self.env)._fetch_tkt_tags(db)
self.log.info("Synchronized ticket attributes to tags table")
db.commit()
示例2: EnvironmentSetupTestCase
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
class EnvironmentSetupTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(enable=['trac.*'])
self.env.path = tempfile.mkdtemp()
self.db_mgr = DatabaseManager(self.env)
self.db = self.env.get_db_cnx()
self.votes = VoteSystem(self.env)
def tearDown(self):
self.db.close()
# Really close db connections.
self.env.shutdown()
shutil.rmtree(self.env.path)
# Helpers
def _schema_init(self, schema=None):
cursor = self.db.cursor()
cursor.execute("DROP TABLE IF EXISTS votes")
if schema:
connector = self.db_mgr._get_connector()[0]
for table in schema:
for stmt in connector.to_sql(table):
cursor.execute(stmt)
def _verify_curr_schema(self):
self.assertFalse(self.votes.environment_needs_upgrade(self.db))
cursor = self.db.cursor()
cursor.execute('SELECT * FROM votes')
cols = get_column_names(cursor)
self.assertTrue('resource' not in cols)
self.assertEquals(['realm', 'resource_id', 'version', 'username',
'vote', 'time', 'changetime'], cols)
cursor.execute("""
SELECT value
FROM system
WHERE name='vote_version'
""")
schema_ver = int(cursor.fetchone()[0])
self.assertEquals(self.votes.schema_version, schema_ver)
def _verify_schema_unregistered(self):
cursor = self.db.cursor()
cursor.execute("""
SELECT value
FROM system
WHERE name='vote_version'
""")
self.assertFalse(cursor.fetchone())
# Tests
def test_new_install(self):
# Current tracvotes schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
self._schema_init()
self.assertEquals(0, self.votes.get_schema_version(self.db))
self.assertTrue(self.votes.environment_needs_upgrade(self.db))
self.votes.upgrade_environment(self.db)
self._verify_curr_schema()
def test_upgrade_v1_to_current(self):
# The initial db schema from r2963 - 02-Jan-2008 by Alec Thomas.
schema = [
Table('votes', key=('resource', 'username', 'vote'))[
Column('resource'),
Column('username'),
Column('vote', 'int'),
]
]
self._schema_init(schema)
# Populate tables with test data.
cursor = self.db.cursor()
cursor.executemany("""
INSERT INTO votes
(resource,username,vote)
VALUES (%s,%s,%s)
""", (('ticket/1','user',-1), ('ticket/2','user',1),
('wiki/DeletedPage','user',-1), ('wiki/ExistingPage','user',1)))
# Resources must exist for successful data migration.
t = Ticket(self.env, db=self.db)
t['summary'] = 'test ticket'
t.insert()
w = WikiPage(self.env, 'ExistingPage')
w.text = 'content'
w.save('author', 'comment', '::1')
self._verify_schema_unregistered()
self.assertEquals(1, self.votes.get_schema_version(self.db))
self.assertTrue(self.votes.environment_needs_upgrade(self.db))
# Data migration and registration of unversioned schema.
self.votes.upgrade_environment(self.db)
self._verify_curr_schema()
cursor.execute('SELECT * FROM votes')
votes = cursor.fetchall()
#.........这里部分代码省略.........
示例3: AnnouncementSystemSetupTestCase
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
class AnnouncementSystemSetupTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(enable=['trac.*'])
self.env.path = tempfile.mkdtemp()
self.db_mgr = DatabaseManager(self.env)
self.db = self.env.get_db_cnx()
self.an_sys = AnnouncementSystem(self.env)
def tearDown(self):
self.db.close()
# Really close db connections.
self.env.shutdown()
shutil.rmtree(self.env.path)
# Helpers
def _get_cursor_description(self, cursor):
# Cursors don't look the same across Trac versions
if trac_version < '0.12':
return cursor.description
else:
return cursor.cursor.description
def _schema_init(self, schema=None):
# Current announcer schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
cursor = self.db.cursor()
cursor.execute("DROP TABLE IF EXISTS subscriptions")
cursor.execute("DROP TABLE IF EXISTS subscription")
cursor.execute("DROP TABLE IF EXISTS subscription_attribute")
cursor.execute("DELETE FROM system WHERE name='announcer_version'")
if schema:
connector = self.db_mgr._get_connector()[0]
for table in schema:
for stmt in connector.to_sql(table):
cursor.execute(stmt)
def _verify_curr_schema(self):
self.assertFalse(self.an_sys.environment_needs_upgrade(self.db))
cursor = self.db.cursor()
cursor.execute("SELECT * FROM subscription_attribute")
columns = [col[0] for col in self._get_cursor_description(cursor)]
self.assertTrue('name' not in columns)
self.assertTrue('value' not in columns)
self.assertEquals(
['id', 'sid', 'authenticated', 'class', 'realm', 'target'],
columns
)
cursor.execute("""
SELECT value
FROM system
WHERE name='announcer_version'
""")
version = int(cursor.fetchone()[0])
self.assertEquals(db_default.schema_version, version)
def _verify_version_unregistered(self):
cursor = self.db.cursor()
cursor.execute("""
SELECT value
FROM system
WHERE name='announcer_version'
""")
self.assertFalse(cursor.fetchone())
# Tests
def test_new_install(self):
# Just do db table clean-up.
self._schema_init()
self.assertEquals(0, self.an_sys.get_schema_version(self.db))
self.assertTrue(self.an_sys.environment_needs_upgrade(self.db))
self.an_sys.upgrade_environment(self.db)
self._verify_curr_schema()
def test_upgrade_v1_to_current(self):
# The initial db schema from r3015 - 10-Jan-2008 by Stephen Hansen.
schema = [
Table('subscriptions', key='id')[
Column('id', auto_increment=True),
Column('sid'),
Column('enabled', type='int'),
Column('managed', type='int'),
Column('realm'),
Column('category'),
Column('rule'),
Column('destination'),
Column('format'),
Index(['id']),
Index(['realm', 'category', 'enabled']),
]
]
self._schema_init(schema)
# Populate tables with test data.
cursor = self.db.cursor()
cursor.executemany("""
#.........这里部分代码省略.........
示例4: _call_db5_upgrade
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
def _call_db5_upgrade(self):
dbm = DatabaseManager(self.env)
db_connector, _ = dbm._get_connector()
db = self.env.get_db_cnx()
self.assert_true(do_upgrade(self.env, 5, db.cursor(), db_connector))
db.commit()
示例5: TagSetupTestCase
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
class TagSetupTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(enable=["trac.*"])
self.env.path = tempfile.mkdtemp()
self.db_mgr = DatabaseManager(self.env)
self.db = self.env.get_db_cnx()
def tearDown(self):
self.db.close()
# Really close db connections.
self.env.shutdown()
shutil.rmtree(self.env.path)
# Helpers
def _get_cursor_description(self, cursor):
# Cursors don't look the same across Trac versions
if trac_version < "0.12":
return cursor.description
else:
return cursor.cursor.description
def _revert_tractags_schema_init(self):
cursor = self.db.cursor()
cursor.execute("DROP TABLE IF EXISTS tags")
cursor.execute("DELETE FROM system WHERE name='tags_version'")
cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ("TAGS_%",))
# Tests
def test_new_install(self):
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
self._revert_tractags_schema_init()
self.assertEquals(0, setup.get_schema_version(self.db))
self.assertTrue(setup.environment_needs_upgrade(self.db))
setup.upgrade_environment(self.db)
self.assertFalse(setup.environment_needs_upgrade(self.db))
cursor = self.db.cursor()
cursor.execute("SELECT * FROM tags")
tags = cursor.fetchall()
self.assertEquals([], tags)
self.assertEquals(["tagspace", "name", "tag"], [col[0] for col in self._get_cursor_description(cursor)])
cursor.execute(
"""
SELECT value
FROM system
WHERE name='tags_version'
"""
)
version = int(cursor.fetchone()[0])
self.assertEquals(db_default.schema_version, version)
def test_upgrade_schema_v1(self):
# Ancient, unversioned schema - wiki only.
schema = [Table("wiki_namespace")[Column("name"), Column("namespace"), Index(["name", "namespace"])]]
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
self._revert_tractags_schema_init()
connector = self.db_mgr._get_connector()[0]
cursor = self.db.cursor()
for table in schema:
for stmt in connector.to_sql(table):
cursor.execute(stmt)
# Populate table with migration test data.
cursor.execute(
"""
INSERT INTO wiki_namespace
(name, namespace)
VALUES ('WikiStart', 'tag')
"""
)
cursor = self.db.cursor()
cursor.execute("SELECT * FROM wiki_namespace")
tags = cursor.fetchall()
self.assertEquals([("WikiStart", "tag")], tags)
self.assertEquals(1, setup.get_schema_version(self.db))
self.assertTrue(setup.environment_needs_upgrade(self.db))
setup.upgrade_environment(self.db)
self.assertFalse(setup.environment_needs_upgrade(self.db))
cursor = self.db.cursor()
cursor.execute("SELECT * FROM tags")
tags = cursor.fetchall()
# Db content should be migrated.
self.assertEquals([("wiki", "WikiStart", "tag")], tags)
self.assertEquals(["tagspace", "name", "tag"], [col[0] for col in self._get_cursor_description(cursor)])
cursor.execute(
"""
SELECT value
FROM system
WHERE name='tags_version'
"""
)
version = int(cursor.fetchone()[0])
#.........这里部分代码省略.........
示例6: TagSetupTestCase
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import _get_connector [as 别名]
class TagSetupTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(enable=['trac.*'])
self.env.path = tempfile.mkdtemp()
self.db_mgr = DatabaseManager(self.env)
def tearDown(self):
self.env.shutdown()
shutil.rmtree(self.env.path)
# Helpers
def _get_cursor_description(self, cursor):
# Cursors don't look the same across Trac versions
if trac_version < '0.12':
return cursor.description
else:
return cursor.cursor.description
def _revert_tractags_schema_init(self):
with self.env.db_transaction as db:
db("DROP TABLE IF EXISTS tags")
db("DROP TABLE IF EXISTS tags_change")
db("DELETE FROM system WHERE name='tags_version'")
db("DELETE FROM permission WHERE action %s" % db.like(),
('TAGS_%',))
def get_db_version(self):
for version, in self.env.db_query("""
SELECT value FROM system
WHERE name='tags_version'
"""):
return int(version)
# Tests
def test_new_install(self):
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
self._revert_tractags_schema_init()
self.assertEquals(0, setup.get_schema_version())
self.assertTrue(setup.environment_needs_upgrade())
setup.upgrade_environment()
self.assertFalse(setup.environment_needs_upgrade())
with self.env.db_query as db:
cursor = db.cursor()
cursor.execute("SELECT * FROM tags")
cols = [col[0] for col in self._get_cursor_description(cursor)]
self.assertEquals([], cursor.fetchall())
self.assertEquals(['tagspace', 'name', 'tag'], cols)
self.assertEquals(db_default.schema_version, self.get_db_version())
def test_upgrade_schema_v1(self):
# Ancient, unversioned schema - wiki only.
schema = [
Table('wiki_namespace')[
Column('name'),
Column('namespace'),
Index(['name', 'namespace']),
]
]
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
# Revert these changes for clean install testing.
self._revert_tractags_schema_init()
connector = self.db_mgr._get_connector()[0]
with self.env.db_transaction as db:
for table in schema:
for stmt in connector.to_sql(table):
db(stmt)
# Populate table with migration test data.
db("""INSERT INTO wiki_namespace (name, namespace)
VALUES ('WikiStart', 'tag')""")
tags = self.env.db_query("SELECT * FROM wiki_namespace")
self.assertEquals([('WikiStart', 'tag')], tags)
self.assertEquals(1, setup.get_schema_version())
self.assertTrue(setup.environment_needs_upgrade())
setup.upgrade_environment()
self.assertFalse(setup.environment_needs_upgrade())
with self.env.db_query as db:
cursor = db.cursor()
cursor.execute("SELECT * FROM tags")
tags = cursor.fetchall()
cols = [col[0] for col in self._get_cursor_description(cursor)]
# Db content should be migrated.
self.assertEquals([('wiki', 'WikiStart', 'tag')], tags)
self.assertEquals(['tagspace', 'name', 'tag'], cols)
self.assertEquals(db_default.schema_version, self.get_db_version())
def test_upgrade_schema_v2(self):
# Just register a current, but unversioned schema.
schema = [
Table('tags', key=('tagspace', 'name', 'tag'))[
Column('tagspace'),
#.........这里部分代码省略.........