# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://omalab.gitbook.io/guide/engineering-wiki/migrations/postgres.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
