我正在尝试将用户从一个系统迁移到另一个系统。每个系统都有自己的数据库和不同的类。
我的计划是连接到一个数据库,并通过SQL命令从一个数据库中读取一些信息:
activeRecord::Base.connection.execute(sql_command)
处理数据,然后使用常规模型在新数据库上写入一些结果。
我计划在Sidekiq工作中进行此操作,但是我试图使用Heroku上的Rails控制台进行一些测试。
这应该很简单,但是事实证明这很困难。
当我在Heroku上启动Rails控制台时。我正在连接到DATABASE_URL,可以,但是当我尝试连接到旧数据库并执行命令时,如下所示:
activeRecord::Base.establish_connection(adapter: "postgresql",encoding: "unicode",pool: 10,url: "postgres://...info...")
activeRecord::Base.connection.execute("select count(*) from users")
我最终得到:
PG::ConnectionBad (could not connect to server: No such file or directory)
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
但是,我可以通过使用DATABASE_URL作为env变量在heroku上启动我的rails控制台来连接到该旧数据库:
$ heroku run 'DATABASE_URL=postgres://...info... rails console' -a console' -a app
但是我不知道如何切换回新数据库,所以我可以进行更新。
在heroku上使用Rails控制台时,如何在运行时切换数据库?