Skip to content

Commit

Permalink
Fixes Issue 15: case insensitive mysql short code search takes very long
Browse files Browse the repository at this point in the history
1. fix bug when getting latest short code using order by link.id
2. fix bug when comparing two short code using collate
  • Loading branch information
Hu1-Li authored and Amit Tripathi committed May 27, 2018
1 parent 863fc7a commit 67260ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center"><img src="src/pyui/static/logo/logov2.png" alt="pygmy" height="200px"></p>

Pygmy
=====

<p align="center"><img src="src/pyui/static/logo/logov2.png" alt="pygmy" height="200px"></p>

[![Build Status](https://travis-ci.org/amitt001/pygmy.svg?branch=master)](https://travis-ci.org/amitt001/pygmy) [![Coverage Status](https://coveralls.io/repos/github/amitt001/pygmy/badge.svg?branch=master)](https://coveralls.io/github/amitt001/pygmy?branch=master) [![Requirements Status](https://requires.io/github/amitt001/pygmy/requirements.svg?branch=master)](https://requires.io/github/amitt001/pygmy/requirements/?branch=master)

Live version of this project @ [https://pygy.co](https://pygy.co)
Expand Down Expand Up @@ -90,20 +90,26 @@ DB Setup:
Use MySQL
---------

First install `pymysql`:

`pip install pymysql`

Check correct port:

`mysqladmin variables | grep port`

Enter below line in src/pygmy/core/pygmy.cfg fro database->url value
Change below line in `src/pygmy/core/pygmy.cfg`:

`mysql+pymysql://root:root@127.0.0.1:3306/pygmy`
```
engine: mysql
url: mysql+pymysql://root:root@127.0.0.1:3306/pygmy
```

Enter MySQL URL

`CREATE DATABASE pygmy;`

Note: Better using Mysql with version > `5.6.5` to use default value of `CURRENT_TIMESTAMP` for `DATETIME`.

Use Postgresql
--------------
Expand Down
3 changes: 3 additions & 0 deletions src/pygmy/database/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pygmy.config import config
from pygmy.database.sqlite import SqliteDatabase
from pygmy.database.postgresql import PostgreSQLDatabase
from pygmy.database.mysql import MySQLDatabase
from pygmy.database.base import Model


Expand All @@ -15,6 +16,8 @@ def create():
database = SqliteDatabase()
elif config.database['engine'] == 'postgresql':
database = PostgreSQLDatabase()
elif config.database['engine'] == 'mysql':
database = MySQLDatabase()
else:
raise Exception(
"Unsupported DB type. Supported types are "
Expand Down
5 changes: 5 additions & 0 deletions src/pygmy/database/mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pygmy.database.base import BaseDatabase


class MySQLDatabase(BaseDatabase):
pass
15 changes: 13 additions & 2 deletions src/pygmy/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import binascii
import datetime

from sqlalchemy import event, and_, or_
from sqlalchemy import event, and_, or_, DDL
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import relationship
from sqlalchemy import (Column, String, Integer, Boolean,
Expand Down Expand Up @@ -67,6 +67,17 @@ def generate_short_code(_, connection, target):

event.listen(Link, 'after_insert', Link.generate_short_code)

# adding event to modify field `short_code` when using mysql
event.listen(
Link.__table__,
"after_create",
DDL(
"alter table {table} modify short_code varchar(6) "
"CHARACTER SET utf8 COLLATE utf8_bin default null".format(
table=Link.__tablename__
)
).execute_if(dialect='mysql')
)

class LinkManager:
"""Link model manager"""
Expand Down Expand Up @@ -244,7 +255,7 @@ def latest_default_link(self, db):
Link.is_custom.is_(False),
Link.short_code.isnot(None),
Link.short_code != ''
)).order_by(Link.created_at.desc()).first()
)).order_by(Link.id.desc()).first()

@dbconnection
def find(self, db, **kwargs):
Expand Down

0 comments on commit 67260ba

Please sign in to comment.