svn commit: r1476118 - in /bloodhound/trunk/bloodhound_multiproduct: multiproduct/api.py tests/upgrade.py
Author: astaric Date: Fri Apr 26 09:10:14 2013 New Revision: 1476118 URL: http://svn.apache.org/r1476118 Log: Added more tests for multiproduct upgrade, refactoring. Modified: bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py Modified: bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py?rev=1476118r1=1476117r2=1476118view=diff == --- bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py (original) +++ bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py Fri Apr 26 09:10:14 2013 @@ -30,6 +30,7 @@ from trac.attachment import Attachment from trac.config import Option, PathOption from trac.core import Component, TracError, implements, Interface from trac.db import Table, Column, DatabaseManager, Index +import trac.db_default from trac.env import IEnvironmentSetupParticipant, Environment from trac.perm import IPermissionRequestor, PermissionCache from trac.resource import IExternalResourceConnector, IResourceChangeListener,\ @@ -97,7 +98,7 @@ class MultiProductSystem(Component): global environment configuration. ) -SCHEMA = [mcls._get_schema() \ +SCHEMA = [mcls._get_schema() for mcls in (Product, ProductResourceMap)] # Tables which should be migrated (extended with 'product' column) @@ -202,171 +203,207 @@ class MultiProductSystem(Component): db_installed_version = self.get_version() with self.env.db_direct_transaction as db: if db_installed_version 1: -# Initial installation -db(ALTER TABLE ticket ADD COLUMN product TEXT) -self.log.debug(creating initial db tables for %s plugin. % - PLUGIN_NAME) -db_connector, dummy = DatabaseManager(self.env)._get_connector() -for table in self.SCHEMA: -for statement in db_connector.to_sql(table): -db(statement) +self._add_column_product_to_ticket(db) +self._create_multiproduct_tables(db) db_installed_version = self._update_db_version(db, 1) if db_installed_version 2: -from multiproduct.model import Product -products = Product.select(self.env) -for prod in products: -db(UPDATE ticket SET product=%s - WHERE product=%s, (prod.prefix, prod.name)) +self._replace_product_on_ticket_with_product_prefix(db) db_installed_version = self._update_db_version(db, 2) if db_installed_version 3: -from multiproduct.model import Product -import trac.db_default - -def create_temp_table(table): -creates temporary table with the new schema and -drops original table -table_temp_name = '%s_temp' % table -if table == 'report': -cols = ','.join([c for c in table_columns[table] if c != 'id']) -else: -cols = ','.join(table_columns[table]) -self.log.info(Migrating table '%s' to a new schema, table) -db(CREATE TABLE %s AS SELECT %s FROM %s % - (table_temp_name, cols, table)) -db(DROP TABLE %s % table) -db_connector, _ = DatabaseManager(self.env)._get_connector() -table_schema = [t for t in table_defs if t.name == table][0] -for sql in db_connector.to_sql(table_schema): -db(sql) -return table_temp_name, cols - -def drop_temp_table(table): -drops specified temporary table -db(DROP TABLE %s % table) - -TICKET_TABLES = ['ticket_change', 'ticket_custom', - 'attachment', -] SYSTEM_TABLES = ['system'] +TICKET_TABLES = [ +'ticket_change', 'ticket_custom', 'attachment', +] +table_defs = self._add_product_column_to_tables( +self.MIGRATE_TABLES + TICKET_TABLES + SYSTEM_TABLES, +db_installed_version) +table_columns = self._get_table_columns(table_defs) +create_temp_table = lambda table: self._create_temp_table( +db, table, table_columns, table_defs) + +self._insert_default_product(db) +self._upgrade_tickets(db, TICKET_TABLES, create_temp_table) +
[Apache Bloodhound] New user registration: think4ward
New user registration for user think4ward -- Apache Bloodhound https://issues.apache.org/bloodhound/ The Apache Bloodhound issue tracker
svn commit: r1476152 - in /bloodhound/trunk/bloodhound_multiproduct/tests: upgrade.py upgrade_postgres.py
Author: astaric Date: Fri Apr 26 11:56:33 2013 New Revision: 1476152 URL: http://svn.apache.org/r1476152 Log: Added upgrade tests for postgres backend. Added: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py Modified: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py Modified: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py?rev=1476152r1=1476151r2=1476152view=diff == --- bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py (original) +++ bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py Fri Apr 26 11:56:33 2013 @@ -20,6 +20,7 @@ from sqlite3 import OperationalError from contextlib import contextmanager import os +import shutil import tempfile import unittest import uuid @@ -51,23 +52,25 @@ TABLES_WITH_PRODUCT_FIELD = ( class EnvironmentUpgradeTestCase(unittest.TestCase): -def setUp(self): +def setUp(self, options=()): self.env_path = tempfile.mkdtemp('multiproduct-tempenv') -self.env = Environment(self.env_path, create=True) +self.env = Environment(self.env_path, create=True, options=options) DummyPlugin.version = 1 +def tearDown(self): +shutil.rmtree(self.env_path) + def test_can_upgrade_environment_with_multi_product_disabled(self): self.env.upgrade() # Multiproduct was not enabled so multiproduct tables should not exist -with self.env.db_direct_transaction as db: -for table in BLOODHOUND_TABLES: -with self.assertFailsWithMissingTable(): -db(SELECT * FROM %s % table) +for table in BLOODHOUND_TABLES: +with self.assertFailsWithMissingTable(): +self.env.db_direct_query(SELECT * FROM %s % table) -for table in TABLES_WITH_PRODUCT_FIELD: -with self.assertFailsWithMissingColumn(): -db(SELECT product FROM %s % table) +for table in TABLES_WITH_PRODUCT_FIELD: +with self.assertFailsWithMissingColumn(): +self.env.db_direct_query(SELECT product FROM %s % table) def test_upgrade_creates_multi_product_tables_and_adds_product_column(self): self._enable_multiproduct() @@ -91,8 +94,8 @@ class EnvironmentUpgradeTestCase(unittes self._add_custom_field('custom_field') with self.env.db_direct_transaction as db: db(INSERT INTO ticket (id) VALUES (1)) -db(INSERT INTO attachment (type, id) - VALUES ('ticket', '1')) +db(INSERT INTO attachment (type, id, filename) + VALUES ('ticket', '1', '')) db(INSERT INTO ticket_custom (ticket, name, value) VALUES (1, 'custom_field', '42')) db(INSERT INTO ticket_change (ticket, time, field) @@ -114,8 +117,8 @@ class EnvironmentUpgradeTestCase(unittes def test_upgrade_moves_custom_wikis_to_default_product(self): with self.env.db_direct_transaction as db: db(INSERT INTO wiki (name, version) VALUES ('MyPage', 1)) -db(INSERT INTO attachment (type, id) - VALUES ('wiki', 'MyPage')) +db(INSERT INTO attachment (type, id, filename) + VALUES ('wiki', 'MyPage', '')) self._enable_multiproduct() self.env.upgrade() @@ -131,8 +134,8 @@ class EnvironmentUpgradeTestCase(unittes def test_upgrade_duplicates_system_wikis_to_products(self): with self.env.db_direct_transaction as db: db(INSERT INTO wiki (name, version) VALUES ('WikiStart', 1)) -db(INSERT INTO attachment (type, id) - VALUES ('wiki', 'WikiStart')) +db(INSERT INTO attachment (type, id, filename) + VALUES ('wiki', 'WikiStart', '')) self._enable_multiproduct() self.env.upgrade() @@ -227,18 +230,18 @@ class EnvironmentUpgradeTestCase(unittes def test_can_upgrade_database_with_ticket_attachment_with_text_ids(self): with self.env.db_direct_transaction as db: -db(INSERT INTO attachment (id, type) - VALUES ('abc', 'ticket')) +db(INSERT INTO attachment (id, type, filename) + VALUES ('abc', 'ticket', '')) self._enable_multiproduct() self.env.upgrade() def test_can_upgrade_database_with_orphaned_attachments(self): with self.env.db_direct_transaction as db: -db(INSERT INTO attachment (id, type) - VALUES ('5', 'ticket')) -db(INSERT INTO attachment (id, type) - VALUES ('MyWiki', 'wiki')) +db(INSERT INTO attachment (id, type, filename) + VALUES ('5', 'ticket',
svn commit: r1476161 - /bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py
Author: andrej Date: Fri Apr 26 12:16:31 2013 New Revision: 1476161 URL: http://svn.apache.org/r1476161 Log: fixing auto_inc_fields behaviour, adding ORDER BY feature to model select function Modified: bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py Modified: bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py?rev=1476161r1=1476160r2=1476161view=diff == --- bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py (original) +++ bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py Fri Apr 26 12:16:31 2013 @@ -158,21 +158,32 @@ class ModelBase(object): raise TracError('%(object_name)s %(keys)s already exists %(values)s' % sdata) +auto_inc = self._meta.get('auto_inc_fields', []) for key in self._meta['key_fields']: -if self._data[key] is None: +if self._data[key] is None and key not in auto_inc: sdata = {'key':key} sdata.update(self._meta) raise TracError('%(key)s required for %(object_name)s' % sdata) -fields = self._meta['key_fields']+self._meta['non_key_fields'] + +auto_inc = self._meta.get('auto_inc_fields', []) +non_auto_increment_key_fields = [ +field for field in self._meta['key_fields'] +if field not in auto_inc] +fields = non_auto_increment_key_fields + self._meta['non_key_fields'] sdata = {'fields':','.join(fields), 'values':','.join(['%s'] * len(fields))} sdata.update(self._meta) - + sql = INSERT INTO %(table_name)s (%(fields)s) VALUES (%(values)s) % sdata with self._env.db_transaction as db: -db(sql, [self._data[f] for f in fields]) +cursor = db.cursor() +cursor.execute(sql, [self._data[f] for f in fields]) +for auto_in_field in auto_inc: +self._data[auto_in_field] = db.get_last_id( +cursor, sdata[table_name], auto_in_field) + self._exists = True self._old_data.update(self._data) TicketSystem(self._env).reset_ticket_fields() @@ -216,10 +227,13 @@ class ModelBase(object): ResourceSystem(self._env).resource_changed(self, old_values) - @classmethod -def select(cls, env, db=None, where=None, limit=None): -Query the database to get a set of records back +def select(cls, env, db=None, where=None, limit=None, order_by=None): + +Query the database to get a set of records back +* order_by: is list of fields with optional sort direction +(asc or desc) e.g. [field1, field2 desc] + rows = [] fields = cls._meta['key_fields']+cls._meta['non_key_fields'] @@ -229,11 +243,13 @@ class ModelBase(object): wherestr, values = dict_to_kv_str(where) if wherestr: wherestr = ' WHERE ' + wherestr +final_sql = sql + wherestr if limit is not None: -limitstr = ' LIMIT ' + str(int(limit)) -else: -limitstr = '' -for row in env.db_query(sql + wherestr + limitstr, values): +final_sql += ' LIMIT ' + str(int(limit)) +final_sql +if order_by: +final_sql += \nORDER BY + ', '.join(order_by) +for row in env.db_query(final_sql, values): # we won't know which class we need until called model = cls.__new__(cls) data = dict([(fields[i], row[i]) for i in range(len(fields))])
svn commit: r1476162 - in /bloodhound/trunk: ./ bloodhound_relations/ bloodhound_relations/bhrelations/ bloodhound_relations/bhrelations/tests/ bloodhound_relations/trac_ticket_links/
Author: andrej Date: Fri Apr 26 12:17:58 2013 New Revision: 1476162 URL: http://svn.apache.org/r1476162 Log: adding bhrelation api draft implementation, removing trac-ticket-links dependencies Added: bloodhound/trunk/bloodhound_relations/bhrelations/db_default.py bloodhound/trunk/bloodhound_relations/bhrelations/model.py bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py Removed: bloodhound/trunk/bloodhound_relations/bhrelations/tests/bhrelations_links.py bloodhound/trunk/bloodhound_relations/bhrelations/ticket_links_other.py bloodhound/trunk/bloodhound_relations/trac_ticket_links/ Modified: bloodhound/trunk/.rat-ignore bloodhound/trunk/bloodhound_relations/README bloodhound/trunk/bloodhound_relations/bhrelations/api.py bloodhound/trunk/bloodhound_relations/setup.py Modified: bloodhound/trunk/.rat-ignore URL: http://svn.apache.org/viewvc/bloodhound/trunk/.rat-ignore?rev=1476162r1=1476161r2=1476162view=diff == --- bloodhound/trunk/.rat-ignore (original) +++ bloodhound/trunk/.rat-ignore Fri Apr 26 12:17:58 2013 @@ -10,7 +10,6 @@ doc/html-templates/js/jquery-1.8.2.js doc/wireframes/src/ installer/README.rst trac/ -bloodhound_relations/bhrelations/trac/ bloodhound_relations/bhrelations/default-pages/ bloodhound_multiproduct/tests/admin/*.txt bloodhound_multiproduct/tests/*.txt \ No newline at end of file Modified: bloodhound/trunk/bloodhound_relations/README URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_relations/README?rev=1476162r1=1476161r2=1476162view=diff == --- bloodhound/trunk/bloodhound_relations/README (original) +++ bloodhound/trunk/bloodhound_relations/README Fri Apr 26 12:17:58 2013 @@ -28,8 +28,4 @@ If you have any issues, please create a == The Trac ticket-links branch Bloodhound Relations plugin contains the code from the Trac ticket-links branch, which -is licensed under the same license as Trac (http://trac.edgewall.org/wiki/TracLicense). -The plugin trac_ticket_links directory represents an updated copy of the combined vendor branch -located on https://svn.apache.org/repos/asf/bloodhound/vendor/trac-ticket-links. -The combined vendor branch represents a source tree merged from the several original -vendor branches. For more information on the original vendor branches, see //svn.apache.org/repos/asf/bloodhound/vendor/README +is licensed under the same license as Trac (http://trac.edgewall.org/wiki/TracLicense). \ No newline at end of file Modified: bloodhound/trunk/bloodhound_relations/bhrelations/api.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_relations/bhrelations/api.py?rev=1476162r1=1476161r2=1476162view=diff == --- bloodhound/trunk/bloodhound_relations/bhrelations/api.py (original) +++ bloodhound/trunk/bloodhound_relations/bhrelations/api.py Fri Apr 26 12:17:58 2013 @@ -17,5 +17,280 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from bhrelations import db_default +from bhrelations.model import Relation +from multiproduct.env import ProductEnvironment +from trac.core import Component, implements, TracError +from trac.env import IEnvironmentSetupParticipant +from trac.db import DatabaseManager +from trac.resource import manager_for_neighborhood, ResourceSystem +from trac.ticket import Ticket +PLUGIN_NAME = 'Bloodhound Relations Plugin' + + +class EnvironmentSetup(Component): +implements(IEnvironmentSetupParticipant) + +def environment_created(self): +self.found_db_version = 0 +self.upgrade_environment(self.env.db_transaction) + +def environment_needs_upgrade(self, db): +Detects if the installed db version matches the running system +db_installed_version = self._get_version() + +db_version = db_default.DB_VERSION +if db_installed_version db_version: +raise TracError('''Current db version (%d) newer than supported by +this version of the %s (%d).''' % (db_installed_version, + PLUGIN_NAME, + db_version)) +needs_upgrade = db_installed_version db_version +return needs_upgrade + +def upgrade_environment(self, db): +self.log.debug(upgrading existing environment for %s plugin. % + PLUGIN_NAME) +db_installed_version = self._get_version() +with self.env.db_direct_transaction as db: +if db_installed_version 1: +self._initialize_db(db) +self._update_db_version(db, 1) +#add upgrade logic later if needed + +def _get_version(self): +Finds the current