Fix account migration
This commit is contained in:
parent
19d23668e1
commit
ba6d18d202
38
apps/account/management/commands/add_account.py
Normal file
38
apps/account/management/commands/add_account.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
from django.db.models import Q
|
||||
from establishment.management.commands.add_position import namedtuplefetchall
|
||||
from account.models import User
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add account from old db to new db'
|
||||
|
||||
def account_sql(self):
|
||||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select a.email, a.id as account_id
|
||||
from accounts as a
|
||||
where a.email is not null
|
||||
and a.email not in ('cyril@tomatic.net',
|
||||
'cyril2@tomatic.net',
|
||||
'd.sadykova@id-east.ru',
|
||||
'd.sadykova@octopod.ru',
|
||||
'n.yurchenko@id-east.ru'
|
||||
)
|
||||
and a.confirmed_at is not null
|
||||
''')
|
||||
return namedtuplefetchall(cursor)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
objects = []
|
||||
for a in self.account_sql():
|
||||
count = User.objects.filter(Q(email=a.email) | Q(old_id=a.account_id)).count()
|
||||
if count == 0:
|
||||
objects.append(User(email=a.email,
|
||||
unconfirmed_email=False,
|
||||
email_confirmed=True,
|
||||
old_id=a.account_id
|
||||
))
|
||||
User.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created accounts objects.'))
|
||||
50
apps/account/management/commands/add_social.py
Normal file
50
apps/account/management/commands/add_social.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
from django.db.models import Q
|
||||
from social_django.models import UserSocialAuth
|
||||
from establishment.management.commands.add_position import namedtuplefetchall
|
||||
from account.models import User
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add account from old db to new db'
|
||||
|
||||
def social_sql(self):
|
||||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select
|
||||
DISTINCT
|
||||
i.account_id, i.provider, i.uid
|
||||
from
|
||||
(
|
||||
select a.email, a.id as account_id
|
||||
from accounts as a
|
||||
where a.email is not null
|
||||
and a.email not in ('cyril@tomatic.net',
|
||||
'cyril2@tomatic.net',
|
||||
'd.sadykova@id-east.ru',
|
||||
'd.sadykova@octopod.ru',
|
||||
'n.yurchenko@id-east.ru'
|
||||
)
|
||||
and a.confirmed_at is not null
|
||||
) t
|
||||
join identities i on i.account_id = t.account_id
|
||||
''')
|
||||
return namedtuplefetchall(cursor)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
objects = []
|
||||
for s in self.social_sql():
|
||||
user = User.objects.filter(old_id=s.account_id)
|
||||
if user.count() > 0:
|
||||
social = UserSocialAuth.objects.filter(user=user.first(),
|
||||
provider=s.provider,
|
||||
uid=s.uid)
|
||||
if social.count() == 0:
|
||||
objects.append(UserSocialAuth(user=user.first(), provider=s.provider,
|
||||
uid=s.uid)
|
||||
)
|
||||
print('INSERT')
|
||||
|
||||
UserSocialAuth.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created social objects.'))
|
||||
|
|
@ -24,7 +24,6 @@ class Command(BaseCommand):
|
|||
return namedtuplefetchall(cursor)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
|
||||
objects = []
|
||||
for p in self.position_sql():
|
||||
count = Position.objects.filter(name={"en-GB": p.position_name}).count()
|
||||
|
|
|
|||
|
|
@ -73,6 +73,28 @@ LOGGING = {
|
|||
}
|
||||
|
||||
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.contrib.gis.db.backends.postgis',
|
||||
'NAME': os.environ.get('DB_NAME'),
|
||||
'USER': os.environ.get('DB_USERNAME'),
|
||||
'PASSWORD': os.environ.get('DB_PASSWORD'),
|
||||
'HOST': os.environ.get('DB_HOSTNAME'),
|
||||
'PORT': os.environ.get('DB_PORT'),
|
||||
},
|
||||
'legacy': {
|
||||
'ENGINE': 'django.db.backends.mysql',
|
||||
# 'HOST': '172.17.0.1',
|
||||
# 'HOST': '172.23.0.1',
|
||||
'HOST': 'mysql_db',
|
||||
'PORT': 3306,
|
||||
'NAME': 'dev',
|
||||
'USER': 'dev',
|
||||
'PASSWORD': 'octosecret123'
|
||||
}
|
||||
}
|
||||
|
||||
# ELASTICSEARCH SETTINGS
|
||||
ELASTICSEARCH_DSL = {
|
||||
'default': {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
-r base.txt
|
||||
ipdb
|
||||
ipython
|
||||
ipython
|
||||
mysqlclient==1.4.4
|
||||
Loading…
Reference in New Issue
Block a user