I''d like to put tables in separate schemas: current working copies of data, historical data (logged with acts_as_versioned or something), and internal management data. ActiveRecord has a class variable table_name_prefix that would help with handling a single schema, but not with the three I need. If someone has found an elegant way to use multiple schemas, please share it. If it can only be made to work with effort, I rather stick with a single schema. Michael -- Michael Schuerig The Fifth Rider of the Apocalypse mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org is a programmer. http://www.schuerig.de/michael/
How about placing a view inside your primary schema that references the table in the other schema and then use that view with activerecord? On 9/19/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > I''d like to put tables in separate schemas: current working copies of > data, historical data (logged with acts_as_versioned or something), and > internal management data. ActiveRecord has a class variable > table_name_prefix that would help with handling a single schema, but > not with the three I need. > > If someone has found an elegant way to use multiple schemas, please > share it. If it can only be made to work with effort, I rather stick > with a single schema. > > Michael > > -- > Michael Schuerig The Fifth Rider of the Apocalypse > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org is a programmer. > http://www.schuerig.de/michael/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Marlon "And if I claim to be a wise man, it surely means that I don''t know" liberal, fanatical, criminal. "
On 9/19/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > > I''d like to put tables in separate schemas: current working copies of > data, historical data (logged with acts_as_versioned or something), and > internal management data. ActiveRecord has a class variable > table_name_prefix that would help with handling a single schema, but > not with the three I need. > > If someone has found an elegant way to use multiple schemas, please > share it. If it can only be made to work with effort, I rather stick > with a single schema. > > Michael >Are you talking about postgresql schema''s, or just using schema as a general term? With postgresql schema''s you use the set search_path statement to switch between schema''s. I believe the postgresql driver for activerecord supports setting the search path. Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Monday 19 September 2005 17:42, snacktime wrote:> On 9/19/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote: > > I''d like to put tables in separate schemas: current working copies > > of data, historical data (logged with acts_as_versioned or > > something), and internal management data. ActiveRecord has a class > > variable table_name_prefix that would help with handling a single > > schema, but not with the three I need. > > > > If someone has found an elegant way to use multiple schemas, please > > share it. If it can only be made to work with effort, I rather > > stick with a single schema. > > > > Michael > > Are you talking about postgresql schema''s, or just using schema as a > general term? With postgresql schema''s you use the set search_path > statement to switch between schema''s. I believe the postgresql driver > for activerecord supports setting the search path.I''m talking about PgSQL schemas and I need to access all of them at the same time. Switching between schemas in application code doesn''t feel good. I fear it is much too easy to forget it and it clutters the code. Michael -- Michael Schuerig They tell you that the darkness mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Is a blessing in disguise http://www.schuerig.de/michael/ --Janis Ian, From Me To You
On 9/19/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > On Monday 19 September 2005 17:42, snacktime wrote: > > On 9/19/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote: > > > I''d like to put tables in separate schemas: current working copies > > > of data, historical data (logged with acts_as_versioned or > > > something), and internal management data. ActiveRecord has a class > > > variable table_name_prefix that would help with handling a single > > > schema, but not with the three I need. > > > > > > If someone has found an elegant way to use multiple schemas, please > > > share it. If it can only be made to work with effort, I rather > > > stick with a single schema. > > > > > > Michael > > > > Are you talking about postgresql schema''s, or just using schema as a > > general term? With postgresql schema''s you use the set search_path > > statement to switch between schema''s. I believe the postgresql driver > > for activerecord supports setting the search path. > > I''m talking about PgSQL schemas and I need to access all of them at the > same time. Switching between schemas in application code doesn''t feel > good. I fear it is much too easy to forget it and it clutters the code. > > Michael > > what about a :before filter to set the search_path according to whateverlogic you use to decide what schema is being used? Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Monday 19 September 2005 19:16, snacktime wrote:> what about a :before filter to set the search_path according to > whatever logic you use to decide what schema is being used?A before_filter applies to actions in a controller only. It would not help when I access my app from a script (cron job) or script/console. Michael -- Michael Schuerig Not only does lightning not strike mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org twice, it usually doesn''t strike once. http://www.schuerig.de/michael/ --Salman Rushdie, Fury
On 19/09/05 13:03 +0200, Michael Schuerig wrote:> > I''d like to put tables in separate schemas: current working copies of > data, historical data (logged with acts_as_versioned or something), and > internal management data. ActiveRecord has a class variable > table_name_prefix that would help with handling a single schema, but > not with the three I need. > > If someone has found an elegant way to use multiple schemas, please > share it. If it can only be made to work with effort, I rather stick > with a single schema. >Looking at the API docs you should be able to pass in a :schema_search_path in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter. You can even put this in database.yml. If you pass in the schema names that you want to search then you wont have to specify them in the controller, nor will you have to qualify the table hane with the schema name. Jason
On Tuesday 20 September 2005 15:05, Jason Stewart wrote:> On 19/09/05 13:03 +0200, Michael Schuerig wrote: > > I''d like to put tables in separate schemas: current working copies > > of data, historical data (logged with acts_as_versioned or > > something), and internal management data. ActiveRecord has a class > > variable table_name_prefix that would help with handling a single > > schema, but not with the three I need. > > > > If someone has found an elegant way to use multiple schemas, please > > share it. If it can only be made to work with effort, I rather > > stick with a single schema. > > Looking at the API docs you should be able to pass in a > > :schema_search_path in > : ActiveRecord::ConnectionAdapters::PostgreSQLAdapter. > > You can even put this in database.yml. If you pass in the schema > names that you want to search then you wont have to specify them in > the controller, nor will you have to qualify the table hane with the > schema name.The point is that search won''t help. I''d like to use multiple schemas precisely because I want to have tables with the same names, one schema containing the current working versions, another containing historical data. The idea is to use the same AR class to access tables in either of the schemas. As programming helps me get a better understanding of what I want, below is a mixin for AR::Base that handles schema switching. The idea is to surround DB accesses not meant to go to the default schema like this ActiveRecord::Base.with_schema ''audit'' do old_stuff = Stuff.find(:all, :conditions => ...) end I''ll see if this really works out the way I want it to. Also, I still need to adapt Rick Olson''s acts_as_versioned for my needs. Michael require ''active_record'' module BoilerPlate # :nodoc: module Model # :nodoc: module Schema mattr_accessor :default_schema def self.included(base) base.extend(ClassMethods) base.class_eval do unless method_defined?(:table_name_with_schema) class <<base alias_method :table_name_without_schema, :table_name def table_name_with_schema plain_table_name = table_name_without_schema if schema = BoilerPlate::Model::Schema.default_schema "#{schema}.#{plain_table_name}" else plain_table_name end end alias_method :table_name, :table_name_with_schema end end end end module ClassMethods def with_schema(schema) old_schema = BoilerPlate::Model::Schema.default_schema BoilerPlate::Model::Schema.default_schema = schema yield ensure BoilerPlate::Model::Schema.default_schema = old_schema end end end end end -- Michael Schuerig The Fifth Rider of the Apocalypse mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org is a programmer. http://www.schuerig.de/michael/