Esempio n. 1
0
def apply_alter(reverse=False):
    with connection.cursor() as cursor:
        pprint(settings.CONSTRAINT_MIGRATIONS)
        for mig in settings.CONSTRAINT_MIGRATIONS:
            table = mig['table']
            for c in mig['columns']:
                cursor.execute(
                    'select *'
                    'from information_schema.KEY_COLUMN_USAGE '
                    'where TABLE_NAME = %s and REFERENCED_TABLE_NAME = %s and COLUMN_NAME = %s and '
                    'REFERENCED_TABLE_SCHEMA = %s',
                    [table,
                     c['new_ref_table'] if reverse else c['origin_ref_table'],
                     c.get('new_name', c['origin_name']) if reverse else c['origin_name'],
                     cursor.db.settings_dict['NAME']])
                rows = dictfetchall(cursor)
                if rows:
                    constraint_name = rows[0]['CONSTRAINT_NAME']
                    ref_field = rows[0]['REFERENCED_COLUMN_NAME']
                    # drop constrain first
                    cursor.execute('ALTER TABLE `%s` DROP FOREIGN KEY `%s`; ' %
                                   (table,
                                    constraint_name))

                    if 'new_name' in c:
                        cursor.execute('SHOW COLUMNS FROM `%s` WHERE Field = \'%s\'' %
                                       (table,
                                        c['new_name'] if reverse else c['origin_name']))
                        c_type = dictfetchall(cursor)[0]['Type']
                        cursor.execute('ALTER TABLE `%s` CHANGE %s %s %s' % (
                            table,
                            c['new_name'] if reverse else c['origin_name'],
                            c['origin_name'] if reverse else c['new_name'],
                            c_type
                        ))

                    # add constrain
                    cursor.execute('ALTER TABLE `%s`  ADD CONSTRAINT `%s` '
                                   'FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`);' %
                                   (table,
                                    constraint_name,
                                    c['origin_name'] if reverse else c.get('new_name', c['origin_name']),
                                    c['origin_ref_table'] if reverse else c['new_ref_table'],
                                    c.get('ref_field', ref_field)))
Esempio n. 2
0
def forward_func(apps, schema_editor):
    if not old_uer_table_exist():
        return

    User = apps.get_model('account', 'User')
    UserProperty = apps.get_model('account', 'UserProperty')

    db_alias = schema_editor.connection.alias
    with connection.cursor() as cursor:
        cursor.execute('select * from %s' %
                       getattr(settings, 'USER_TABLE', 'account_bkuser'))
        bk_users = dictfetchall(cursor)
        cursor.execute(
            'select * from %s' %
            getattr(settings, 'USER_GROUP_TABLE', 'account_bkuser_groups'))
        groups = dictfetchall(cursor)
        cursor.execute('select * from %s' %
                       getattr(settings, 'USER_PERMISSION_TABLE',
                               'account_bkuser_user_permissions'))
        permissions = dictfetchall(cursor)

    users = []
    user_properties = []

    for row in bk_users:

        try:
            user = User.objects.using(db_alias).get(username=row['username'])
        except Exception:
            user = User(id=row[fields_map['id']],
                        username=row[fields_map['username']],
                        nickname=row[fields_map['nickname']],
                        is_staff=row[fields_map['is_staff']],
                        is_active=True,
                        is_superuser=row[fields_map['is_superuser']],
                        date_joined=row[fields_map['date_joined']])
            users.append(user)

        for key in additional_key:
            user_properties.append(
                UserProperty(user=user, key=key, value=row[key] or ''))

    group_values = []
    for row in groups:
        group_values.append('(%s, %s, %s)' %
                            (row['id'], row['bkuser_id'], row['group_id']))

    permission_values = []
    for row in permissions:
        permission_values.append(
            '(%s, %s, %s)' %
            (row['id'], row['bkuser_id'], row['permission_id']))

    with transaction.atomic():
        User.objects.bulk_create(users)
        UserProperty.objects.bulk_create(user_properties)
        with connection.cursor() as cursor:
            if group_values:
                cursor.execute(
                    'insert into `account_user_groups` (id, user_id, group_id) values %s;'
                    % ','.join(group_values))
            if permission_values:
                cursor.execute(
                    'insert into `account_user_permissions` (id, user_id, permission_id) values %s;'
                    % ','.join(permission_values))