Skip to content

Commit

Permalink
Merge pull request #9 from cs169/187042246-dev-pg-config
Browse files Browse the repository at this point in the history
[FEAT!] Set Up Postgres For All Environments
  • Loading branch information
cynthia-lixinyi authored Feb 20, 2024
2 parents 6239213 + cc817a6 commit d4fda76
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 26 deletions.
5 changes: 2 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ gem "rails", "~> 7.1.3"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"
# Use postgres for all env dbs
gem "pg"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"
Expand Down Expand Up @@ -73,4 +73,3 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end

13 changes: 3 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ GEM
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.5)
mini_portile2 (2.8.5)
minitest (5.22.2)
msgpack (1.7.2)
multi_test (1.1.0)
Expand Down Expand Up @@ -231,6 +230,8 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
pg (1.5.5)
pg (1.5.5-x86-mingw32)
popper_js (2.11.8)
pry (0.14.2)
coderay (~> 1.1)
Expand Down Expand Up @@ -331,14 +332,6 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.7.2)
mini_portile2 (~> 2.8.0)
sqlite3 (1.7.2-aarch64-linux)
sqlite3 (1.7.2-arm-linux)
sqlite3 (1.7.2-arm64-darwin)
sqlite3 (1.7.2-x86-linux)
sqlite3 (1.7.2-x86_64-darwin)
sqlite3 (1.7.2-x86_64-linux)
stimulus-rails (1.3.3)
railties (>= 6.0.0)
stringio (3.1.0)
Expand Down Expand Up @@ -388,13 +381,13 @@ DEPENDENCIES
importmap-rails
jbuilder
jquery-rails
pg
puma (>= 5.0)
rails (~> 7.1.3)
rspec-rails
sassc-rails (~> 2.1)
simplecov
sprockets-rails
sqlite3 (~> 1.4)
stimulus-rails
turbo-rails
tzinfo-data
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# These targets were made with unix in mind. If you are not on unix, feel free to update for support.
-include .env
.DEFAULT_GOAL = dev

env:
@export DB_PORT=${DB_PORT}
@export DB_USER=${DB_USER}
@export DB_PASSWORD=${DB_PASSWORD}
@export DB_NAME=${DB_NAME}
@echo 'db environment updated'

dev: env
@yarn run dev

db-migrate: env
bin/rails db:migrate

db-seed: env
bin/rails db:seed

init: env
@command -v yarn > /dev/null 2>&1 || { echo >&2 "please install yarn first"; exit 1; }
bin/rails db:setup
bin/rails db:migrate
bin/rails db:seed
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bundle exec rails s -p 3000
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
# flextensions
Back end/API for UC Berkeley EECS "Flextensions" software

[![Maintainability](https://api.codeclimate.com/v1/badges/8d99ec9a1784ddba34ac/maintainability)](https://codeclimate.com/github/cs169/flextensions/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/8d99ec9a1784ddba34ac/test_coverage)](https://codeclimate.com/github/cs169/flextensions/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/8d99ec9a1784ddba34ac/maintainability)](https://codeclimate.com/github/cs169/flextensions/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/8d99ec9a1784ddba34ac/test_coverage)](https://codeclimate.com/github/cs169/flextensions/test_coverage)

## Installation

### Environment Variables

For the environment variables, you will need to configure on your local system (and any deployment machines) the following environment variables (recommended through a `.env` file at root):

- DB_PORT (default: 5432)
- DB_USER (default: postgres)
- DB_PASSWORD (default: password)
- DB_NAME (default: postgres)

Changing only the user and password then running `$make env` should be sufficient.

### Postgres Installation

#### MacOS

- `brew install postgresql chromedriver`
- Start postgres if necessary. `brew services start postgresql`

#### Linux/WSL

- `sudo apt install postgresql`
- Create a postgres user.
- `sudo su - postgres` (to get into postgres shell)
- `createuser --interactive --pwprompt` (in postgres shell)
- Save `DB_USER` and `DB_PASSWORD` fields in the `.env` file.
- Start postgres if necessary. `pg_ctlcluster 12 main start`
- Note: if you are using WSL2 on windows, the command to start postgres is `sudo service postgresql start`

### Stand Up Server

In order to stand up the server with [Overmind](https://github.com/DarthSim/overmind) in dev, run:

```bash
make dev
```

*NOTE:* The Overmind binary is stored in `bin/overmind` and needs to be maintained manually.
1 change: 1 addition & 0 deletions bin.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
overmind (= 2.4.0)
Binary file added bin/overmind
Binary file not shown.
32 changes: 20 additions & 12 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
adapter: postgresql
pool: 5
timeout: 5000
host: localhost
port: <%= ENV['DB_PORT'] || '5432' %>
username: <%= ENV['DB_USER'] || ENV['USER'] || 'postgres' %>
password: <%= ENV['DB_PASSWORD'] || 'password' %>
database: <%= ENV['DB_NAME'] || 'postgres' %>

development:
<<: *default
database: storage/development.sqlite3
database: flextentions_dev

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: storage/test.sqlite3
database: flextentions_test

staging:
adapter: postgresql
pool: 5
timeout: 5000
database: flextentions_stage

# MAKE SURE THE ENVIRONMENT CONFIG IS SET UP FOR PROD
production:
<<: *default
database: storage/production.sqlite3
adapter: postgresql
pool: 5
timeout: 5000
database: flextentions_prod
17 changes: 17 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 0) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

end
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "flextensions",
"version": "1.0.0",
"description": "Back end/API for UC Berkeley EECS \"Flextensions\" software",
"directories": {
"lib": "lib"
},
"scripts": {
"dev": "bin/overmind s -f Procfile.dev"
},
"author": "CS169l flextentions development team",
"license": "BSD-2-Clause"
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


0 comments on commit d4fda76

Please sign in to comment.