How to upgrade Python Social Auth
I had an old Django project running python social auth that needed to be upgraded to Django 2, python 3 and the latest version of python social auth at the time of this writing.
These are the steps I took (adapted from here: https://github.com/python-social-auth/social-app-django/issues/28#issuecomment-405803775)
- Dump the contents of social_auth_usersocialauth into a csv file
psql -U postgres -W postgres
\COPY social_auth_usersocialauth TO ‘/Users/username/Documents/persons_db_.csv’ DELIMITER ‘,’ CSV HEADER;
- Delete all the old social auth tables
DROP TABLE social_auth_usersocialauth; DROP TABLE social_auth_nonce; DROP TABLE social_auth_code; DROP TABLE social_auth_association;
Delete all the old migrations DELETE FROM django_migrations WHERE app=‘default’; This deleted 3 rows for me
Install the new Django social or whatever it is called. Migrate as anew. python manage.py migrate
Now some of the columns are in a different order and you’ll notice there are 2 new columns (created, modified). I’ll alter the table so that importing the csv file isnt an issue
ALTER TABLE social_auth_usersocialauth ALTER COLUMN created SET DEFAULT ‘2022-03-02 19:58:19.398458+00’;
ALTER TABLE social_auth_usersocialauth ALTER COLUMN modified SET DEFAULT ‘2022-03-02 19:58:19.398458+00’;
\COPY social_auth_usersocialauth(id, user_id, provider, uid, extra_data) FROM ‘/persons_db_.csv’ DELIMITER ‘,’ CSV HEADER;
ALTER TABLE social_auth_usersocialauth ALTER COLUMN created DROP DEFAULT;
ALTER TABLE social_auth_usersocialauth ALTER COLUMN modified DROP DEFAULT;
All done :)
Update
After doing this I realized that I also need to change my pkey. This is the way you can “reset” a primary key when it is out of sync
SELECT setval(‘public.“social_auth_usersocialauth_id_seq”’, (SELECT MAX(id) FROM public.social_auth_usersocialauth) );