ruby-on-rails 用于更改现有数据库视图的Rails迁移

1sbrub3j  于 2023-10-21  发布在  Ruby
关注(0)|答案(2)|浏览(126)

我有一个名为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中可用。但我不知道如何写这个迁移。任何帮助非常感谢。谢谢

xu3bshqb

xu3bshqb1#

简单地重新创建视图如何:

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
s3fp2yjn

s3fp2yjn2#

scenic gem(由thoughtbot开发)将在管理视图方面对您有很大帮助!

相关问题