Skip to content

Setting up a developer environment

Ian Townsend edited this page Jan 10, 2023 · 26 revisions

How to set up a developer environment

Install basic dependencies

EnergySparks is a Ruby on Rails application. It's been developed against the Postgres database. The source code is managed using git and hosted on github.

Requirements

You will need an account with:

  • github

You will ultimately need access to the AWS account

You'll need to install:

  • git
  • Postgres. The current live application is running Postgres 10.13
  • Ruby 2.7.6 or later

We would recommend:

  • Using a ruby version manager, like rvm, rbenv or chruby

  • Using a node version manager, like nvm (see also here for bash and zsh scripts to automatically call nvm use using a .nvmrc file in the project directory)

For deploying to test and production you will also need some AWS command-line tools.

Note, on OSX, if installing Ruby fails with a message about openssl, you may need to specify a version, e.g.

ruby-install ruby-2.7.6 -- --with-openssl-dir="$(brew --prefix openssl@1.1)"

Grab the source code

git clone git@github.com:BathHacked/energy-sparks.git

All of the code and configuration will be in the energy-sparks directory

Install Gem files

bundle install

Setup development and test databases

A fresh install of Postgres may need a user set up with permissions and a password.

Example for user 'postgres', password 'postgres':

createuser postgres    # answer 'n' to questions
psql
alter user postgres with createdb login;
alter user postgres unencrypted password 'postgres';

Simplest option is to get Rails to build you an empty database. If your postgres database has a username/password restriction, then you will need to add a few environment variables for Rails to use to connect to the development and test databases.

A default macOS installation using the Postgres.app does not require these setting, but will likely be required on Ubuntu.

Set these in a file called .env in the root directory of the checked out application.

DEV_DB_HOST=localhost
DEV_DB_NAME=energy_sparks_development
DEV_DB_USERNAME=
DEV_DB_PASSWORD=

To create and run the migrations:

rake db:create db:migrate

You will be able to access some of the website running this locally, but without any further data, exploration and development will be limited.

Importing sanitised copy of production.

For serious development, we would recommend downloading a copy of the Production database and importing that. If you're going to do this then either do it before running db:create and db:migrate OR drop and recreate an existing database (rake db:drop db:create).

You can download a copy of the Production database if your IP address is added to the relevant RDS security group & you use the correct username and password for the database.

pg_dump -h XXX.YYY.ZZZ.amazonaws.com -U <db user name> <db name> > output.sql

Otherwise ask another developer.

You'll need to rename the Postgres users in the output.sql using the sed command given in Refreshing the test environment.

You should use your local database user name which may well be just postgres. To import:

psql -U <db-user> energy_sparks_development <  output.sql

Then anonymising the content by nobbling the Contacts to remove email address and phone numbers.

Assuming that you have a production user account, let's say with an email address of hi@example.com, then once you have imported the database, run the following:

rails c
Contact.where.not(email_address: "hi@example.com").update_all(email_address: '', mobile_phone_number: '')

If you don't have a production user, then you should still remove all that information (Contact.update_all). This will remove email addresses and phone numbers for all other contacts.

You'll either need to manually create a user, or for speed, change the password of an existing admin account.

Run tests

At this point you should be able to successfully run the tests:

rspec

If you encounter any errors, ensure that you have created the correct databases, that the database can be accessed using the configured username and password, and run the commands to setup both databases

Run the application

The simplest way, without hotloading of assets is to run the application using the rails server or rails s command. However, the best way for development is to use the ./run_server.sh script which will fire up the rails server and the wepback dev server so that the webpack based assets can be hot loaded.

Your application should now be running on http://localhost:3000/.

Serverless Data Pipeline

One part of the application relies on using Amazon lambda functions. If you're going to be working on the email processing, you also need to install the serverless framework and additional gems. More notes in the background docs.

Environment Variables

For more advanced development, you may require some environment variables

Local emails

Development mode uses mail catcher for sending mails - you need to install the mailcatcher gem for this to work correctly. If you're on macOS (Monterey or greater) there may also be an extra step required for installation - see this issue for details.

Overcommit

Before committing any changes, make sure that overcommit is installed using overcommit --install

Clone this wiki locally