我该如何在Heroku上管理我的数据库索引?我知道有taps,但那似乎是用于推送/拉取数据的。
我该如何查看、更新、删除我的索引?我的开发数据库是sqlite3,而在Heroku上则是postgres。
我该如何在Heroku上管理我的数据库索引?我知道有taps,但那似乎是用于推送/拉取数据的。
我该如何查看、更新、删除我的索引?我的开发数据库是sqlite3,而在Heroku上则是postgres。
您应该通过迁移来管理索引,以便它们在各个环境中保持同步。
看起来你正在使用共享数据库而不是专用数据库,所以你必须走弯路。如果你有一个专用的数据库,那么你可以使用heroku pg:psql
,然后使用\di
和其他各种psql
命令来获取你想要的内容。
当然,总有一条弯路,那就是使用内部目录表。你需要几个 SQL 代码块,你可以将它们包装在ActiveRecord::Base.connection.select_rows
调用中,并从你的 Rails 控制台访问结果。
你可以使用以下代码获取你的表及其索引列表:
select c2.relname as table, c2.oid as table_oid, c.relname as name, c.oid as index_oid
from pg_catalog.pg_class c
join pg_catalog.pg_index i on i.indexrelid = c.oid
join pg_catalog.pg_class c2 on i.indrelid = c2.oid
left join pg_catalog.pg_user u on u.usesysid = c.relowner
left join pg_catalog.pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'i'
and n.nspname <> 'pg_catalog'
and pg_catalog.pg_table_is_visible(c.oid)
order by c2.relname, c.relname
然后,您可以使用以下代码利用index_oid
获取有关该索引的描述:
select c.relname, c.oid, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), c.reltablespace
from pg_catalog.pg_class c
join pg_catalog.pg_index i on c.oid = i.indexrelid
where c.oid = '#{index_oid}'
或者您可以使用table_oid
来获取该表的索引列表,方法如下:
select ci.relname, ci.oid, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), ci.reltablespace
from pg_catalog.pg_index i
join pg_catalog.pg_class ci on i.indexrelid = ci.oid
where i.indrelid = '#{table_oid}'
order by ci.relname
你可能想要将所有这些东西封装在一个实用类中,以便轻松访问:
class PGInfo
def self.list_indexes
data = ActiveRecord::Base.connection.select_rows(%Q{
select c2.relname as table, c.relname as name, c.oid as oid
...
})
# do something pretty with the array of arrays that is in data
end
# etc.
end
我还没有在Heroku上尝试过使用共享数据库(抱歉,我只有一个专用数据库可以玩)。可能有更简单的方法,但是如果您将它们包装在PGInfo类中,这些方法应该能够完成工作,并且易于使用。
所有这些都为您提供所需的索引信息,然后您可以使用常规迁移来添加、删除或修改索引。