Module: Torque::PostgreSQL::Adapter::SchemaStatements
- Included in:
- Torque::PostgreSQL::Adapter
- Defined in:
- lib/torque/postgresql/adapter/schema_statements.rb
Constant Summary collapse
- TableDefinition =
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition
Instance Method Summary collapse
-
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values.
-
#change_table(table_name, **options) ⇒ Object
Simply add the schema to the table name when changing a table.
-
#create_schema(name, options = {}) ⇒ Object
Create a new schema.
-
#create_table(table_name, **options, &block) ⇒ Object
Rewrite the method that creates tables to easily accept extra options.
-
#data_source_sql(name = nil, type: nil) ⇒ Object
Fix the query to include the schema on tables names when dumping.
-
#drop_schema(name, options = {}) ⇒ Object
Drop an existing schema.
-
#drop_table(table_name, **options) ⇒ Object
Simply add the schema to the table name when dropping a table.
-
#drop_type(name, options = {}) ⇒ Object
Drops a type.
-
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
-
#quoted_scope(name = nil, type: nil) ⇒ Object
When dumping the schema we need to add all schemas, not only those active for the current
schema_search_path
. -
#rename_type(type_name, new_name, options = {}) ⇒ Object
Renames a type.
-
#table_options(table_name) ⇒ Object
Add the schema option when extracting table options.
-
#valid_table_definition_options ⇒ Object
Add schema and inherits as one of the valid options for table definition.
Instance Method Details
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values
Example:
add_enum_values 'status', ['baz']
add_enum_values 'status', ['baz'], before: 'bar'
add_enum_values 'status', ['baz'], after: 'foo'
add_enum_values 'status', ['baz'], prepend: true
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 50 def add_enum_values(name, values, = {}) before = .fetch(:before, false) after = .fetch(:after, false) before = enum_values(name).first if .key? :prepend before = quote(before) unless before == false after = quote(after) unless after == false quote_enum_values(name, values, ).each do |value| reference = "BEFORE #{before}" unless before == false reference = "AFTER #{after}" unless after == false execute <<-SQL.squish ALTER TYPE #{quote_type_name(name, [:schema])} ADD VALUE #{value} #{reference} SQL before = false after = value end end |
#change_table(table_name, **options) ⇒ Object
Simply add the schema to the table name when changing a table
91 92 93 94 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 91 def change_table(table_name, **) table_name = "#{[:schema]}.#{table_name}" if [:schema].present? super table_name, ** end |
#create_schema(name, options = {}) ⇒ Object
Create a new schema
11 12 13 14 15 16 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 11 def create_schema(name, = {}) drop_schema(name, ) if [:force] check = 'IF NOT EXISTS' if .fetch(:check, true) execute("CREATE SCHEMA #{check} #{quote_schema_name(name.to_s)}") end |
#create_table(table_name, **options, &block) ⇒ Object
Rewrite the method that creates tables to easily accept extra options
81 82 83 84 85 86 87 88 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 81 def create_table(table_name, **, &block) table_name = "#{[:schema]}.#{table_name}" if [:schema].present? [:id] = false if [:inherits].present? && [:primary_key].blank? && [:id].blank? super table_name, **, &block end |
#data_source_sql(name = nil, type: nil) ⇒ Object
Fix the query to include the schema on tables names when dumping
119 120 121 122 123 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 119 def data_source_sql(name = nil, type: nil) return super unless name.nil? super.sub('SELECT c.relname FROM', "SELECT n.nspname || '.' || c.relname FROM") end |
#drop_schema(name, options = {}) ⇒ Object
Drop an existing schema
19 20 21 22 23 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 19 def drop_schema(name, = {}) force = .fetch(:force, '').upcase check = 'IF EXISTS' if .fetch(:check, true) execute("DROP SCHEMA #{check} #{quote_schema_name(name.to_s)} #{force}") end |
#drop_table(table_name, **options) ⇒ Object
Simply add the schema to the table name when dropping a table
97 98 99 100 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 97 def drop_table(table_name, **) table_name = "#{[:schema]}.#{table_name}" if [:schema].present? super table_name, ** end |
#drop_type(name, options = {}) ⇒ Object
Drops a type.
26 27 28 29 30 31 32 33 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 26 def drop_type(name, = {}) force = .fetch(:force, '').upcase check = 'IF EXISTS' if .fetch(:check, true) execute <<-SQL.squish DROP TYPE #{check} #{quote_type_name(name, [:schema])} #{force} SQL end |
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
72 73 74 75 76 77 78 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 72 def enum_values(name) select_values(<<-SQL.squish, 'SCHEMA') SELECT enumlabel FROM pg_enum WHERE enumtypid = #{quote(name)}::regtype::oid ORDER BY enumsortorder SQL end |
#quoted_scope(name = nil, type: nil) ⇒ Object
When dumping the schema we need to add all schemas, not only those active for the current schema_search_path
112 113 114 115 116 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 112 def quoted_scope(name = nil, type: nil) return super unless name.nil? super.merge(schema: "ANY ('{#{user_defined_schemas.join(',')}}')") end |
#rename_type(type_name, new_name, options = {}) ⇒ Object
Renames a type.
36 37 38 39 40 41 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 36 def rename_type(type_name, new_name, = {}) execute <<-SQL.squish ALTER TYPE #{quote_type_name(type_name, [:schema])} RENAME TO #{Quoting::Name.new(nil, new_name.to_s).quoted} SQL end |
#table_options(table_name) ⇒ Object
Add the schema option when extracting table options
103 104 105 106 107 108 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 103 def (table_name) parts = table_name.split('.').reverse return super unless parts.size == 2 && parts[1] != 'public' (super || {}).merge(schema: parts[1]) end |
#valid_table_definition_options ⇒ Object
Add schema and inherits as one of the valid options for table definition
127 128 129 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 127 def super + [:schema, :inherits] end |