Add internal classes and add type hints + mypy.

This commit is contained in:
David Wobrock authored and David Wobrock committed 2022-12-26 16:26:24 +01:00
1 parent 847ac1241a
commit 2ee7dac3b3
88 files changed
+603 -340

No files matched your search

+27 -21
View File
@@ -1,31 +1,35 @@
from __future__ import annotations
import re
from .base import BaseAnalyser
from .base import BaseAnalyser, Check, CheckMode, CheckType
class SqliteAnalyser(BaseAnalyser):
migration_tests = [
{
"code": "RENAME_TABLE",
"fn": lambda sql, **kw: re.search("ALTER TABLE .* RENAME TO", sql)
migration_checks: list[Check] = [
Check(
code="RENAME_TABLE",
fn=lambda sql, **kw: re.search("ALTER TABLE .* RENAME TO", sql)
and "__old" not in sql
and "new__" not in sql,
"type": "error",
},
{
"code": "DROP_TABLE",
message="RENAMING tables",
mode=CheckMode.ONE_LINER,
type=CheckType.ERROR,
),
Check(
code="DROP_TABLE",
# TODO: improve to detect that the table names overlap
"fn": lambda sql_statements, **kw: any(
fn=lambda sql_statements, **kw: any(
sql.startswith("DROP TABLE") for sql in sql_statements
)
and not any(sql.startswith("CREATE TABLE") for sql in sql_statements),
"msg": "DROPPING table",
"mode": "transaction",
"type": "error",
},
{
"code": "NOT_NULL",
"fn": lambda sql_statements, **kw: any(
message="DROPPING table",
mode=CheckMode.TRANSACTION,
type=CheckType.ERROR,
),
Check(
code="NOT_NULL",
fn=lambda sql_statements, **kw: any(
re.search("NOT NULL(?! PRIMARY)(?! DEFAULT)", sql)
for sql in sql_statements
)
@@ -34,13 +38,14 @@ class SqliteAnalyser(BaseAnalyser):
and ("__old" in sql or "new__" in sql)
for sql in sql_statements
),
"mode": "transaction",
"type": "error",
},
message="NOT NULL constraint on columns",
mode=CheckMode.TRANSACTION,
type=CheckType.ERROR,
),
]
@staticmethod
def detect_table(sql):
def detect_table(sql: list[str] | str) -> str | None:
if isinstance(sql, str):
regex_result = re.search("TABLE [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
if regex_result:
@@ -48,3 +53,4 @@ class SqliteAnalyser(BaseAnalyser):
regex_result = re.search("ON [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
if regex_result:
return regex_result.group(1)
return None