Implement CIText as NOCASE alternative in postgresql

master
Tim Möhlmann 6 years ago
parent 9b9f3731f6
commit 0f3c1b9d15
No known key found for this signature in database
GPG Key ID: 8677988D8072E8DE

@ -1,10 +1,11 @@
from mailu import dkim from mailu import dkim, configuration
from sqlalchemy.ext import declarative from sqlalchemy.ext import declarative
from passlib import context, hash from passlib import context, hash
from datetime import datetime, date from datetime import datetime, date
from email.mime import text from email.mime import text
from flask import current_app as app from flask import current_app as app
from citext import CIText
import flask_sqlalchemy import flask_sqlalchemy
import sqlalchemy import sqlalchemy
@ -18,6 +19,7 @@ import dns
db = flask_sqlalchemy.SQLAlchemy() db = flask_sqlalchemy.SQLAlchemy()
config = configuration.ConfigManager()
class IdnaDomain(db.TypeDecorator): class IdnaDomain(db.TypeDecorator):
@ -56,6 +58,10 @@ class IdnaEmail(db.TypeDecorator):
idna.decode(domain_name), idna.decode(domain_name),
) )
def __init__(self):
if config['DB_FLAVOR'] == 'postgresql':
self.impl = CIText()
class CommaSeparatedList(db.TypeDecorator): class CommaSeparatedList(db.TypeDecorator):
""" Stores a list as a comma-separated string, compatible with Postfix. """ Stores a list as a comma-separated string, compatible with Postfix.

@ -13,12 +13,16 @@ down_revision = '49d77a93118e'
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from flask import current_app as app from flask import current_app as app
from citext import CIText
def upgrade(): def upgrade():
if app.config['DB_FLAVOR'] == 'sqlite': if app.config['DB_FLAVOR'] == "postgresql":
email_type = CIText()
else:
email_type = sa.String(length=255, collation="NOCASE")
with op.batch_alter_table('user') as batch: with op.batch_alter_table('user') as batch:
batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE")) batch.alter_column('email', type_=email_type)
def downgrade(): def downgrade():

@ -12,15 +12,21 @@ down_revision = '9c28df23f77e'
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from flask import current_app as app
from citext import CIText
def upgrade(): def upgrade():
if app.config['DB_FLAVOR'] == "postgresql":
email_type = CIText()
else:
email_type = sa.String(length=255, collation="NOCASE")
op.create_table('token', op.create_table('token',
sa.Column('created_at', sa.Date(), nullable=False), sa.Column('created_at', sa.Date(), nullable=False),
sa.Column('updated_at', sa.Date(), nullable=True), sa.Column('updated_at', sa.Date(), nullable=True),
sa.Column('comment', sa.String(length=255), nullable=True), sa.Column('comment', sa.String(length=255), nullable=True),
sa.Column('id', sa.Integer(), nullable=False), sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_email', sa.String(length=255), nullable=False), sa.Column('user_email', email_type),
sa.Column('password', sa.String(length=255), nullable=False), sa.Column('password', sa.String(length=255), nullable=False),
sa.Column('ip', sa.String(length=255), nullable=True), sa.Column('ip', sa.String(length=255), nullable=True),
sa.ForeignKeyConstraint(['user_email'], ['user.email'], ), sa.ForeignKeyConstraint(['user_email'], ['user.email'], ),

@ -13,14 +13,30 @@ down_revision = 'c162ac88012a'
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from flask import current_app as app from flask import current_app as app
from citext import CIText
def upgrade(): def upgrade():
if app.config['DB_FLAVOR'] == 'sqlite': if app.config['DB_FLAVOR'] == "postgresql":
email_type = CIText()
with op.batch_alter_table('fetch') as batch:
batch.drop_constraint('fetch_user_email_fkey')
with op.batch_alter_table('manager') as batch:
batch.drop_constraint('manager_user_email_fkey')
else:
email_type = sa.String(length=255, collation="NOCASE")
with op.batch_alter_table('user') as batch: with op.batch_alter_table('user') as batch:
batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE")) batch.alter_column('email', type_=email_type)
with op.batch_alter_table('alias') as batch: with op.batch_alter_table('alias') as batch:
batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE")) batch.alter_column('email', type_=email_type)
with op.batch_alter_table('fetch') as batch:
batch.alter_column('user_email', type_=email_type)
if app.config['DB_FLAVOR'] == "postgresql":
batch.create_foreign_key("fetch_user_email_fkey", "user", ["user_email"], ["email"])
with op.batch_alter_table('manager') as batch:
batch.alter_column('user_email', type_=email_type)
if app.config['DB_FLAVOR'] == "postgresql":
batch.create_foreign_key("manager_user_email_fkey", "user", ["user_email"], ["email"])
def downgrade(): def downgrade():

@ -64,7 +64,7 @@ def upgrade():
sa.Column('comment', sa.String(length=255), nullable=True), sa.Column('comment', sa.String(length=255), nullable=True),
sa.Column('id', sa.Integer(), nullable=False), sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_email', sa.String(length=255), nullable=False), sa.Column('user_email', sa.String(length=255), nullable=False),
sa.Column('protocol', sa.Enum('imap', 'pop3'), nullable=False), sa.Column('protocol', sa.Enum('imap', 'pop3', name='protocol'), nullable=False),
sa.Column('host', sa.String(length=255), nullable=False), sa.Column('host', sa.String(length=255), nullable=False),
sa.Column('port', sa.Integer(), nullable=False), sa.Column('port', sa.Integer(), nullable=False),
sa.Column('tls', sa.Boolean(), nullable=False), sa.Column('tls', sa.Boolean(), nullable=False),

@ -45,3 +45,4 @@ Werkzeug==0.14.1
WTForms==2.2.1 WTForms==2.2.1
WTForms-Components==0.10.3 WTForms-Components==0.10.3
psycopg2 psycopg2
sqlalchemy-citext

@ -7,7 +7,7 @@ RUN apk add --no-cache \
RUN pip3 install jinja2 RUN pip3 install jinja2
# Image specific layers under this line # Image specific layers under this line
RUN apk add --no-cache \ RUN apk add --no-cache \
postgresql postgresql-libs \ postgresql postgresql-libs postgresql-contrib \
&& apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \ && apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \
&& pip3 install psycopg2 anosql \ && pip3 install psycopg2 anosql \
&& apk --purge del .build-deps && apk --purge del .build-deps

@ -34,3 +34,10 @@ select 1
create create
database mailu database mailu
owner mailu; owner mailu;
-- name: create_citext!
-- Install the CIText extension
create
extension
if not exists
citext;

@ -7,7 +7,7 @@ import glob
import os import os
def setup(): def setup():
conn = psycopg2.connect('user=postgres') conn = psycopg2.connect(user = 'postgres')
queries = anosql.load_queries('postgres', '/conf/queries.sql') queries = anosql.load_queries('postgres', '/conf/queries.sql')
# Mailu user # Mailu user
queries.create_mailu_user(conn) queries.create_mailu_user(conn)
@ -21,6 +21,10 @@ def setup():
queries.create_db(conn) queries.create_db(conn)
conn.set_isolation_level(1) conn.set_isolation_level(1)
conn.close() conn.close()
conn = psycopg2.connect(user = 'postgres', database= 'mailu')
queries.create_citext(conn)
conn.commit()
conn.close()
# Bootstrap the database if postgresql is running for the first time # Bootstrap the database if postgresql is running for the first time
if not os.path.exists('/var/lib/postgresql/data/pg_wal'): if not os.path.exists('/var/lib/postgresql/data/pg_wal'):

Loading…
Cancel
Save