lino icon indicating copy to clipboard operation
lino copied to clipboard

Enabling social-auth breaks manage.py prep

Open telenieko opened this issue 6 years ago • 8 comments

Hi there, I'm on python 3.6.8, lino==19.2.2, lino-xl==19.2.0;

When I follow the social authentication page on the book mange.py prep won't work anymore.

In my Site class I do: social_auth_backends = [ 'social_core.backends.google.GoogleOAuth2', ], and then set the appropiate settings for the backend and when running prep this comes:

`initdb demo_users` started on database test.
Operations to perform:
  Apply all migrations: contenttypes, sessions, social_django
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying sessions.0001_initial... OK
  Applying social_django.0001_initial...

Traceback (most recent call last):
  File "lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "users_user" does not exist

The furthest I came is that initdb does first the migrations and after migrations it does the non-migration capable applications. What happens (I think) is that modlib.users is not migration capable while social_django is.

So tables for social_django are created before modlib.users tables even exist, hence the exception.

telenieko avatar Feb 28 '19 23:02 telenieko

Oh, I see psycopg2. And does it work with the default DATABASES settings (which use sqlite)?

lsaffre avatar Mar 01 '19 17:03 lsaffre

Yes, sqlite works. Note that to setup DATABASES what I'm doing is that I override Site.get_database_settings() to make it return None. Looking at the code it seemed like the cleanest way to have DATABASES untouched.

And I do not think that caused any bad side effects, but probably I'm wrong.

I'm doing so because I use django-environ to configure my settings (I plan to use Heroku and/or Docker to deploy).

telenieko avatar Mar 01 '19 19:03 telenieko

Hi @telenieko , Take a look at this demo project, it uses social_django. https://github.com/lino-framework/book/blob/master/lino_book/projects/team/settings/demo.py And In the case you want to use postgresql instead of mysql or sqlite ,try just to edit demo.py and add missing DATABASES settings as required by setting.py in any regular Django project.

khchine5 avatar Mar 01 '19 20:03 khchine5

Also if you want to have DATABASES untouched, just define it after the line SITE = Site(...) in your settings.py file.

lsaffre avatar Mar 02 '19 17:03 lsaffre

We didn't yet use neither django-environ nor Heroku nor Docker so far. And I guess that we should do so. It's just a lack of time. Maybe one day you should give us some lessons...

lsaffre avatar Mar 02 '19 19:03 lsaffre

Up to now what I've found is that SQLite works but psycopg2 does not. python manage.py prep produces different results on each database, which might be a good clue:

On SQLite:

Operations to perform:
  Synchronize unmigrated apps: about, accounts, auth, bootstrap3, clients, contacts, countries, extjs, jinja, lino, office, printing, staticfiles, system, users, xl
  Apply all migrations: contenttypes, sessions, social_django
Synchronizing apps without migrations:

On PostgreSQL:

Operations to perform:
  Apply all migrations: contenttypes, sessions, social_django
Running migrations:

Crearly something's off there. Will keep investigatin.

telenieko avatar Mar 04 '19 09:03 telenieko

Indeed, Lino's initdb command is not tested with PostgreSQL: https://github.com/lino-framework/lino/blob/master/lino/management/commands/initdb.py You probably need to change something in that code. But take care to change it only for postgresql.

lsaffre avatar Mar 05 '19 16:03 lsaffre

From a quick glance to that file:

if engine == 'django.db.backends.postgresql':
            # a first time to create tables of contenttypes. At
            # least on PostgreSQL this is required because for
            # some reason the syncdb fails when contenttypes is
            # not initialized.
            call_command('migrate', **options)
call_command('migrate', '--run-syncdb', **options)

I'm willing to bet the problem is there. That running migrate first prevents the "--run-syncdb" to have any effect later. Will do some testing later

telenieko avatar Mar 05 '19 17:03 telenieko