Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CE913D8A8 for ; Tue, 2 Oct 2012 21:30:28 +0000 (UTC) Received: (qmail 63375 invoked by uid 500); 2 Oct 2012 21:30:28 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 63299 invoked by uid 500); 2 Oct 2012 21:30:28 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 63186 invoked by uid 99); 2 Oct 2012 21:30:28 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Oct 2012 21:30:28 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 3CE5A37476; Tue, 2 Oct 2012 21:30:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: brondsem@apache.org To: allura-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [10/34] git commit: [#4785] ticket:163 add migrate-urls command Message-Id: <20121002213028.3CE5A37476@tyr.zones.apache.org> Date: Tue, 2 Oct 2012 21:30:28 +0000 (UTC) [#4785] ticket:163 add migrate-urls command Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/aead33fc Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/aead33fc Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/aead33fc Branch: refs/heads/db/4968 Commit: aead33fc0bf25d13c04f4364aa0326e2305b2fa5 Parents: 0298d85 Author: Igor Bondarenko Authored: Fri Sep 7 12:52:19 2012 +0300 Committer: Cory Johns Committed: Tue Oct 2 15:12:30 2012 +0000 ---------------------------------------------------------------------- Allura/development.ini | 6 ++ ForgeShortUrl/forgeshorturl/command/base.py | 5 + .../forgeshorturl/command/migrate_urls.py | 67 +++++++++++++++ ForgeShortUrl/setup.py | 6 +- 4 files changed, 83 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/aead33fc/Allura/development.ini ---------------------------------------------------------------------- diff --git a/Allura/development.ini b/Allura/development.ini index 60e3ad9..0d835b7 100644 --- a/Allura/development.ini +++ b/Allura/development.ini @@ -43,6 +43,12 @@ beaker.session.validate_key = 714bfe3612c42390726f # Used to retrieve project info from SFX for private projects sfx.secret = my-secret-key +# Used for migrate urls to ForgeShortUrl +sfx.hostedapps_db.hostname = localhost +sfx.hostedapps_db.port = 3306 +sfx.hostedapps_db.username = root +sfx.hostedapps_db.password = 0000 + # Google Analytics account for tracking # ga.account = UA-XXXXX-X http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/aead33fc/ForgeShortUrl/forgeshorturl/command/__init__.py ---------------------------------------------------------------------- diff --git a/ForgeShortUrl/forgeshorturl/command/__init__.py b/ForgeShortUrl/forgeshorturl/command/__init__.py new file mode 100644 index 0000000..e69de29 http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/aead33fc/ForgeShortUrl/forgeshorturl/command/base.py ---------------------------------------------------------------------- diff --git a/ForgeShortUrl/forgeshorturl/command/base.py b/ForgeShortUrl/forgeshorturl/command/base.py new file mode 100644 index 0000000..08ef1b2 --- /dev/null +++ b/ForgeShortUrl/forgeshorturl/command/base.py @@ -0,0 +1,5 @@ +from allura.command.base import Command + + +class ShortUrlCommand(Command): + group_name = 'ForgeShortUrl' http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/aead33fc/ForgeShortUrl/forgeshorturl/command/migrate_urls.py ---------------------------------------------------------------------- diff --git a/ForgeShortUrl/forgeshorturl/command/migrate_urls.py b/ForgeShortUrl/forgeshorturl/command/migrate_urls.py new file mode 100644 index 0000000..9d2e2d4 --- /dev/null +++ b/ForgeShortUrl/forgeshorturl/command/migrate_urls.py @@ -0,0 +1,67 @@ +import tg +from bson import ObjectId +from forgeshorturl.command.base import ShortUrlCommand +from forgeshorturl.model import ShortUrl +from allura.lib import exceptions +from allura.lib import helpers as h +from allura import model as M +from ming.orm import session +import sqlalchemy +from datetime import datetime + + +class MigrateUrls(ShortUrlCommand): + """Usage example: + + paster migrate-urls ../Allura/development.ini urls 504867768fd0920bb8c21ffd + """ + min_args = 3 + max_args = 3 + usage = ' ' + summary = ('Migrate short urls from MySQL tables to ShortUrlApp\n' + '\t - MySQL database name to load data from\n' + '\t\tthe rest of the connection information comes from ini file:\n' + '\t\t\tsfx.hostedapps_db.hostname - database hostname\n' + '\t\t\tsfx.hostedapps_db.port - database port\n' + '\t\t\tsfx.hostedapps_db.username - database username\n' + '\t\t\tsfx.hostedapps_db.password - user password\n' + '\t - the project id to load data to\n') + parser = ShortUrlCommand.standard_parser(verbose=True) + + def command(self): + self.basic_setup() + p_id = self.args[2] + p = M.Project.query.get(_id=ObjectId(p_id)) + if not p: + raise exceptions.NoSuchProjectError('The project %s ' + 'could not be found in the database' % p_id) + + db = sqlalchemy.create_engine(self._connection_string()) + meta = sqlalchemy.MetaData() + meta.bind = db + urls = sqlalchemy.Table('sfurl', meta, autoload=True) + + for row in urls.select().execute(): + url = ShortUrl.upsert(h.really_unicode(row['short_id'])) + url.url = h.really_unicode(row['url']) + url.description = h.really_unicode(row['description']) + url.private = row['private'] == 'Y' + url.created = datetime.fromtimestamp(row['create_time']) + url.last_updated = datetime.fromtimestamp(row['edit_time']) + url.project_id = p._id + user = M.User.query.get(sfx_userid=row['create_user']) + user_id = user._id if user else M.User.anonymous()._id + url.create_user = user_id + + session(ShortUrl).flush() + + def _connection_string(self): + prefix = 'sfx.hostedapps_db.' + params = { + 'host': tg.config.get(prefix + 'hostname', 'localhost'), + 'port': tg.config.get(prefix + 'port', 3306), + 'user': tg.config.get(prefix + 'username', ''), + 'pwd': tg.config.get(prefix + 'password', ''), + 'db': self.args[1] + } + return 'mysql://%(user)s:%(pwd)s@%(host)s:%(port)s/%(db)s' % params http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/aead33fc/ForgeShortUrl/setup.py ---------------------------------------------------------------------- diff --git a/ForgeShortUrl/setup.py b/ForgeShortUrl/setup.py index 641ae2f..62fc1ce 100644 --- a/ForgeShortUrl/setup.py +++ b/ForgeShortUrl/setup.py @@ -17,4 +17,8 @@ setup(name='ForgeShortUrl', entry_points=""" # -*- Entry points: -*- [allura] - ShortURL=forgeshorturl.main:ForgeShortUrlApp""",) + ShortURL=forgeshorturl.main:ForgeShortUrlApp + + [paste.paster_command] + migrate-urls=forgeshorturl.command.migrate_urls:MigrateUrls + """,)