在Heroku中插入记录无序发生

时间:2015-10-19 07:49:39

标签: ruby-on-rails postgresql heroku

我的生产系统遇到了一个问题,我在开发箱上没有看到。

我使用活动记录将记录插入表中,获取结果,然后插入原始记录所拥有的其他几个依赖记录。这些其他插入是在他们自己的方法中执行的,整个事情都包含在事务中。

这在我的开发盒(运行PostgreSQL的Linux)上很有效,但在Heroku上它失败了。

检查日志后,将日志记录设置为:debug并插入一些日志记录,似乎插入不按顺序发生。 (事实上​​,日志行似乎也不正常。)

这是执行插入的代码

ActiveRecord::Base.transaction do
  @transaction = create_sale_transaction(@organisation, params[:sale])
  unless @transaction.nil?
    Rails.logger.info('A')
    create_account_recv_transaction(@organisation, @transaction)
    Rails.logger.info('B')
    create_tax_transaction(@organisation, @transaction)
    Rails.logger.info('C')
    create_retained_earnings_transaction(@organisation, @transaction)
    Rails.logger.info('D')
  end
end

每个被调用的方法都会设置一个新事务并填充数据。这是一种创作方法,它们在功能上都相似。

def create_account_recv_transaction(organisation, sale)
  Rails.logger.info('create_account_recv_transaction')
  transaction = Transaction.new
  transaction.organisation_id = organisation.id
  transaction.transaction_type = Transaction::JOURNAL
  transaction.transaction_state = 0
  transaction.owner_id = sale.id
  transaction.owner_type = 'Transaction'
  transaction.date = sale.date
  transaction.refnum = sale.refnum
  transaction.description = sale.description
  transaction.amount = sale.amount + sale.tax
  transaction.finacct_id = ar_account
  transaction.save!
end

这是Heroku PostgreSQL数据库的正常行为吗?

如果是,解决这个问题的常用方法是什么?

更新 我确实将初始保存的结果包装在if中。这并没有任何区别。

这是其中一条日志行。

2015-10-19T08:58:17.874036+00:00 app[web.1]:   SQL (1.2ms)  INSERT INTO "transactions" ("organisation_id", "transaction_type", "transaction_state", "owner_id", "owner_type", "date", "refnum", "description", "amount", "finacct_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["organisation_id", 19], ["transaction_type", 0], ["transaction_state", 0], ["owner_id", 256], ["owner_type", "Transaction"], ["date", "2015-10-19"], ["refnum", "INV12345"], ["description", "A description"], ["amount", "100.0"], ["finacct_id", 14], ["created_at", "2015-10-19 08:58:17.870929"], ["updated_at", "2015-10-19 08:58:17.870929"]]

更新2

我在Heroku中发现了一个名为Postgres Studio的插件,它让我可以查看生产数据库。

似乎除了第一个以外的任何其他记录都没有插入数据库中。

0 个答案:

没有答案