How do I Migrate the Database?


#1

I am stuck with DB migration, How to create and migrate the remote db?


#2

Edit your .env.development.remote with a DATABASE_URL. Then you can run:

JETS_ENV_REMOTE=1 jets db:migrate

This assumes that you’re running it on a machine with access to the remote database. Here’s a little more info on JETS_ENV_REMOTE http://rubyonjets.com/docs/env-files/ Hope that helps.


#3

Hi,

what if my local machine does not have access to the remote database?
I deploy a jets app into a VPC connecting to an Aurora MySQL serverless, which can only run inside a VPC and is not accessible from outside. I managed already to make this setup work after an entire weekend of digging :slight_smile:
My lambda function are accessible through API gateway and the app connects to aurora serverless. The only think now is to do this db:migrate part.


#4

@artur2000 One approach is to run the command on AWS Lambda itself. Here’s an example: jobs/command_job.rb Also here for convenience:

class CommandJob < ApplicationJob
  def execute
    command = event['command'] || 'uptime'
    sh command
  end

  def migrate
    sh "jets db:migrate"
  end

private
  def sh(command)
    puts "=> #{command}"
    puts `#{command}`
  end
end

Usually, migrations are ran before deployment. So you can deploy the code with migrations to a new environment first:

JETS_ENV_EXTRA=2 jets deploy

Then you can use the AWS Lambda Console to run the migrate job or any arbitrary command. Afterwards you can delete the environment:

JETS_ENV_EXTRA=2 jets delete

That’s one workaround if you don’t have a machine with access to the remote RDS db.

Another way is an ssh tunnel. Forward your machine to a server that has access, and then you can migrate locally. Only works if you can at least ssh into the machine. This article talks about ssh forwarding:

3 SSH tips: Ssh-agent, Tunnel, and Escaping from the Dead


#5

Hello,

I tried to apply some solutions that as you telling here, but there didn’t work properly. I was starting developing in local with my MySQL instance and now I’m trying to deploy this petproject in AWS. I created an aurora rds instance and thinking in CI/CD I want to automatize all of the steps.

I don’t see how can I deploy with db:create and db:migrate as deploy task.

Any idea?


#6

May need a little more info on the error you’re seeing. A shot in the dark here. With an Aurora DB, I believe you need to be within the same VPC as the Aurora DB. In this thread, there are essentially 3 options listed:

  1. Run command from on an ec2 instance that is in the same VPC and has access
  2. Create that command_job.rb function and configure it with a vpc_config function property. More info here: Jets Function Properties and Considerations VPC
  3. Create an SSH tunnel from your local machine to ec2 instance set up as described in number 1. Then you can run jets db:migrate https://blog.boltops.com/2017/09/21/3-ssh-tips-ssh-agent-tunnel-and-escaping-from-the-dead

If you’re trying technique #2, please make sure you configure vpc_config and make sure that subnet contains a NAT gateway.


Running migrations with "jets deploy" command