Rideshare is a Rails API app that implements a portion of a fictional Ridesharing service.
This repo started as a side project back in 2019 to demonstrate good practices.
In 2022, it took on a new purpose as the Rails application for exercises and examples in High Performance PostgreSQL for Rails.
Rideshare proudly runs on PostgreSQL. ๐
To set up Rideshare for development, please read the Prerequisites and Installation Steps sections below.
These steps involve setting up your development machine with all the runtime dependencies, and then setting up your local PostgreSQL instance.
For Rideshare, you'll need Ruby and PostgreSQL. First, work on Ruby.
- Ruby
3.2.2
(check.ruby-version
)- Use a Ruby version manager like rbenv
- Bundler. After installing Ruby, run
gem install bundler
. - (optional) To generate a DB image Install graphiz
brew install graphviz
Front-end technologies were removed because this is an API only app.
importmap-rails
does not require yarn, npm etc. https://fly.io/ruby-dispatch/making-sense-of-rails-assets/
- On macOS, Postgres.app is the recommended way to install it
- Currently on PostgreSQL 16 (2023)
- run
export RIDESHARE_DB_PASSWORD=<secret value here>
before running setup scripts (see below) - Configures PostgreSQL following My GOTO Postgres Configuration for Web Services
- Configuration uses a series of SQL scripts in
db
directory, run frompsql
via a shell script (see next step) - Run
sh db/setup.sh
- expects
DATABASE_URL
is set - Migrations will first run
SET role = owner
to run Migrations asowner
, which will own the tables, checklib/tasks/migration_hooks.rake
-
Install the Prerequisites
-
cd
into your source code directory e.g.:~/Projects
-
From there, clone the repo:
git clone https://github.com/andyatkinson/rideshare.git
-
cd rideshare
Install all the Rideshare gems by running
bundle install
-
If all gems were installed, you're ready to set up the database.
You should have PostgreSQL installed but no database for the app created yet.
Normally you'd run
bin/rails db:create
to create databases, but Rideshare has a custom setup.On macOS run, for a brand new installation:
export RIDESHARE_DB_PASSWORD=$(openssl rand -hex 12)
For existing credentials (upgrades), set RIDESHARE_DB_PASSWORD using the password defined in ~/.pgpass.
This sets an environment variable with a value like '2C6uw3LprgUMwSLQ' that will be used when creating roles.
Create the file
~/.pgpass
and refer to the sample file in thepostgresql
directory of this project, filling in your details.Once that's set, run the following script to set up the databases, roles, grants, and more.
sh db/setup.sh
DATABASE_URL
should be set, which is used locally inconfig/database.yml
. Use theowner
role which is a readwrite role, and should have permissions to modify the schema, creating tables in therideshare
PostgreSQL schema.export DATABASE_URL="postgres://owner:@localhost:5432/rideshare_development"
Try connecting with
psql $DATABASE_URL
and run\dn
from psql. You should see therideshare
schema and all the tables created in that schema.Run pending migrations. You may need to install graphviz (see above) to update the ERD.
bin/rails db:migrate
Below is sample ~/.pgpass
file.
cat ~/.pgpass
localhost:5432:rideshare_development:owner:HSTnDDgFtyW9fyFI
localhost:5432:rideshare_development:app:HSTnDDgFtyW9fyFI
Authenticate without the password (supplied from from ~/.pgpass
)
psql $DATABASE_URL
Or:
psql -U owner -d rideshare_development
For development, you'll use some good practices in PostgreSQL like a custom app schema and app user, with reduced explicitly granted privileges.
For the test database, you'll keep it simpler. Use the postgres superuser and the public schema. The test configuration is also used for Circle CI.
-
Run
sh db/setup_test_database.sh
to set uprideshare_test
-
Run
bin/rails test
Refer to .circleci/config.yml
for the Circle CI config.
In addition to this Readme, Development Guides go into greater depth for setting up your machine for Rideshare development.
For Guides specific to this repo, check Guides.
Although Rideshare is an API-only app, there are some UI elements.
Rideshare proudly runs PgHero, which can be reached at http://localhost:3000/pghero