Skip to content

Commit

Permalink
Back to Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
northeastprince committed Feb 13, 2025
1 parent 7dc1005 commit 673bc36
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 128 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Install system dependencies
env:
DEBIAN_FRONTEND: noninteractive
- name: Install dependencies
run: |
sudo apt-get install postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y
sudo apt-get update
sudo apt-get install sqlite3 libvips
sudo apt-get install libvips postgresql-client
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Setup Postgres
run: docker compose up --detach
- name: Run tests
run: bin/rails test; bin/rails test:system
env:
RAILS_ENV: test
- name: Keep screenshots from failed system tests
uses: actions/upload-artifact@v4
if: failure()
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENV RAILS_ENV="production" \
FROM base AS build

RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config libyaml-dev sqlite3 libvips
apt-get install --no-install-recommends -y build-essential git pkg-config libyaml-dev libpq-dev libvips

COPY .ruby-version Gemfile Gemfile.lock ./
RUN bundle install && \
Expand All @@ -29,7 +29,7 @@ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
FROM base

RUN apt-get update -qq && \
apt-get install --no-install-recommends -y sqlite3 libvips curl libjemalloc2 && \
apt-get install --no-install-recommends -y postgresql-client libvips curl libjemalloc2 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
Expand Down
6 changes: 1 addition & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ gem "rails", github: "rails/rails"
gem "dotenv-rails", require: "dotenv/load"

# Drivers
gem "sqlite3"
gem "pg"
gem "puma"

gem "solid_cable"

# Assets
gem "sprockets-rails"
gem "dartsass-rails"
Expand Down Expand Up @@ -54,8 +52,6 @@ gem "hashid-rails" # Non-sequential IDs
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby] # Windows doesn't include zoneinfo files
gem "bootsnap", require: false # reduces boot times through caching; required in config/boot.rb

gem "litestream"

gem "appsignal"
gem "lograge"

