From fa56196f048f07c2d0f71adeb9744336094e5939 Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Sat, 12 Jun 2021 14:14:06 +0200 Subject: [PATCH] Add PG reindex linting warning and improve some smaller stuff --- django_migration_linter/sql_analyser/base.py | 6 +++--- django_migration_linter/sql_analyser/postgresql.py | 7 +++++++ docs/incompatibilities.md | 5 +++-- tests/unit/test_sql_analyser.py | 10 ++++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/django_migration_linter/sql_analyser/base.py b/django_migration_linter/sql_analyser/base.py index e7f8ef2..584ebbc 100644 --- a/django_migration_linter/sql_analyser/base.py +++ b/django_migration_linter/sql_analyser/base.py @@ -121,15 +121,14 @@ class BaseAnalyser(object): @property def one_line_migration_tests(self): - return [test for test in self.migration_tests if test["mode"] == "one_liner"] + return (test for test in self.migration_tests if test["mode"] == "one_liner") @property def transaction_migration_tests(self): - return [test for test in self.migration_tests if test["mode"] == "transaction"] + return (test for test in self.migration_tests if test["mode"] == "transaction") def _test_sql(self, test, sql): if test["fn"](sql, errors=self.errors): - err = self.build_error_dict(migration_test=test, sql_statement=sql) if test["code"] in self.exclude_migration_tests: action = "IGNORED" list_to_add = self.ignored @@ -140,6 +139,7 @@ class BaseAnalyser(object): action = "ERROR" list_to_add = self.errors logger.debug("Testing %s -- %s", sql, action) + err = self.build_error_dict(migration_test=test, sql_statement=sql) list_to_add.append(err) else: logger.debug("Testing %s -- PASSED", sql) diff --git a/django_migration_linter/sql_analyser/postgresql.py b/django_migration_linter/sql_analyser/postgresql.py index 1d64fd2..140aba3 100644 --- a/django_migration_linter/sql_analyser/postgresql.py +++ b/django_migration_linter/sql_analyser/postgresql.py @@ -21,4 +21,11 @@ class PostgresqlAnalyser(BaseAnalyser): "mode": "one_liner", "type": "warning", }, + { + "code": "REINDEX", + "fn": lambda sql, **kw: sql.startswith("REINDEX"), + "msg": "REINDEX locks table", + "mode": "one_liner", + "type": "warning", + }, ] diff --git a/docs/incompatibilities.md b/docs/incompatibilities.md index 2518a1e..883a79f 100644 --- a/docs/incompatibilities.md +++ b/docs/incompatibilities.md @@ -39,5 +39,6 @@ You can ignore check through the `--exclude-migration-test` option and specifyin |`RUNPYTHON_MODEL_IMPORT` | Missing apps.get_model() calls for model |`RUNPYTHON_MODEL_VARIABLE_NAME` | The model variable name is different from the model class itself |`RUNSQL_REVERSIBLE` | RunSQL data migration is not reversible (missing reverse SQL) -|`CREATE_INDEX` | Creating an index without the concurrent keyword will lock the table and may generate downtime -|`DROP_INDEX` | Dropping an index without the concurrent keyword will lock the table and may generate downtime +|`CREATE_INDEX` | (Postgresql specific) Creating an index without the concurrent keyword will lock the table and may generate downtime +|`DROP_INDEX` | (Postgresql specific) Dropping an index without the concurrent keyword will lock the table and may generate downtime +|`REINDEX` | (Postgresql specific) Reindexing will lock the table and may generate downtime diff --git a/tests/unit/test_sql_analyser.py b/tests/unit/test_sql_analyser.py index 5b7e590..bb30d99 100644 --- a/tests/unit/test_sql_analyser.py +++ b/tests/unit/test_sql_analyser.py @@ -213,13 +213,13 @@ class PostgresqlAnalyserTestCase(SqlAnalyserTestCase): self.assertValidSql(sql) def test_create_index_non_concurrently(self): - sql = "CREATE INDEX ON films ((lower(titre)));" + sql = "CREATE INDEX ON films ((lower(title)));" self.assertWarningSql(sql) sql = "CREATE UNIQUE INDEX title_idx ON films (title);" self.assertWarningSql(sql) def test_create_index_concurrently(self): - sql = "CREATE INDEX CONCURRENTLY ON films ((lower(titre)));" + sql = "CREATE INDEX CONCURRENTLY ON films (lower(title));" self.assertValidSql(sql) sql = "CREATE UNIQUE INDEX CONCURRENTLY title_idx ON films (title);" self.assertValidSql(sql) @@ -231,3 +231,9 @@ class PostgresqlAnalyserTestCase(SqlAnalyserTestCase): def test_drop_index_concurrently(self): sql = "DROP INDEX CONCURRENTLY ON films;" self.assertValidSql(sql) + + def test_reindex(self): + sql = "REINDEX INDEX my_index;" + self.assertWarningSql(sql) + sql = "REINDEX TABLE my_table;" + self.assertWarningSql(sql)