From db850248599c29e7252c953d5e4043b2ef0d07ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Sat, 1 Aug 2020 15:53:56 +0200 Subject: [PATCH] =?UTF-8?q?feat(spip):=20ajoute=20la=20prise=20en=20compte?= =?UTF-8?q?=20d=20un=20pr=C3=A9fixe=20de=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 +++- drupal2spip_lal/base/checks.py | 10 +++++++--- drupal2spip_lal/base/management/commands/inspectdb.py | 8 ++++++++ drupal2spip_lal/settings/base.py | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index e58c1b3..855b804 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ # -*- mode: makefile-gmake -*- ## Définition des variables +SHELL := /bin/bash # Le nom de l'exécutable Python à utiliser ou son chemin absolu # (ex. : python ou python3). PYTHON_EXE := python3 @@ -120,8 +121,9 @@ ifeq ($(ENV), production) endif 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=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 diff --git a/drupal2spip_lal/base/checks.py b/drupal2spip_lal/base/checks.py index da8c546..721d55b 100644 --- a/drupal2spip_lal/base/checks.py +++ b/drupal2spip_lal/base/checks.py @@ -1,18 +1,22 @@ import io from difflib import unified_diff +from django.conf import settings from django.core.checks import Error, Warning, register from django.core.management import call_command @register() 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() - rc = call_command('inspectdb', '--database', dbname, stdout=buf) + rc = call_command('inspectdb', '--database', dbname, stdout=buf, **kwargs) if rc is not None: return [ Error( diff --git a/drupal2spip_lal/base/management/commands/inspectdb.py b/drupal2spip_lal/base/management/commands/inspectdb.py index 311eaf7..1a95e2e 100644 --- a/drupal2spip_lal/base/management/commands/inspectdb.py +++ b/drupal2spip_lal/base/management/commands/inspectdb.py @@ -24,6 +24,10 @@ class Command(BaseCommand): parser.add_argument( '--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( '--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 = options.get('table_name_filter') + tables_prefix = options['tables_prefix'] + 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()) with connection.cursor() as cursor: diff --git a/drupal2spip_lal/settings/base.py b/drupal2spip_lal/settings/base.py index 19bcd5b..817d5ec 100644 --- a/drupal2spip_lal/settings/base.py +++ b/drupal2spip_lal/settings/base.py @@ -66,6 +66,8 @@ DATABASES = { ), } +SPIP_TABLES_PREFIX = env('SPIP_TABLES_PREFIX', default='spip') + DATABASE_ROUTERS = ['drupal2spip_lal.base.routers.Drupal2SPIPRouter'] # URLS