def upgrade_environment(self, db): db_connector, _ = DatabaseManager(self.env).get_connector() cursor = db.cursor() dbver = self.get_db_version() if dbver == 0: self.env.log.info("Initialize %s database schema to version %s", PLUGIN_NAME, PLUGIN_VERSION) for table in SCHEMA: for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute( """ INSERT INTO system (name, value) VALUES (%s, %s) """, (PLUGIN_NAME, PLUGIN_VERSION)) else: while dbver != PLUGIN_VERSION: dbver = dbver + 1 self.env.log.info("Upgrade %s database schema to version %s", PLUGIN_NAME, dbver) modulename = 'db%i' % dbver upgrades = __import__('cards.upgrades', globals(), locals(), [modulename]) script = getattr(upgrades, modulename) script.do_upgrade(self.env, dbver, cursor) cursor.execute( """ UPDATE system SET value=%s WHERE name=%s """, (PLUGIN_VERSION, PLUGIN_NAME))
def do_upgrade(env, ver, cursor): """Add two more subscription db tables for a better normalized schema.""" connector = DatabaseManager(env).get_connector()[0] for table in schema: for stmt in connector.to_sql(table): cursor.execute(stmt)
def do_upgrade(env, ver, cursor): """Add an auto-increment primary key to `node_change` table and indices (repos, rev, path) and (repos, path, rev) (#3676). """ db_connector, _ = DatabaseManager(env)._get_connector() table = Table('node_change', key='id')[Column('id', auto_increment=True), Column('repos', type='int'), Column('rev', key_size=40), Column('path', key_size=255), Column('node_type', size=1), Column('change_type', size=1), Column('base_path'), Column('base_rev'), Index(['repos', 'rev', 'path']), Index(['repos', 'path', 'rev'])] with env.db_transaction: cursor.execute("""CREATE TEMPORARY TABLE node_change_old AS SELECT * FROM node_change""") cursor.execute("DROP TABLE node_change") for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute("""\ INSERT INTO node_change (repos,rev,path,node_type,change_type,base_path,base_rev) SELECT repos,rev,path,node_type,change_type,base_path,base_rev FROM node_change_old""") cursor.execute("DROP TABLE node_change_old")
def do_upgrade(env, ver, cursor): """Add an auto-increment primary key to `node_change` table and indices (repos, rev, path) and (repos, path, rev) (#3676). """ db_connector, _ = DatabaseManager(env)._get_connector() table = Table('node_change', key='id')[ Column('id', auto_increment=True), Column('repos', type='int'), Column('rev', key_size=40), Column('path', key_size=255), Column('node_type', size=1), Column('change_type', size=1), Column('base_path'), Column('base_rev'), Index(['repos', 'rev', 'path']), Index(['repos', 'path', 'rev'])] with env.db_transaction: cursor.execute("""CREATE TEMPORARY TABLE node_change_old AS SELECT * FROM node_change""") cursor.execute("DROP TABLE node_change") for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute("""\ INSERT INTO node_change (repos,rev,path,node_type,change_type,base_path,base_rev) SELECT repos,rev,path,node_type,change_type,base_path,base_rev FROM node_change_old""") cursor.execute("DROP TABLE node_change_old")
def do_upgrade(env, ver, cursor): """Change `subscription_attribute` db table: + 'subscription_attribute.authenticated' """ cursor.execute(""" CREATE TEMPORARY TABLE subscription_attribute_old AS SELECT * FROM subscription_attribute """) cursor.execute("DROP TABLE subscription_attribute") connector = DatabaseManager(env).get_connector()[0] for table in schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute(""" INSERT INTO subscription_attribute (sid,authenticated,class,realm,target) SELECT o.sid,s.authenticated,o.class,o.realm,o.target FROM subscription_attribute_old AS o LEFT JOIN session AS s ON o.sid=s.sid """) cursor.execute("DROP TABLE subscription_attribute_old") # DEVEL: Think that an old 'subscriptions' db table may still exist here. cursor.execute("DROP TABLE IF EXISTS subscriptions")
def upgrade_environment(self, db): db_connector, _ = DatabaseManager(self.env).get_connector() cursor = db.cursor() dbver = self.get_db_version() if dbver == 0: self.env.log.info("Initialize %s database schema to version %s", PLUGIN_NAME, PLUGIN_VERSION) for table in SCHEMA: for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute(""" INSERT INTO system (name, value) VALUES (%s, %s) """, (PLUGIN_NAME, PLUGIN_VERSION)) else: while dbver != PLUGIN_VERSION: dbver = dbver + 1 self.env.log.info("Upgrade %s database schema to version %s", PLUGIN_NAME, dbver) modulename = 'db%i' % dbver upgrades = __import__('cards.upgrades', globals(), locals(), [modulename]) script = getattr(upgrades, modulename) script.do_upgrade(self.env, dbver, cursor) cursor.execute(""" UPDATE system SET value=%s WHERE name=%s """, (PLUGIN_VERSION, PLUGIN_NAME))
def do_upgrade(env, ver, cursor): """Changes to subscription db table: - 'subscriptions.destination', 'subscriptions.format' + 'subscriptions.authenticated', 'subscriptions.transport' 'subscriptions.managed' type='int' --> (default == char) """ cursor.execute(""" CREATE TEMPORARY TABLE subscriptions_old AS SELECT * FROM subscriptions """) cursor.execute("DROP TABLE subscriptions") connector = DatabaseManager(env).get_connector()[0] for table in schema: for stmt in connector.to_sql(table): cursor.execute(stmt) cursor.execute(""" INSERT INTO subscriptions (sid,authenticated,enabled,managed, realm,category,rule,transport) SELECT o.sid,s.authenticated,o.enabled,'watcher', o.realm,o.category,rule,'email' FROM subscriptions_old AS o LEFT JOIN session AS s ON o.sid=s.sid """) cursor.execute("DROP TABLE subscriptions_old")
def upgrade_environment(self, db): self.log.debug("Upgrading schema for svnhooks plugin") db_backend, _ = DatabaseManager(self.env).get_connector() cursor = db.cursor() for table in SVNHooksModel.svnhooks_schema: for stmt in db_backend.to_sql(table): self.log.debug(stmt) cursor.execute(stmt)
def environment_created(self): db_connector, _ = DatabaseManager(self.env).get_connector() with self.env.db_transaction as db: cursor = db.cursor() for table in SCHEMA: for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute(""" INSERT INTO system (name, value) VALUES (%s, %s) """, (PLUGIN_NAME, PLUGIN_VERSION))
def environment_created(self): db_connector, _ = DatabaseManager(self.env).get_connector() with self.env.db_transaction as db: cursor = db.cursor() for table in SCHEMA: for stmt in db_connector.to_sql(table): cursor.execute(stmt) cursor.execute( """ INSERT INTO system (name, value) VALUES (%s, %s) """, (PLUGIN_NAME, PLUGIN_VERSION))
def upgrade_environment(self, db): self.log.debug("Upgrading schema for disclaimer plugin") db_backend, _ = DatabaseManager(self.env).get_connector() cursor = db.cursor() for table in DisclaimerModel.disclaimer_schema: for stmt in db_backend.to_sql(table): self.log.debug(stmt) cursor.execute(stmt) cursor.execute("select count(*) from disclaimer") entries = cursor.fetchone()[0] if entries == 0: DisclaimerModel(self.env).insert(self.c_name, self.c_text, 'system', db=db)
def create_table(env): ''' Constructor, see trac/db/postgres_backend.py:95 (method init_db) ''' conn, dummyArgs = DatabaseManager(env).get_connector() db = env.get_read_db() cursor = db.cursor() for stmt in conn.to_sql(XMAIL_TABLE): if db.schema: stmt = re.sub(r'CREATE TABLE ', 'CREATE TABLE "' + db.schema + '".', stmt) env.log.info("result of execution: %s" % cursor.execute(stmt)) db.commit() db.close()
def create_table(env): ''' Constructor, see trac/db/postgres_backend.py:95 (method init_db) ''' conn, dummyArgs = DatabaseManager(env).get_connector() db = env.get_read_db() cursor = db.cursor() for stmt in conn.to_sql(XMAIL_TABLE): if db.schema: stmt = re.sub(r'CREATE TABLE ','CREATE TABLE "' + db.schema + '".', stmt) env.log.info( "result of execution: %s" % cursor.execute(stmt) ) db.commit() db.close()
def do_upgrade(env, ver, cursor): """Migrate old `subscriptions` db table. Changes to other tables: 'subscription.priority' type=(default == char) --> 'int' 'subscription_attribute.name --> 'subscription_attribute.realm' 'subscription_attribute.value --> 'subscription_attribute.target' """ with env.db_transaction as db: cursor = db.cursor() cursor.execute(""" CREATE TEMPORARY TABLE subscription_old AS SELECT * FROM subscription """) cursor.execute("DROP TABLE subscription") cursor.execute(""" CREATE TEMPORARY TABLE subscription_attribute_old AS SELECT * FROM subscription_attribute """) cursor.execute("DROP TABLE subscription_attribute") connector = DatabaseManager(env).get_connector()[0] for table in schema: for stmt in connector.to_sql(table): cursor.execute(stmt) # Convert priority values to integer. cursor.execute(""" INSERT INTO subscription (time,changetime,class,sid,authenticated, distributor,format,priority,adverb) SELECT o.time,o.changetime,o.class,o.sid,o.authenticated, o.distributor,o.format,%s,o.adverb FROM subscription_old AS o """ % db.cast('o.priority', 'int')) cursor.execute("DROP TABLE subscription_old") # Copy table on column name change. cursor.execute(""" INSERT INTO subscription_attribute (sid,class,realm,target) SELECT o.sid,o.class,o.name,o.value FROM subscription_attribute_old AS o """) cursor.execute("DROP TABLE subscription_attribute_old") # DEVEL: Migrate old subscription db table data. cursor.execute("DROP TABLE IF EXISTS subscriptions")
def create_table(self): ''' Constructor, see trac/postgres_backend.py:95 (method init_db) ''' conn, dummyArgs = DatabaseManager(self.env).get_connector() try: with self.env.db_transaction as db: for stmt in conn.to_sql(BUDGETING_TABLE): if db.schema: stmt = re.sub(r'CREATE TABLE ', 'CREATE TABLE "' + db.schema + '".', stmt) stmt = re.sub(r'(?i)bigint', 'NUMERIC(10,2)', stmt) stmt += ";" self.log.info("[INIT table] executing sql: %s" % stmt) db(stmt) self.log.info("[INIT table] successfully created table %s" % BUDGETING_TABLE.name) except Exception, e: self.log.error("[INIT table] Error executing SQL Statement \n %s" % e)
def create_table(self): ''' Constructor, see trac/postgres_backend.py:95 (method init_db) ''' conn, dummyArgs = DatabaseManager(self.env).get_connector() try: with self.env.db_transaction as db: for stmt in conn.to_sql(BUDGETING_TABLE): if db.schema: stmt = re.sub(r'CREATE TABLE ', 'CREATE TABLE "' + db.schema + '".', stmt) stmt = re.sub(r'(?i)bigint', 'NUMERIC(10,2)', stmt) stmt += ";" self.log.info("[INIT table] executing sql: %s" % stmt) db(stmt) self.log.info( "[INIT table] successfully created table %s" % BUDGETING_TABLE.name) except Exception, e: self.log.error("[INIT table] Error executing SQL Statement \n %s" % e)
def to_sql(env, table): """ Convenience function to get the to_sql for the active connector.""" dc = DatabaseManager(env)._get_connector()[0] return dc.to_sql(table)
def to_sql(env, table): from trac.db.api import DatabaseManager dc = DatabaseManager(env).get_connector()[0] return dc.to_sql(table)
def _create_table(self, cursor, table): db_manager, _ = DatabaseManager(self.env)._get_connector() for sql in db_manager.to_sql(table): cursor.execute(sql)
def to_sql(env, table): """ Convenience function to get the to_sql for the active connector.""" from trac.db.api import DatabaseManager dc = DatabaseManager(env)._get_connector()[0] return dc.to_sql(table)