Skip to content

Commit ba2cd5d

Browse files
committed
chore: support insert ignore for postgres
1 parent fd98456 commit ba2cd5d

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

DEVELOPER_SETUP.md

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ docker-compose -f docker-compose-test-mysql.yml up --build --remove-orphans
5353
# in separate console window...
5454
docker-compose -f docker-compose-test-mysql.yml run --rm tests bash
5555
56+
# inside the tests container
57+
bundle exec rake
58+
```
59+
60+
## Running the tests with postgres
61+
62+
```
63+
docker-compose -f docker-compose-test-postgres.yml up --build --remove-orphans
64+
65+
# in separate console window...
66+
docker-compose -f docker-compose-test-postgres.yml run --rm tests bash
67+
5668
# inside the tests container
5769
bundle exec rake
5870
```

Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ RUN gem install bundler -v '~>2.0.0' \
3636
RUN echo '#!/bin/sh' >> /home/start.sh
3737
RUN echo 'bundle exec rackup -o 0.0.0.0 -p 9292' >> /home/start.sh
3838
RUN chmod +x /home/start.sh
39+
40+
RUN echo '#!/bin/sh' >> /home/init-db.sh
41+
RUN echo 'bundle exec rake db:prepare:test' >> /home/init-db.sh
42+
RUN chmod +x /home/init-db.sh
43+
3944
ENTRYPOINT bash
4045
CMD []

config/database.yml

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ test:
1818
password: postgres
1919
host: localhost
2020
port: "5433"
21+
docker_compose_postgres:
22+
adapter: postgres
23+
database: postgres
24+
username: postgres
25+
password: postgres
26+
host: postgres
27+
port: "5432"
2128
mysql:
2229
<<: *default
2330
adapter: mysql2

lib/sequel/plugins/insert_ignore.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ module ClassMethods
2525
module InstanceMethods
2626
def insert_ignore(opts = {})
2727
save(opts)
28+
load_values_from_previously_inserted_object unless id
29+
self
2830
rescue Sequel::NoExistingObject
2931
# MySQL. Ruining it for everyone.
32+
load_values_from_previously_inserted_object
33+
end
34+
35+
def load_values_from_previously_inserted_object
3036
query = self.class.insert_ignore_identifying_columns.each_with_object({}) do | column_name, q |
3137
q[column_name] = values[column_name]
3238
end
3339
self.id = model.where(query).single_record.id
34-
self.refresh
40+
refresh
3541
end
3642

3743
# Does the job for Sqlite and Postgres

0 commit comments

Comments
 (0)