Plain is headed towards 1.0! Subscribe for development updates →

 1from plain import preflight
 2from plain.models.backends.base.validation import BaseDatabaseValidation
 3
 4
 5class DatabaseValidation(BaseDatabaseValidation):
 6    def check(self, **kwargs):
 7        issues = super().check(**kwargs)
 8        issues.extend(self._check_sql_mode(**kwargs))
 9        return issues
10
11    def _check_sql_mode(self, **kwargs):
12        if not (
13            self.connection.sql_mode & {"STRICT_TRANS_TABLES", "STRICT_ALL_TABLES"}
14        ):
15            return [
16                preflight.Warning(
17                    f"{self.connection.display_name} Strict Mode is not set for the database connection",
18                    hint=(
19                        f"{self.connection.display_name}'s Strict Mode fixes many data integrity problems in "
20                        f"{self.connection.display_name}, such as data truncation upon insertion, by "
21                        "escalating warnings into errors. It is strongly "
22                        "recommended you activate it.",
23                    ),
24                    id="mysql.W002",
25                )
26            ]
27        return []
28
29    def check_field_type(self, field, field_type):
30        """
31        MySQL has the following field length restriction:
32        No character (varchar) fields can have a length exceeding 255
33        characters if they have a unique index on them.
34        MySQL doesn't support a database index on some data types.
35        """
36        errors = []
37        if (
38            field_type.startswith("varchar")
39            and field.primary_key
40            and (field.max_length is None or int(field.max_length) > 255)
41        ):
42            errors.append(
43                preflight.Warning(
44                    f"{self.connection.display_name} may not allow unique CharFields to have a max_length "
45                    "> 255.",
46                    obj=field,
47                    id="mysql.W003",
48                )
49            )
50
51        return errors