Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom PostgreSQL domains not being used in table definitions #1073

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions cms/db/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import sqlalchemy
from sqlalchemy import DDL, event, TypeDecorator, Unicode
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.compiler import compiles

from . import metadata

Expand Down Expand Up @@ -58,10 +59,6 @@ class Codename(TypeDecorator):
domain_name = "CODENAME"
impl = Unicode

@classmethod
def compile(cls, dialect=None):
return cls.domain_name

@classmethod
def get_create_command(cls):
return DDL("CREATE DOMAIN %(domain)s VARCHAR "
Expand All @@ -78,6 +75,11 @@ def get_drop_command(cls):
event.listen(metadata, "after_drop", Codename.get_drop_command())


@compiles(Codename)
def compile_codename(element, compiler, **kw):
return Codename.domain_name


class Filename(TypeDecorator):
"""Check that the column is a filename using a simple alphabet.

Expand All @@ -91,10 +93,6 @@ class Filename(TypeDecorator):
domain_name = "FILENAME"
impl = Unicode

@classmethod
def compile(cls, dialect=None):
return cls.domain_name

@classmethod
def get_create_command(cls):
return DDL("CREATE DOMAIN %(domain)s VARCHAR "
Expand All @@ -113,6 +111,11 @@ def get_drop_command(cls):
event.listen(metadata, "after_drop", Filename.get_drop_command())


@compiles(Filename)
def compile_filename(element, compiler, **kw):
return Filename.domain_name


class FilenameSchema(TypeDecorator):
"""Check that the column is a filename schema using a simple alphabet.

Expand All @@ -132,10 +135,6 @@ class FilenameSchema(TypeDecorator):
domain_name = "FILENAME_SCHEMA"
impl = Unicode

@classmethod
def compile(cls, dialect=None):
return cls.domain_name

@classmethod
def get_create_command(cls):
return DDL("CREATE DOMAIN %(domain)s VARCHAR "
Expand All @@ -150,6 +149,11 @@ def get_drop_command(cls):
context={"domain": cls.domain_name})


@compiles(FilenameSchema)
def compile_filename_schema(element, compiler, **kw):
return FilenameSchema.domain_name


event.listen(metadata, "before_create", FilenameSchema.get_create_command())
event.listen(metadata, "after_drop", FilenameSchema.get_drop_command())

Expand All @@ -170,10 +174,6 @@ class FilenameSchemaArray(TypeDecorator):
domain_name = "FILENAME_SCHEMA_ARRAY"
impl = CastingArray(Unicode)

@classmethod
def compile(cls, dialect=None):
return cls.domain_name

@classmethod
def get_create_command(cls):
# Postgres allows the condition "<sth> <op> ALL (<array>)" that
Expand Down Expand Up @@ -203,6 +203,11 @@ def get_drop_command(cls):
event.listen(metadata, "after_drop", FilenameSchemaArray.get_drop_command())


@compiles(FilenameSchemaArray)
def compile_filename_schema_array(element, compiler, **kw):
return FilenameSchemaArray.domain_name


class Digest(TypeDecorator):
"""Check that the column is a valid SHA1 hex digest.

Expand All @@ -221,10 +226,6 @@ class Digest(TypeDecorator):
# The fake digest used to mark a file as deleted in the backend.
TOMBSTONE = "x"

@classmethod
def compile(cls, dialect=None):
return cls.domain_name

@classmethod
def get_create_command(cls):
return DDL("CREATE DOMAIN %(domain)s VARCHAR "
Expand All @@ -240,3 +241,8 @@ def get_drop_command(cls):

event.listen(metadata, "before_create", Digest.get_create_command())
event.listen(metadata, "after_drop", Digest.get_drop_command())


@compiles(Digest)
def compile_digest(element, compiler, **kw):
return Digest.domain_name