# Postgres

Migrations using ActiveRecord happen in the omalab/oma repo which is a Rails app. In that repository all standard Rails documentation related to migrations applies.

To generate a migration, do:`bundle exec rails g migration migration_name`

This generates a migration, as normal. Run it here (remember to run it for test as well).

```
# development
bundle exec rake db:migrate
# test
OMA_ENV=test bundle exec  rake db:migrate
# production 
OMA_ENV=production bundle exec rake db:migrate
```

The and result of migrations is a changed db/schema.rb file. Once you're happy with the changes that happened to the DB you need to copy the file to oma-models/db/schema.rb and oma-processors/db/schema.rb.

```
# you are in the oma folder/repo
cp ./db/schema.rb oma-models/db/schema.rb
cp ./db/schema.rb oma-processors/db/schema.rb
```

When the code you're working on is ready for Pull requests, you need to make sure that there are Pull Requests open in all three repositories so database schema changes are reflected in all off them.

Three pull requests:

*   **oma**

    PR should contain the migrations, the changed db/schema.rb and a reference in the Gemfile to the right oma-models.
*   **oma-models**

    PR should contain the same schema as the the oma PR and changes to the actual model classes that come with the changed schema.
*   **oma-processors**

    PR should contain the same schema as the the oma PR and and a reference in the Gemfile to the right oma-models.

Reference to oma-models:

```
cd oma-models
git log
# copy the latest hash and paste it into oma/Gemfile and oma-processors/Gemfile
```
