<commit_before>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 - <PASSWORD>'
            '\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.utcfromtimestamp(row['create_time'])
            url.last_updated = datetime.utcfromtimestamp(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<commit_msg>[#4785] Fixes for changes from [#4637]<commit_after>import tg
from pylons import tmpl_context as c
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]
        mount_point = 'url'
        c.project = M.Project.query.get(_id=ObjectId(p_id))
        if not c.project:
            raise exceptions.NoSuchProjectError('The project %s '
                    'could not be found in the database' % p_id)
        c.app = c.project.app_instance(mount_point)
        assert c.app, 'Project does not have ShortURL app installed'

        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.full_url = h.really_unicode(row['url'])
            url.description = h.really_unicode(row['description'])
            url.private = row['private'] == 'Y'
            url.created = datetime.utcfromtimestamp(row['create_time'])
            url.last_updated = datetime.utcfromtimestamp(row['edit_time'])
            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
{'fraction_non_alphanumeric': 0.10538493899873791, 'fraction_numerical': 0.009676062263357174, 'mean_word_length': 2.635321100917431, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885268', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1432, 'n_words': 360}