Expand Down
38 changes: 2 additions & 36 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,32 +260,7 @@ GEM
railties (>= 6.1)
rexml
lint_roller (1.1.0)
litestream (0.12.0)
actionpack (>= 7.0)
actionview (>= 7.0)
activejob (>= 7.0)
activesupport (>= 7.0)
logfmt (>= 0.0.10)
railties (>= 7.0)
sqlite3
litestream (0.12.0-arm64-darwin)
actionpack (>= 7.0)
actionview (>= 7.0)
activejob (>= 7.0)
activesupport (>= 7.0)
logfmt (>= 0.0.10)
railties (>= 7.0)
sqlite3
litestream (0.12.0-x86_64-linux)
actionpack (>= 7.0)
actionview (>= 7.0)
activejob (>= 7.0)
activesupport (>= 7.0)
logfmt (>= 0.0.10)
railties (>= 7.0)
sqlite3
local_time (3.0.2)
logfmt (0.0.10)
logger (1.6.5)
lograge (0.14.0)
actionpack (>= 4)
Expand Down Expand Up @@ -338,6 +313,7 @@ GEM
parser (3.3.7.0)
ast (~> 2.4.1)
racc
pg (1.5.9)
pp (0.6.2)
prettyprint
premailer (1.21.0)
Expand Down Expand Up @@ -417,11 +393,6 @@ GEM
simple_form (5.3.1)
actionpack (>= 5.2)
activemodel (>= 5.2)
solid_cable (3.0.7)
actioncable (>= 7.2)
activejob (>= 7.2)
activerecord (>= 7.2)
railties (>= 7.2)
solid_queue (1.1.3)
activejob (>= 7.1)
activerecord (>= 7.1)
Expand All @@ -436,9 +407,6 @@ GEM
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
sqlite3 (2.5.0-aarch64-linux-gnu)
sqlite3 (2.5.0-arm64-darwin)
sqlite3 (2.5.0-x86_64-linux-gnu)
standard (1.44.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
Expand Down Expand Up @@ -521,20 +489,18 @@ DEPENDENCIES
importmap-rails
jbuilder
letter_opener_web
litestream
local_time
lograge
mission_control-jobs
pg
premailer-rails
puma
rack-cors
rack-mini-profiler
rails!
simple_form
solid_cable
solid_queue
sprockets-rails
sqlite3
standard
stimulus-rails
turbo-rails
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _The thing that powers [hackathons.hackclub.com](https://hackathons.hackclub.com
## Contributing

This app is built with 🛤️ [Ruby on Rails](https://rubyonrails.org) (running [on the edge](https://shopify.engineering/living-on-the-edge-of-rails))
and uses 🥋 [Solid Queue](https://github.com/rails/solid_queue) for running background jobs.
using 🐘 [PostgreSQL](https://www.postgresql.org) for the database and 💾 [Solid Queue](https://github.com/rails/solid_queue) for running background jobs.

### Getting Started

Expand All @@ -39,6 +39,12 @@ and uses 🥋 [Solid Queue](https://github.com/rails/solid_queue) for running ba
```sh
bundle install
```

4. Start Postgres

```sh
docker compose up --detach
```

4. Setup the database and run the server

Expand All @@ -51,8 +57,11 @@ The application will now be running at [localhost:3000](http://localhost:3000)!

### Additional Dependencies

Rails 7 (Active Storage) depends on [vips](https://libvips.github.io/libvips/) to process images. You'll want this
dependency installed on your machine. For macs, run:
[Postgres will need to be installed](https://www.postgresql.org/download)
in order for the `pg` gem to be compiled.

Additionally, Active Storage depends on [vips](https://libvips.github.io/libvips/)
to process images. You'll want this dependency installed on your machine. For Macs, run:

```sh
brew install vips
Expand Down
21 changes: 14 additions & 7 deletions app/models/database_dump/processed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module DatabaseDump::Processed
included do
has_one_attached :file

after_create_commit :process_later
after_create_commit { DatabaseDumpJob.perform_later(self) }
end

def processed?
Expand All @@ -14,7 +14,7 @@ def processed?
def process
return if processed?

raise "sqlite3 not found" unless `which sqlite3`.present?
raise "pg_dump not found" unless `which pg_dump`.present?

transaction do
Tempfile.create do |io|
Expand All @@ -28,12 +28,19 @@ def process

private

def process_later
DatabaseDumpJob.perform_later(self)
def dump(tables, to:)
system postgres_env, "pg_dump --table '#{tables.join("|")}' --file #{to}", exception: true
end

def dump(tables, to:)
db = self.class.connection.raw_connection.filename
system "sqlite3 #{db} \".dump '#{tables.join("' '")}'\" > #{to}", exception: true
def postgres_env
connection = ApplicationRecord.connection_db_config.configuration_hash

{}.tap do |env|
env["PGHOST"] = connection[:host]
env["PGPORT"] = connection[:port]
env["PGUSER"] = connection[:username]
env["PGPASSWORD"] = connection[:password]
env["PGDATABASE"] = connection[:database]
end.transform_values!(&:to_s)
end
end
6 changes: 3 additions & 3 deletions app/models/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class << self
def acquire(key, limit: 1, duration: nil)
lock = nil
transaction do
lock = active.find_by(key:) || create!(key:, expiration: duration&.from_now)
lock = active.lock.find_or_create_by!(key:)

if lock.capacity >= limit
return false
else
lock.acquire
lock.update! expiration: duration&.from_now
end
end

Expand All @@ -29,8 +30,7 @@ def acquire(capacity = 1)
end

def release(quantity = 1)
transaction do
reload
with_lock do
if capacity <= 1
destroy!
else
Expand Down
7 changes: 2 additions & 5 deletions config/cable.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
development:
adapter: async
adapter: postgresql

test:
adapter: test

production:
adapter: solid_cable
connects_to:
database:
writing: cable
adapter: postgresql
27 changes: 14 additions & 13 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
default: &default
adapter: sqlite3
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
local: &local
<<: *default
database: storage/development.sqlite3
username: hackathons
host: 127.0.0.1
gssencmode: disable # https://github.com/ged/ruby-pg/issues/311#issuecomment-561927000

development:
<<: *local
database: hackathons_development

test:
<<: *default
database: storage/test.sqlite3
<<: *local
database: hackathons_test

production:
primary:
<<: *default
database: storage/production.sqlite3
cable:
<<: *default
database: storage/production_cable.sqlite3
migrations_paths: db/cable_migrate
<<: *default
database: hackathons_production
7 changes: 0 additions & 7 deletions config/initializers/litestream.rb

This file was deleted.

8 changes: 0 additions & 8 deletions config/litestream.yml

This file was deleted.

11 changes: 0 additions & 11 deletions db/cable_schema.rb

This file was deleted.

3 changes: 3 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
web:
build: .
deploy:
resources:
limits:
cpus: 1
memory: 2G
environment:
WEB_CONCURRENCY: 2
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/up"]
volumes: ["storage:/hackathons/storage"]
jobs:
volumes: ["storage:/hackathons/storage"]
build: .
depends_on: [web]
command: bin/rails solid_queue:start
deploy:
resources:
limits:
cpus: 1
memory: 1G

volumes:
storage: {}
Loading

0 comments on commit 673bc36

Please sign in to comment.