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