Rails 改变现有数据库视图的迁移
Rails migration for altering existing database view
我有一个名为 my_view
的视图,它是我使用以下迁移创建的。
class CreateMyView < ActiveRecord::Migration
def change
execute <<-SQL
drop view if exists my_view
SQL
execute <<-SQL
CREATE OR REPLACE VIEW my_view AS
SELECT
t1.wfs_id,
t1.step_id,
t1.status,
t1.applied_by,
t2.created_at,
t2.is_wfs_end,
t2.app_status AS flowstep
FROM table1 t1
JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
WHERE t1.del_flag = false;
SQL
end
end
现在我需要另一个字段,比如 table1
中的 my_new_field
在 my_view
中可用。但我不知道如何为此编写迁移。非常感谢任何帮助。谢谢
简单地重新创建视图怎么样:
class ChangeMyView < ActiveRecord::Migration
def change
execute <<-SQL
drop view if exists my_view
SQL
execute <<-SQL
CREATE OR REPLACE VIEW my_view AS
SELECT
t1.wfs_id,
t1.step_id,
t1.status,
t1.applied_by,
t1.my_new_field,
t2.created_at,
t2.is_wfs_end,
t2.app_status AS flowstep
FROM table1 t1
JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
WHERE t1.del_flag = false;
SQL
end
end
scenic gem(来自 Thoughtbot)将对您管理视图有很大帮助!
我有一个名为 my_view
的视图,它是我使用以下迁移创建的。
class CreateMyView < ActiveRecord::Migration
def change
execute <<-SQL
drop view if exists my_view
SQL
execute <<-SQL
CREATE OR REPLACE VIEW my_view AS
SELECT
t1.wfs_id,
t1.step_id,
t1.status,
t1.applied_by,
t2.created_at,
t2.is_wfs_end,
t2.app_status AS flowstep
FROM table1 t1
JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
WHERE t1.del_flag = false;
SQL
end
end
现在我需要另一个字段,比如 table1
中的 my_new_field
在 my_view
中可用。但我不知道如何为此编写迁移。非常感谢任何帮助。谢谢
简单地重新创建视图怎么样:
class ChangeMyView < ActiveRecord::Migration
def change
execute <<-SQL
drop view if exists my_view
SQL
execute <<-SQL
CREATE OR REPLACE VIEW my_view AS
SELECT
t1.wfs_id,
t1.step_id,
t1.status,
t1.applied_by,
t1.my_new_field,
t2.created_at,
t2.is_wfs_end,
t2.app_status AS flowstep
FROM table1 t1
JOIN table2 t2 ON t1.wfs_id = t2.wfs_id
WHERE t1.del_flag = false;
SQL
end
end
scenic gem(来自 Thoughtbot)将对您管理视图有很大帮助!