W4rQCC1yGh
W4rQCC1yGh

Reputation: 2219

How to check creation date of postgres schema?

For debugging purposes I would like to know when are some of my postgres schemas created at- is it possible?

Searched from PostgreSQL and Apartment docs but didn't find any helpful clues.

Environment and tools I'm using:

+---------------+----------+
| Tools         | Version  |
+--------------------------+
| PostgreSQL    | 9.4.1    |
| Ruby          | 2.2.1p85 |
| Ruby on Rails | 4.1.9    |
| Apartment gem | 0.26.1   |
+---------------+----------+

As you can see I use Apartment for creating schemas in my multi-tenant rails application.

Upvotes: 2

Views: 8059

Answers (4)

W4rQCC1yGh
W4rQCC1yGh

Reputation: 2219

I'll post a "Rails way" solution that will not work for previously made schemas but new-ones.

  1. Create model for tenants rails g model Tenant name:string
  2. Add after_create callback for Tenant model

    class Tenant < ActiveRecord::Base
      after_create :tenant_moves_in
    
      private
    
      def tenant_moves_in
        Apartment::Tenant.create self.name
      end
    
  3. For ex. create somekind of script for making tenants or use ActiveRecord CRUD ability with Tenant model to operate with tenants in your app directly like Tenant.create(name: 'new_tenant').

Thatway you can have created_at and updated_at dates in your db tenants table in relation to Apartment Tenants, which represent postgres schemas.

Of course then you should avoid making tenants with Aparmtent tool like Apartment::Tenant.create 'new_tenant' because that will not create a Tenant-model object and therefore no created_at time will be preserved.

Upvotes: 0

Frank Heikens
Frank Heikens

Reputation: 126991

You could create an event trigger and store the date of this event in a table. And example can be found here

Upvotes: 1

jvenezia
jvenezia

Reputation: 900

Disclamer : This answers a wrong interpretation of the question. I keep it in case it is useful for someone...

When you run you Ruby on Rails migrations with rake db:migrate, rails maintain a file witch shows you a snapshot of the database structure. This file is in db/schema.rb.

Here is an example of a table with a created_at added by rails :

  create_table "comments", force: true do |t|
    t.integer  "user_id"
    t.integer  "article_id"
    t.text     "content"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
    t.string   "origin",     default: "web"
  end

More details here: http://edgeguides.rubyonrails.org/active_record_migrations.html

Upvotes: -1

PostgreSQL system catalogs don't store the dates on which database objects were created. You can configure PostgreSQL to log more or less every SQL statement (log_statement setting), but you'd have had to do that beforehand.

Upvotes: 1

Related Questions