You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2012/10/02 18:36:14 UTC

[6/6] git commit: [#4785] ticket:163 add migrate-urls command

[#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/cj/4785
Commit: aead33fc0bf25d13c04f4364aa0326e2305b2fa5
Parents: 0298d85
Author: Igor Bondarenko <je...@gmail.com>
Authored: Fri Sep 7 12:52:19 2012 +0300
Committer: Cory Johns <jo...@geek.net>
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 = '<ini file> <database name> <project>'
+    summary = ('Migrate short urls from MySQL tables to ShortUrlApp\n'
+            '\t<database name> - 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<project> - 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
+      """,)