简体   繁体   English

Rails 4:JSON备份回Postgresql DB

[英]Rails 4: JSON backup back to Postgresql DB

I have an issue re-creating my app DB from JSON backup file. 我从JSON备份文件重新创建我的应用程序数据库时遇到问题。

What I am trying to do now is to create all the records from rake task reading lines in the file and rendering json like: 我现在想要做的是从文件中的rake任务读取行创建所有记录,并渲染json,例如:

  new_record = key.camelize.constantize.new(hs['key'])
  new_record.save

The problem is that I can not skip callbacks for all the models, to be sure I am not creating any dup stuff there. 问题是我不能跳过所有模型的回调,以确保没有在其中创建任何dup内容。 .camelize.constantize.skip_callback(:after_create) is just not working giving me an error undefined method '_after_create_callbacks' for #<Class: . .camelize.constantize.skip_callback(:after_create)无法正常工作,给我undefined method '_after_create_callbacks' for #<Class:的错误undefined method '_after_create_callbacks' for #<Class:

So two questions here: 所以这里有两个问题:

1) Is there any way to skip AR callbacks other way? 1)有没有其他方法可以跳过AR回调? 2) Are there any other options for re-creating db from JSON except SQL queries? 2)除SQL查询外,是否还有其他选项可用于从JSON重新创建数据库?

1. Skipping callbacks 1.跳过回调

You can ask AR to skip validation callbacks when changing a record like this: 您可以要求AR在更改如下记录时跳过 验证 回调:

new_record.class.skip_callback(:create, :after, :specific_callback)
new_record.save

2. Importing JSON into PSQL 2.将JSON导入PSQL

Since your data is in JSON, there is no magical way of importing it into your DB. 由于您的数据使用JSON,因此没有神奇的方式将其导入数据库。 But, Postgres allows you easily import it using SQL like it is shown in this SO answer . 但是,Postgres允许您使用SQL轻松导入它,就像该SO答案所示。 Also, many others have written utilities to make your life easier. 此外,许多其他人编写了实用程序 ,使您的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM