Module: Torque::PostgreSQL::Adapter::SchemaStatements
- Included in:
- Torque::PostgreSQL::Adapter
- Defined in:
- lib/torque/postgresql/adapter/schema_statements.rb
Instance Method Summary collapse
-
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values.
-
#add_search_language(table, name, options = {}) ⇒ Object
Creates a column that stores the underlying language of the record so that a search vector can be created dynamically based on it.
-
#add_search_vector(table, name, columns, options = {}) ⇒ Object
Creates a column and setup a search vector as a virtual column.
-
#assume_migrated_upto_version(version) ⇒ Object
Add proper support for schema load when using versioned commands.
-
#data_source_sql(name = nil, type: nil) ⇒ Object
Fix the query to include the schema on tables names when dumping.
-
#drop_type(name, options = {}) ⇒ Object
Drops a type.
-
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
-
#insert_versions_sql(versions) ⇒ Object
Add proper support for schema load when using versioned commands.
-
#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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 63 def add_enum_values(name, values, = {}) name = sanitize_name_with_schema(name, ) 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 " ALTER TYPE \#{quote_type_name(name)}\n ADD VALUE \#{value} \#{reference}\n SQL\n\n before = false\n after = value\n end\nend\n".squish |
#add_search_language(table, name, options = {}) ⇒ Object
Creates a column that stores the underlying language of the record so that a search vector can be created dynamically based on it. It uses a ‘regconfig` type, so string conversions are mandatory
31 32 33 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 31 def add_search_language(table, name, = {}) add_column(table, name, :regconfig, ) end |
#add_search_vector(table, name, columns, options = {}) ⇒ Object
Creates a column and setup a search vector as a virtual column. The options are dev-friendly and controls how the vector function will be defined
Options
- :columns
-
The list of columns that will be used to create the search vector. It can be a single column, an array of columns, or a hash as a combination of column name and weight (A, B, C, or D).
- :language
-
Specify the language config to be used for the search vector. If a string is provided, then the value will be statically embedded. If a symbol is provided, then it will reference another column.
- :stored
-
Specify if the value should be stored in the database. As of now, PostgreSQL only supports ‘true`, which will create a stored column.
51 52 53 54 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 51 def add_search_vector(table, name, columns, = {}) = Builder.(columns: columns, **) add_column(table, name, .delete(:type), ) end |
#assume_migrated_upto_version(version) ⇒ Object
Add proper support for schema load when using versioned commands
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 141 def assume_migrated_upto_version(version) return super unless PostgreSQL.config.versioned_commands.enabled return super if (commands = pool.migration_context.migration_commands).empty? version = version.to_i migration_context = pool.migration_context migrated = migration_context.get_all_versions versions = migration_context.migrations.map(&:version) inserting = (versions - migrated).select { |v| v < version } inserting << version unless migrated.include?(version) return if inserting.empty? duplicated = inserting.tally.filter_map { |v, count| v if count > 1 } raise " Duplicate migration \#{duplicated.first}.\n Please renumber your migrations to resolve the conflict.\n MSG\n\n VersionedCommands::SchemaTable.new(pool).create_table\n execute insert_versions_sql(inserting)\nend\n".squish if duplicated.present? |
#data_source_sql(name = nil, type: nil) ⇒ Object
Fix the query to include the schema on tables names when dumping
128 129 130 131 132 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 128 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_type(name, options = {}) ⇒ Object
Drops a type
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 8 def drop_type(name, = {}) force = .fetch(:force, '').upcase check = 'IF EXISTS' if .fetch(:check, true) name = sanitize_name_with_schema(name, ) internal_exec_query(" DROP TYPE \#{check}\n \#{quote_type_name(name)} \#{force}\n SQL\nend\n".squish).tap { reload_type_map } |
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
86 87 88 89 90 91 92 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 86 def enum_values(name) select_values(" SELECT enumlabel FROM pg_enum\n WHERE enumtypid = \#{quote(name)}::regtype::oid\n ORDER BY enumsortorder\n SQL\nend\n".squish, 'SCHEMA') |
#insert_versions_sql(versions) ⇒ Object
Add proper support for schema load when using versioned commands
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 165 def insert_versions_sql(versions) return super unless PostgreSQL.config.versioned_commands.enabled commands = pool.migration_context.migration_commands.select do |migration| versions.include?(migration.version) end return super if commands.empty? table = quote_table_name(VersionedCommands::SchemaTable.new(pool).table_name) sql = super(versions - commands.map(&:version)) sql << "\nINSERT INTO #{table} (version, type, object_name) VALUES\n" sql << commands.map do |m| +"(#{quote(m.version)}, #{quote(m.type)}, #{quote(m.object_name)})" end.join(",\n") sql << ";" 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
118 119 120 121 122 123 124 125 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 118 def quoted_scope(name = nil, type: nil) return super unless name.nil? scope = super global = scope[:schema].start_with?('ANY (') scope[:schema] = "ANY ('{#{user_defined_schemas.join(',')}}')" scope end |
#rename_type(type_name, new_name, options = {}) ⇒ Object
Renames a type
20 21 22 23 24 25 26 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 20 def rename_type(type_name, new_name, = {}) type_name = sanitize_name_with_schema(type_name, ) internal_exec_query(" ALTER TYPE \#{quote_type_name(type_name)}\n RENAME TO \#{Quoting::Name.new(nil, new_name.to_s).quoted}\n SQL\nend\n".squish).tap { reload_type_map } |
#table_options(table_name) ⇒ Object
Add the schema option when extracting table options
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 96 def (table_name) = super if PostgreSQL.config.schemas.enabled table, schema = table_name.split('.').reverse if table.present? && schema.present? && schema != current_schema [:schema] = schema end end if [:options]&.start_with?('INHERITS (') .delete(:options) tables = inherited_table_names(table_name) [:inherits] = tables.one? ? tables.first : tables end end |
#valid_table_definition_options ⇒ Object
Add schema and inherits as one of the valid options for table definition
136 137 138 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 136 def super + [:schema, :inherits] end |