'Migrate the Team owner\'s Gratipay 1.0 tips into 2.0 payment instructions to the Team.
:return: ``None``
:raises: :py:exc:`~gratipay.models.team.AlreadyMigrated` if payment
instructions already exist for this Team
This method gets called under :py:func:`migrate_all_tips` during payday.'

def migrate_tips(self):

    payment_instructions = self.db.all(u'\n                                                SELECT    pi.*\n                                                        FROM    payment_instructions    pi\n                                                        JOIN    teams    t    ON    t.id    =    pi.team_id\n                                                    WHERE    t.owner    =    %s\n                                                            AND    pi.ctime    <    t.ctime\n                                ', (self.owner,))
    if payment_instructions:
        raise AlreadyMigrated
    return self.db.one(u'\n                                WITH    rows    AS    (\n\n                                                INSERT    INTO    payment_instructions\n                                                                                                (ctime,    mtime,    participant_id,    team_id,    amount,    is_funded)\n                                                                    SELECT    ct.ctime\n                                                                                        ,    ct.mtime\n                                                                                        ,    (SELECT    id    FROM    participants    WHERE    username=ct.tipper)\n                                                                                        ,    %(team_id)s\n                                                                                        ,    ct.amount\n                                                                                        ,    ct.is_funded\n                                                                            FROM    current_tips    ct\n                                                                            JOIN    participants    p    ON    p.username    =    tipper\n                                                                        WHERE    ct.tippee=%(owner)s\n                                                                                AND    p.claimed_time    IS    NOT    NULL\n                                                                                AND    p.is_suspicious    IS    NOT    TRUE\n                                                                                AND    p.is_closed    IS    NOT    TRUE\n                                                        RETURNING    1\n\n                                )    SELECT    count(*)    FROM    rows;\n                                ', {u'team_id': self.id, u'owner': self.owner})
