1
0
mirror of https://github.com/Mailu/Mailu.git synced 2025-01-14 02:34:22 +02:00

Implement CIText as NOCASE alternative in postgresql

This commit is contained in:
Tim Möhlmann 2018-11-20 14:41:17 +02:00
parent 9b9f3731f6
commit 0f3c1b9d15
No known key found for this signature in database
GPG Key ID: 8677988D8072E8DE
9 changed files with 60 additions and 16 deletions

View File

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

View File

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

View File

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

View File

@ -13,14 +13,30 @@ down_revision = 'c162ac88012a'
from alembic import op
import sqlalchemy as sa
from flask import current_app as app
from citext import CIText
def upgrade():
if app.config['DB_FLAVOR'] == 'sqlite':
with op.batch_alter_table('user') as batch:
batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE"))
with op.batch_alter_table('alias') as batch:
batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE"))
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:
batch.alter_column('email', type_=email_type)
with op.batch_alter_table('alias') as batch:
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():

View File

@ -64,7 +64,7 @@ def upgrade():
sa.Column('comment', sa.String(length=255), nullable=True),
sa.Column('id', sa.Integer(), 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('port', sa.Integer(), nullable=False),
sa.Column('tls', sa.Boolean(), nullable=False),

View File

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

View File

@ -7,7 +7,7 @@ RUN apk add --no-cache \
RUN pip3 install jinja2
# Image specific layers under this line
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 \
&& pip3 install psycopg2 anosql \
&& apk --purge del .build-deps

View File

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

View File

@ -7,7 +7,7 @@ import glob
import os
def setup():
conn = psycopg2.connect('user=postgres')
conn = psycopg2.connect(user = 'postgres')
queries = anosql.load_queries('postgres', '/conf/queries.sql')
# Mailu user
queries.create_mailu_user(conn)
@ -21,6 +21,10 @@ def setup():
queries.create_db(conn)
conn.set_isolation_level(1)
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
if not os.path.exists('/var/lib/postgresql/data/pg_wal'):