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.