From 00aced0ec5ad9520614ba1846d061f9605ace758 Mon Sep 17 00:00:00 2001 From: "Mauro S. M. Rodrigues" Date: Mon, 11 Feb 2013 14:38:34 -0500 Subject: Canonizes IPv6 before insert it into the db This is normalize IPv6 to be inserted always in shortened and no mixed form into db, this way postgresql, which uses INET type, and other databases like mysql will have equivalent contents. Fix bug 1116236 Related to bp migration-testing-with-data Change-Id: Iae5aa8a28e3ccc0d3a1a96459232b827c3a19d5c --- nova/db/sqlalchemy/types.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/types.py b/nova/db/sqlalchemy/types.py index 275e61a4c..ef861b832 100644 --- a/nova/db/sqlalchemy/types.py +++ b/nova/db/sqlalchemy/types.py @@ -18,9 +18,21 @@ """Custom SQLAlchemy types.""" from sqlalchemy.dialects import postgresql -from sqlalchemy import String +from sqlalchemy import types +from nova import utils -def IPAddress(): + +class IPAddress(types.TypeDecorator): """An SQLAlchemy type representing an IP-address.""" - return String(39).with_variant(postgresql.INET(), 'postgresql') + impl = types.String(39).with_variant(postgresql.INET(), 'postgresql') + + def process_bind_param(self, value, dialect): + """Process/Formats the value before insert it into the db.""" + if dialect.name == 'postgresql': + return value + # NOTE(maurosr): The purpose here is to convert ipv6 to the shortened + # form, not validate it. + elif utils.is_valid_ipv6(value): + return utils.get_shortened_ipv6(value) + return value -- cgit