Rails App: using multiple versions table in paper_trail

88 Views Asked by At

I am using Rails 7.0.4 with Ruby 3.0.4 for my app. The app used multiple databases with Active Record (primary and secondary), each on a specific server. In the app, I have different models, all of them with reading and writing access from primary DB and have also paper_trail enabled. Only one model (Student) with reading and wrtining access from secondary DB.

On both DB, primary and secondary, there is a "versions" table.

What I want to do is, to set the versions table for Student model to the secondary DB. So, for all models the versions table will be on the primary DB; and only for the Student model the versions table will be on the secondary DB. Hier is my configuration so far:

 class StudentRecord < ApplicationRecord
    self.abstract_class = true
    # acts_as_paranoid
    acts_as_paranoid
    # paper_trail
    has_paper_trail :versions => { :class_name => "StudentVersion" }
    connects_to database: { reading: :secondary, writing: :secondary }
  end
  class StudentVersion < PaperTrail::Version    
    self.table_name = "school_log.versions"
    self.sequence_name = "school_log.versions_id_seq"
  end
development:
  primary:
    adapter: postgresql
    host: xxxxxxxxxxxxx
    database: school_portal
    username: xxxxxxxxxxxxx
    password: xxxxxxxxxxxxx
    pool: 20
    schema_search_path: "public,school_data,school_log"

  secondary:
    adapter: postgresql
    host: xxxxxxxxxxxxx
    database: school_portal
    port: xxxxxxxxxxxxx
    username: xxxxxxxxxxxxx
    password: xxxxxxxxxxxxx
    pool: 14
    schema_search_path: "public,school_data,school_log"
class Student < StudentRecord
.....
end

Is my configuration correct? How to set the versions table for Student model to the secondary DB?

Also, in console, if I change some parameters in the Student instant record and try to save it (.save), I got an error "ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "versions_pkey"". How to fix that?

0

There are 0 best solutions below