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                    "{} Strict Mode is not set for database connection '{}'".format(
18                        self.connection.display_name, self.connection.alias
19                    ),
20                    hint=(
21                        "{}'s Strict Mode fixes many data integrity problems in "
22                        "{}, such as data truncation upon insertion, by "
23                        "escalating warnings into errors. It is strongly "
24                        "recommended you activate it.".format(
25                            self.connection.display_name,
26                            self.connection.display_name,
27                        ),
28                    ),
29                    id="mysql.W002",
30                )
31            ]
32        return []
33
34    def check_field_type(self, field, field_type):
35        """
36        MySQL has the following field length restriction:
37        No character (varchar) fields can have a length exceeding 255
38        characters if they have a unique index on them.
39        MySQL doesn't support a database index on some data types.
40        """
41        errors = []
42        if (
43            field_type.startswith("varchar")
44            and field.unique
45            and (field.max_length is None or int(field.max_length) > 255)
46        ):
47            errors.append(
48                preflight.Warning(
49                    "%s may not allow unique CharFields to have a max_length "
50                    "> 255." % self.connection.display_name,
51                    obj=field,
52                    id="mysql.W003",
53                )
54            )
55
56        if field.db_index and field_type.lower() in self.connection._limited_data_types:
57            errors.append(
58                preflight.Warning(
59                    "{} does not support a database index on {} columns.".format(
60                        self.connection.display_name, field_type
61                    ),
62                    hint=(
63                        "An index won't be created. Silence this warning if "
64                        "you don't care about it."
65                    ),
66                    obj=field,
67                    id="fields.W162",
68                )
69            )
70        return errors