Created
April 10, 2019 18:45
-
-
Save berlotto/d7ee90697e363cb08f133ff37414b992 to your computer and use it in GitHub Desktop.
Campo ArrayField compatível com SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Esta classe compatibiliza o campo ArrayField do Postgres para rodar na migration do SQLite. | |
Django 1.9 | |
O correto é você testar em um banco de dados Postgres mesmo, mas para ambientes onde você execute os dois | |
testes, ajuda. | |
""" | |
from django.contrib.postgres.fields import ArrayField | |
class CustomArrayField(ArrayField): | |
"""Classe criada somente para compatibilizar o campo ArrayField do Postgres | |
com o banco de dados SQLite para fins de testes unitários em SQLite. | |
""" | |
def db_type(self, connection): | |
size = self.size or '' | |
if 'sqlite' in connection.settings_dict['ENGINE']: | |
ret = '%s[%s]' % (self.base_field.db_type(connection), size) | |
if ret and ret.endswith('[]'): | |
ret = ret[:-2] | |
else: | |
ret = '%s[%s]' % (self.base_field.db_type(connection), size) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment