4

I created a new table via rails migration in Postgres in a new schema (name - "concluded")

class CreateWeeklyLeaderboard < ActiveRecord::Migration
  def up
    execute('create schema concluded')
    create_table('concluded.weekly_leaderboard') do |t|
      t.date :week_start_date
      t.references :user
      t.integer :rank
      t.integer :points
      t.timestamps null: true
    end
  end

  def down
    drop_table('concluded.weekly_leaderboard')
    execute('drop schema concluded')
  end
end

Update

If you add your schema to your database configuration, you will get the table definition in schema.rb

development:
  adapter: postgresql
  encoding: utf8
  database: duggout
  host: 127.0.0.1
  pool: 5
  username: postgres
  password: pass
  schema_search_path: public, concluded

The create_table in schema.rb appears like below:

create_table "weekly_leaderboard", force: :cascade do |t|
   t.date     "week_start_date"
   t.integer  "user_id"
   t.integer  "rank"
   t.integer  "points"
   t.datetime "created_at"
   t.datetime "updated_at"
 end

As you see, the schema name does not appear in this file. How to ensure schema name is present in schema.rb for non public tables?

This is important because in our test machines (or CI agents), we do rake schema:load instead of rake db:migrate as recommended by Rails. In such cases, these tables get created in the public schema instead of 'concluded' schema.

Raj
  • 22,346
  • 14
  • 99
  • 142
  • 1
    can you share your database configuration? You have to enable schemas in configurations http://stackoverflow.com/questions/8806284/using-multiple-postgresql-schemas-with-rails-models – Mike S Jun 26 '16 at 09:18
  • @MihhailSidorin Checking it.. – Raj Jun 26 '16 at 09:21
  • This [answer](http://stackoverflow.com/a/8838881/1826279) mostly covers multiple schema setup. – Mike S Jun 26 '16 at 09:24
  • @MihhailSidorin schema.rb get the create definition now, but without the schema name – Raj Jun 26 '16 at 11:06
  • 2
    I think it is specific of unified approach for different databases. Let's say we will have Sqlite database, we won't be able to set schema there, while with "magical" set of models schema will work even for Sqlite. In given case inheritance from different AR base classes will always define which schema to query. You can rollback to `SQL` version of schema if you want database specific SQL schema dump stored. – Mike S Jun 26 '16 at 14:40
  • 3
    This will have one potential issue, when we do `rake schema:load` the table gets created in the public schema not in my expected schema – Raj Jun 27 '16 at 08:10
  • 2
    @LeninRajRajasekaran , had u any luck fixing this issue? Because I now have the same issue. – Roy Philips Jul 04 '19 at 12:19
  • @RoyPhilips Nope. – Raj Jul 05 '19 at 01:47

0 Answers0