feat(spip): ajoute la prise en compte d un préfixe de tables

This commit is contained in:
François Poulain 2020-08-01 15:53:56 +02:00
parent 63497d4c2e
commit db85024859
4 changed files with 20 additions and 4 deletions

View File

@ -1,5 +1,6 @@
# -*- mode: makefile-gmake -*- # -*- mode: makefile-gmake -*-
## Définition des variables ## Définition des variables
SHELL := /bin/bash
# Le nom de l'exécutable Python à utiliser ou son chemin absolu # Le nom de l'exécutable Python à utiliser ou son chemin absolu
# (ex. : python ou python3). # (ex. : python ou python3).
PYTHON_EXE := python3 PYTHON_EXE := python3
@ -120,8 +121,9 @@ ifeq ($(ENV), production)
endif endif
inspectdb: ### introspecte le schéma de base de donnée inspectdb: ### introspecte le schéma de base de donnée
$(PYTHON) manage.py inspectdb --database=drupal > drupal2spip_lal/drupal/models.py $(PYTHON) manage.py inspectdb --database=drupal > drupal2spip_lal/drupal/models.py
$(PYTHON) manage.py inspectdb --database=spip > drupal2spip_lal/spip/models.py source config.env && $(PYTHON) manage.py inspectdb --database=spip --tables-prefix=$$SPIP_TABLES_PREFIX > drupal2spip_lal/spip/models.py
## Cibles liées à l'environnement virtuel ## Cibles liées à l'environnement virtuel

View File

@ -1,18 +1,22 @@
import io import io
from difflib import unified_diff from difflib import unified_diff
from django.conf import settings
from django.core.checks import Error, Warning, register from django.core.checks import Error, Warning, register
from django.core.management import call_command from django.core.management import call_command
@register() @register()
def check_inspected_databases(app_configs, **kwargs): def check_inspected_databases(app_configs, **kwargs):
return check_inspected_db('drupal') + check_inspected_db('spip') r = []
r += check_inspected_db('drupal')
r += check_inspected_db('spip', tables_prefix=settings.SPIP_TABLES_PREFIX)
return r
def check_inspected_db(dbname): def check_inspected_db(dbname, **kwargs):
buf = io.StringIO() buf = io.StringIO()
rc = call_command('inspectdb', '--database', dbname, stdout=buf) rc = call_command('inspectdb', '--database', dbname, stdout=buf, **kwargs)
if rc is not None: if rc is not None:
return [ return [
Error( Error(

View File

@ -24,6 +24,10 @@ class Command(BaseCommand):
parser.add_argument( parser.add_argument(
'--include-partitions', action='store_true', help='Also output models for partition tables.', '--include-partitions', action='store_true', help='Also output models for partition tables.',
) )
parser.add_argument(
'--tables-prefix', nargs='?', type=str, default='',
help='Tables prefix to be stripped from model names.',
)
parser.add_argument( parser.add_argument(
'--include-views', action='store_true', help='Also output models for database views.', '--include-views', action='store_true', help='Also output models for database views.',
) )
@ -40,7 +44,11 @@ class Command(BaseCommand):
# 'table_name_filter' is a stealth option # 'table_name_filter' is a stealth option
table_name_filter = options.get('table_name_filter') table_name_filter = options.get('table_name_filter')
tables_prefix = options['tables_prefix']
def table2model(table_name): def table2model(table_name):
if tables_prefix and table_name.startswith(tables_prefix):
table_name = table_name[len(tables_prefix):]
return re.sub(r'[^a-zA-Z0-9]', '', table_name.title()) return re.sub(r'[^a-zA-Z0-9]', '', table_name.title())
with connection.cursor() as cursor: with connection.cursor() as cursor:

View File

@ -66,6 +66,8 @@ DATABASES = {
), ),
} }
SPIP_TABLES_PREFIX = env('SPIP_TABLES_PREFIX', default='spip')
DATABASE_ROUTERS = ['drupal2spip_lal.base.routers.Drupal2SPIPRouter'] DATABASE_ROUTERS = ['drupal2spip_lal.base.routers.Drupal2SPIPRouter']
# URLS # URLS