-
Notifications
You must be signed in to change notification settings - Fork 6
/
postgresql_tablespaces.rb
84 lines (65 loc) · 2.48 KB
/
postgresql_tablespaces.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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