Mads
Mads

Reputation: 724

Database schemas in Heroku

I am new at heroku and I have made a java application which runs at heroku. I now want to make a more advanced database for the application and I want to upload the database schema. The example application for java I downloaded from heroku, shows the table being made by a string literal from the code. I would like to make an advanced schema and upload. Is that possible or do I need to create the tables from the code?

Thanks

Upvotes: 0

Views: 149

Answers (1)

klotz
klotz

Reputation: 2031

A common approach to database migrations is to run them as part of the deployment process which is nothing specific to Heroku – it's just part of upgrading infrastructure as required by application code.

Running migrations as part of the deployment separates it nicely from your application code so you won't have to worry about including it into your app.

Sample deployment

To avoid any request being accepted during deployment it's recommended to begin with enabling the app's maintenance mode.

heroku maintenance:on --app myapp

# push to Heroku which will trigger the buildpack to run
git push [email protected]:myapp.git master

# Use migration tool of your choice, this example is using migrate 
# https://github.com/mattes/migrate which supports a number of 
# different database systems. Make sure to use the DATABASE_URL that
# is set in your application's environment variables.
migrate -database $(heroku config:get DATABASE_URL --app myapp) \
  -path ./migrations up

heroku maintenance:off --app myapp

Note, the example above does not include any error handling, for instance how to proceed if the deployment to Heroku fails or how to recover from a failed database migration. Furthermore, it might be a good idea to create a database snapshot/backup before the migration starts.

I included migrate in the example since it comes with support for PostgreSQL, MySQL, SQLite and a bunch of other databases. But any migration tool will do, depending on your requirements.

Upvotes: 2

Related Questions