`
yiyanwan77
  • 浏览: 182584 次
  • 性别: Icon_minigender_1
  • 来自: 威海
社区版块
存档分类
最新评论

rails postgreSQL schema

 
阅读更多

摘自老外的一个pg数据库schema解决方案

 

原帖地址:http://stackoverflow.com/questions/2782758/creating-a-multi-tenant-application-using-postgresqls-schemas-and-rails

 

 

 

module ActionController
  module Routing
    class RouteSet
      def extract_request_environment(request)
        env = { :method => request.method }
        env[:domain] = request.domain if request.domain
        env[:host] = request.host if request.host

        env
      end
    end
    class Route
      alias_method :old_recognition_conditions, :recognition_conditions
      def recognition_conditions
        result = old_recognition_conditions
        [:host, :domain].each do |key|
          if conditions[key]
            operator = "==="
            if conditions[key].is_a?(Regexp)
              operator = "=~"
            end
            result << "conditions[:#{key.to_s}] #{operator} env[:#{key.to_s}]"
          end
        end
        result
      end
    end# end class Route
  end
end

 

 

 

 

 

 

 module SchemaUtils
   def self.add_schema_to_path(schema)
    conn = ActiveRecord::Base.connection
    conn.execute "SET search_path TO #{schema}, #{conn.schema_search_path}"
   end

   def self.reset_search_path
    conn = ActiveRecord::Base.connection
    conn.execute "SET search_path TO #{conn.schema_search_path}"
   end

   def self.create_and_migrate_schema(schema_name)
    conn = ActiveRecord::Base.connection

    schemas = conn.select_values("select * from pg_namespace where nspname != 'information_schema' AND nspname NOT LIKE 'pg%'")

    if schemas.include?(schema_name)
     tables = conn.tables
     Rails.logger.info "#{schema_name} exists already with these tables #{tables.inspect}"
    else
     Rails.logger.info "About to create #{schema_name}"
     conn.execute "create schema #{schema_name}"
    end

    # Save the old search path so we can set it back at the end of this method
    old_search_path = conn.schema_search_path

    # Tried to set the search path like in the methods above (from Guy Naor)
    # [METHOD 1]: conn.execute "SET search_path TO #{schema_name}"
    # But the connection itself seems to remember the old search path.
    # When Rails executes a schema it first asks if the table it will load in already exists and if :force => true. 
    # If both true, it will drop the table and then load it. 
    # The problem is that in the METHOD 1 way of setting things, ActiveRecord::Base.connection.schema_search_path still returns $user,public.
    # That means that when Rails tries to load the schema, and asks if the tables exist, it searches for these tables in the public schema.
    # See line 655 in Rails 2.3.5 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
    # That's why I kept running into this error of the table existing when it didn't (in the newly created schema).
    # If used this way [METHOD 2], it works. ActiveRecord::Base.connection.schema_search_path returns the string we pass it.
    conn.schema_search_path = schema_name

    # Directly from databases.rake. 
    # In Rails 2.3.5 databases.rake can be found in railties/lib/tasks/databases.rake
    file = "#{Rails.root}/db/schema.rb"
    if File.exists?(file)
     Rails.logger.info "About to load the schema #{file}"
     load(file)
    else
     abort %{#{file} doesn't exist yet. It's possible that you just ran a migration!}
    end

    Rails.logger.info "About to set search path back to #{old_search_path}."
    conn.schema_search_path = old_search_path
   end
  end
分享到:
评论

相关推荐

    activerecord-postgres-dump-schemas:在Rails dbstructure.sql文件中定义要转储的PostgreSQL模式

    在调用rake db:structure:dump时,明确定义要在Rails db/structure.sql文件中转储的PostgreSQL模式。 此功能将包含在Rails的5,但这种宝石反向移植此功能的Rails 3个Rails 4幸得为为Rails 5。 安装 将此行添加到您...

    ridgepole:Ridgepole 是一个管理 DB 模式的工具。 它使用 Rails DSL 定义 DB schema,并根据 DSL 更新 DB schema。 (如ChefPuppet)

    脊杆 Ridgepole 是一个管理 DB 模式的工具。 它使用定义 DB 模式,并根据 DSL 更新 DB 模式。 (如厨师/木偶) 变更日志 &gt;= 0.4.8 activerecord-mysql-unsigned现在是可选的。 如果要使用,请在安装 --enable-...

    最终的

    自述文件 该自述文件通常会记录启动和运行... rails db:schema:load Rails服务器 rails db:migrate:reset 轨道测试 Rails G迁移add_remember_digest_to_users记住_digest:字符串 Heroku日志 Heroku打开 Heroku

    风景:Rails的版本化数据库视图

    风景名胜 Scenic将方法添加到ActiveRecord::Migration以在Rails中创建和管理数据库视图。 使用Scenic,可以将SQL视图的功能带到Rails... 风景秀丽的船支持PostgreSQL。 该适配器是可配置的(请参见Scenic::Configurati

    Data Visualization Toolkit: Using JavaScript

    You’ll learn how to use Rails, jQuery, D3, Leaflet, PostgreSQL, and PostGIS together, creating beautiful visualizations and maps that give your data a voice and to make it “dance.” Barrett Clark ...

    pg_party:ActiveRecord PostgreSQL分区

    产品特点分区特定的数据库操作的迁移方法查询分区数据,创建临时分区和获取分区元数据的模型方法局限性分区表在db/schema.rb表示不正确-请使用:sql模式格式未来的工作自动分区创建(通过cron或其他方式)安装将此行...

    切换员

    支持关联中的跨分片引用(甚至多分片关联) 挂钩ActiveRecord查询基础结构,以自动推断正确的分片以对其执行查询,并执行必要的外键转换在同一数据库服务器上支持多个分片(使用PostgreSQL上的Schema) 主要支持...

    homepicker:通过一起导入,讨论和评估房屋的方式来协作进行房地产开发

    选择器 建立一个有趣的房屋列表,并自动从Zoopla / Rightmove提取(和备份)数据。...bundle exec rails db:schema:load 发展 使用bundle exec rspec测试。 使用bundle exec rails s启动本地服务器

    activerecord-postgres_enum:将PostgreSQLenum数据类型集成到ActiveRecord的架构和迁移中

    ActiveRecord :: PostgresEnum 将迁移和schema.rb支持添加到PostgreSQL枚举数据类型。 安装将此行添加到您的应用程序的Gemfile中: gem 'activerecord-postgres_enum' 然后执行: $ bundle或自己安装为: $ gem ...

    mallard

    凫 绿头鸭(Mallard)是一个自托管的,简单的,直接的RSS阅读器。 这是一个带有Hotwire的Rails应用程序,旨在在...运行rake db:create db:schema:load创建数据库如果要更新,请运行rake db:migrate以捕获最新的数据库

    snookerLeague:管理斯诺克联赛的 SPA 应用程序

    使用 PostgreSQL 和 AngularJS 在 Ruby on Rails 中的单页应用程序。 在这个项目中,我正在学习如何制作好的 Web 应用程序。 我尝试保持干净的代码(我的第一步)并使测试覆盖率保持在 95% 以上。 这是管理斯诺克...

    diabetes-monitor:记录血糖和胰岛素水平的应用程序

    要创建数据库,请运行: $ bin/rails db:create db:schema:load 您将需要在本地运行的Postgresql服务器,以使其正常工作。 最后,要运行该应用程序(在开发环境中),请运行: $ bin/rails s发展(即将推出)执照此...

    PublicArt.io:用于在线访问带有地理标记的涂鸦的最大数据集的API

    brew install postgresql gem install bundler bundle install rake db:schema:load rake db:migrate RAILS_ENV=development unicorn_rails 然后转到 独角兽的东西 默认情况下,Unicorn将绑定到TCP端口8080上的所有...

Global site tag (gtag.js) - Google Analytics