Skip to content

Commit

Permalink
Add postgresql support.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Aug 23, 2016
1 parent ffc3e41 commit d8b36d1
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ gem 'haml'
gem 'sass'
gem 'sequel'

gem 'mysql2'
group :mysql do
gem 'mysql2'
end

group :postgresql do
gem 'pg'
end

gem 'redis'
gem 'em-hiredis'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GEM
hiredis (0.5.2)
multi_json (1.10.1)
mysql2 (0.3.16)
pg (0.18.4)
rack (1.5.2)
rack-protection (1.5.3)
rack
Expand Down Expand Up @@ -46,6 +47,7 @@ DEPENDENCIES
em-hiredis
haml
mysql2
pg
redis
sass
sequel
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Installation

1. Make sure all dependencies are installed and configured.
2. Create a MySQL database and import the schema from `config/sql/mysql-schema.sql`, or, create a PostgreSQL database and import the schema from `config/sql/postgresql-schema.sql`.
3. Run `bundle install --without postgresql` if you use MySQL, or `bundle install --without mysql` if you use PostgreSQL.
3. Copy `config/application.yml.example` to `config/application.yml`.
4. Edit `config/application.yml`. The fields should be self-documenting.
6. Copy `config/nginx.conf.example` to `/etc/nginx/sites-enabled/irclogger`. Edit the `server_name`, `root` and `upstream` directives to match your setup.
Expand Down
3 changes: 2 additions & 1 deletion config/application.yml.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
database: mysql2://root@localhost/irclogs
# database: postgres:///irclogs
redis: redis://localhost:6379

server: irc.freenode.net
Expand All @@ -8,4 +9,4 @@ realname: whitequark's logger bot
nickname: logger_test
channels:
- "#irclogger-test"
domain: example.com
domain: example.com
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions config/sql/postgresql-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DROP TABLE IF EXISTS irclog;
CREATE TABLE irclog (
id SERIAL,
channel VARCHAR(30),
nick VARCHAR(40),
opcode VARCHAR(20),
timestamp INT,
line TEXT,
oper_nick VARCHAR(40),
payload TEXT,
PRIMARY KEY(id)
);

CREATE INDEX irclog_channel_timestamp_index ON irclog (channel, timestamp);
CREATE INDEX irclog_channel_opcode_index ON irclog (channel, opcode);
CREATE INDEX irclog_channel_nick_index ON irclog (channel, nick);
CREATE INDEX irclog_fulltext_index ON irclog
USING gin(to_tsvector('english', nick || ' ' || line));
14 changes: 11 additions & 3 deletions lib/irclogger/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,17 @@ def self.find_by_channel_and_kickban(channel, query)
end

def self.find_by_channel_and_fulltext(channel, query)
order(:timestamp).reverse.filter(:channel => channel).
filter('opcode is null').
filter('match (nick, line) against (? in boolean mode)', query)
dataset = order(:timestamp).reverse.filter(:channel => channel).
filter('opcode is null')
case DB.database_type
when :mysql2
dataset.filter('match (nick, line) against (? in boolean mode)', query)
when :postgres
dataset.filter("to_tsvector('english', nick || ' ' || line) @@ " \
"plainto_tsquery('english', ?)", query)
else
raise NotImplementedError
end
end

def self.find_by_channel_and_nick(channel, query)
Expand Down
7 changes: 4 additions & 3 deletions views/help/search.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
How to search:

%ul
%li
You can use standard <a href='http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html'>MySQL fulltext search</a> operations.
- if DB.database_type == :mysql2
%li
You can use standard <a href='http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html'>MySQL fulltext search</a> operations.

%li
You can fetch a history of kicks and bans of a particular user by searching for "kickban:username".

%li
You can fetch a list of all messages by a particular user by searching for "nick:username".
You can fetch a list of all messages by a particular user by searching for "nick:username".

0 comments on commit d8b36d1

Please sign in to comment.