misc: add missing python/sql migrations to replaced migrations (#40685)

This commit is contained in:
Benjamin Dauvergne 2023-12-14 22:53:42 +01:00
parent 4e881c1c16
commit 589eecdaae
3 changed files with 70 additions and 0 deletions

View File

@ -9,6 +9,11 @@ import authentic2.utils.evaluate
import authentic2.utils.template
def create_login_password_authenticator(apps, schema_editor):
LoginPasswordAuthenticator = apps.get_model('authenticators', 'LoginPasswordAuthenticator')
LoginPasswordAuthenticator.objects.create(slug='password-authenticator', enabled=True)
class Migration(migrations.Migration):
replaces = [
('authenticators', '0001_initial'),
@ -315,4 +320,5 @@ class Migration(migrations.Migration):
},
bases=('authenticators.baseauthenticator',),
),
migrations.RunPython(create_login_password_authenticator, reverse_code=migrations.RunPython.noop),
]

View File

@ -339,4 +339,20 @@ class Migration(migrations.Migration):
verbose_name='allowed services for this profile type',
),
),
migrations.RunSQL(
sql=[
'CREATE EXTENSION IF NOT EXISTS unaccent SCHEMA public',
'CREATE EXTENSION IF NOT EXISTS pg_trgm SCHEMA public',
'CREATE OR REPLACE FUNCTION public.immutable_unaccent(text) RETURNS varchar AS $$ SELECT'
" public.unaccent('public.unaccent',$1::text); $$ LANGUAGE 'sql' IMMUTABLE",
'CREATE INDEX custom_user_name_gist_idx ON custom_user_user USING gist'
" (LOWER(public.immutable_unaccent(first_name || ' ' || last_name)) public.gist_trgm_ops)",
],
reverse_sql=[
'DROP INDEX IF EXISTS custom_user_name_gist_idx',
'DROP FUNCTION IF EXISTS public.immutable_unaccent(text)',
'DROP EXTENSION IF EXISTS pg_trgm',
'DROP EXTENSION IF EXISTS unaccent',
],
),
]

View File

@ -7,6 +7,49 @@ from django.db import migrations, models
import authentic2.a2_rbac.utils
def create_trigger(apps, schema_editor):
with schema_editor.connection.cursor() as cursor:
cursor.execute('SHOW default_text_search_config')
assert cursor.fetchone()
cursor.execute(
'''CREATE OR REPLACE FUNCTION authentic2_update_atv_search_vector() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' OR (TG_OP = 'UPDATE' AND NEW.content <> OLD.content) THEN
NEW.search_vector = to_tsvector(NEW.content);
END IF;
RETURN NEW;
END; $$ LANGUAGE plpgsql'''
)
cursor.execute(
'''CREATE TRIGGER authentic2_attributevalue_search_vector_trigger
BEFORE INSERT OR UPDATE OF content
ON authentic2_attributevalue
FOR EACH ROW EXECUTE PROCEDURE authentic2_update_atv_search_vector()'''
)
def drop_trigger(apps, schema_editor):
with schema_editor.connection.cursor() as cursor:
cursor.execute(
'DROP TRIGGER IF EXISTS authentic2_attributevalue_search_vector_trigger ON'
' authentic2_attributevalue'
)
cursor.execute('DROP FUNCTION IF EXISTS authentic2_update_atv_search_vector')
def initialize_services_runtime_settings(apps, schema_editor):
from authentic2.utils.misc import RUNTIME_SETTINGS
Setting = apps.get_model('authentic2', 'Setting')
for key, data in RUNTIME_SETTINGS.items():
Setting.objects.get_or_create(
key=key,
defaults={
'value': data['value'],
},
)
class Migration(migrations.Migration):
replaces = [
('authentic2', '0001_initial'),
@ -241,4 +284,9 @@ class Migration(migrations.Migration):
name='attributevalue',
unique_together={('content_type', 'object_id', 'attribute', 'multiple', 'content')},
),
migrations.RunPython(create_trigger, drop_trigger),
migrations.RunPython(
initialize_services_runtime_settings,
reverse_code=migrations.RunPython.noop,
),
]