Permalink
Browse files

Add support for tablespaces.

  • Loading branch information...
1 parent f07ff9d commit ce7309c38ad15922a419a3900987103c70db6edc @dark-panda dark-panda committed Jan 18, 2012
@@ -10,6 +10,7 @@ module PostgreSQLExtensions
postgresql_adapter_extensions
postgresql_constraints
postgresql_tables
+ postgresql_tablespaces
postgresql_indexes
postgresql_permissions
postgresql_schemas
@@ -0,0 +1,84 @@
+
+require 'active_record/connection_adapters/postgresql_adapter'
+
+module ActiveRecord
+ class InvalidTablespaceParameter < ActiveRecordError #:nodoc:
+ def initialize(parameter)
+ super("Invalid tablespace parameter - #{parameter}")
+ end
+ end
+
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ # Creates a new PostgreSQL tablespace.
+ def create_tablespace(name, location, options = {})
+ sql = "CREATE TABLESPACE #{quote_tablespace(name)} "
+ sql << "OWNER #{quote_role(options[:owner])} " if options[:owner]
+ sql << "LOCATION #{quote(location)}"
+
+ execute("#{sql};")
+ end
+
+ # Drops a tablespace.
+ #
+ # ==== Options
+ #
+ # * <tt>:if_exists</tt> - adds IF EXISTS.
+ def drop_tablespace(name, options = {})
+ sql = 'DROP TABLESPACE '
+ sql << 'IF EXISTS ' if options[:if_exists]
+ sql << quote_tablespace(name)
+
+ execute("#{sql};")
+ end
+
+ #ALTER TABLESPACE name SET ( tablespace_option = value [, ... ] )
+ #ALTER TABLESPACE name RESET ( tablespace_option [, ... ] )
+
+ # Renames a tablespace.
+ def rename_tablespace(old_name, new_name)
+ execute("ALTER TABLESPACE #{quote_tablespace(old_name)} RENAME TO #{quote_tablespace(new_name)};")
+ end
+
+ # Changes a tablespace's owner.
+ def alter_tablespace_owner(tablespace, role)
+ execute("ALTER TABLESPACE #{quote_tablespace(tablespace)} OWNER TO #{quote_role(role)};")
+ end
+
+ def alter_tablespace_parameters(tablespace, parameters_and_values)
+ sql = "ALTER TABLESPACE #{quote_tablespace(tablespace)} SET ("
+
+ sql << parameters_and_values.collect { |k, v|
+ assert_valid_tablespace_parameter(k)
+ "\n #{quote_generic(k)} = #{v}"
+ }.join(",")
+
+ sql << "\n);"
+
+ execute(sql)
+ end
+
+ def reset_tablespace_parameters(tablespace, *parameters)
+ sql = "ALTER TABLESPACE #{quote_tablespace(tablespace)} RESET ("
+
+ sql << parameters.flatten.collect { |k|
+ assert_valid_tablespace_parameter(k)
+ "\n #{quote_generic(k)}"
+ }.join(",")
+
+ sql << "\n);"
+
+ execute(sql)
+ end
+
+ private
+ TABLESPACE_PARAMETERS = %w{ seq_page_cost random_page_cost }.freeze
+
+ def assert_valid_tablespace_parameter(parameter)
+ if !TABLESPACE_PARAMETERS.include? parameter.to_s.downcase
+ raise ActiveRecord::InvalidTablespaceParameter.new(option)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,63 @@
+
+$: << File.dirname(__FILE__)
+require 'test_helper'
+
+class TablespaceTests < Test::Unit::TestCase
+ include PostgreSQLExtensionsTestHelper
+
+ def test_create_tablespace
+ Mig.create_tablespace('foo', '/tmp/foo')
+ Mig.create_tablespace('foo', '/tmp/foo', :owner => :bar)
+
+ assert_equal([
+ %{CREATE TABLESPACE "foo" LOCATION '/tmp/foo';},
+ %{CREATE TABLESPACE "foo" OWNER "bar" LOCATION '/tmp/foo';}
+ ], statements)
+ end
+
+ def test_drop_tablespace
+ Mig.drop_tablespace('foo')
+ Mig.drop_tablespace('foo', :if_exists => true)
+
+ assert_equal([
+ %{DROP TABLESPACE "foo";},
+ %{DROP TABLESPACE IF EXISTS "foo";}
+ ], statements)
+ end
+
+ def test_rename_tablespace
+ Mig.rename_tablespace('foo', 'bar')
+
+ assert_equal([
+ %{ALTER TABLESPACE "foo" RENAME TO "bar";}
+ ], statements)
+ end
+
+ def test_alter_tablespace_owner
+ Mig.alter_tablespace_owner('foo', 'bar')
+
+ assert_equal([
+ %{ALTER TABLESPACE "foo" OWNER TO "bar";}
+ ], statements)
+ end
+
+ def test_alter_tablespace_paramters
+ Mig.alter_tablespace_parameters('foo', :seq_page_cost => 2.0, :random_page_cost => 5.0)
+
+ assert_equal([
+ %{ALTER TABLESPACE "foo" SET (
+ "seq_page_cost" = 2.0,
+ "random_page_cost" = 5.0
+);} ], statements)
+ end
+
+ def test_reset_tablespace_paramters
+ Mig.reset_tablespace_parameters('foo', :seq_page_cost, :random_page_cost)
+
+ assert_equal([
+ %{ALTER TABLESPACE "foo" RESET (
+ "seq_page_cost",
+ "random_page_cost"
+);} ], statements)
+ end
+end

0 comments on commit ce7309c

Please sign in to comment.