Skip to content

Commit

Permalink
MySQL and Postgres schema_parse_table: retrieve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Bahanix committed Nov 10, 2024
1 parent 0bda470 commit 020b436
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/reflection.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The hash may also contain entries for:
Database#schema takes a table symbol and returns column information in an array with each element being an array with two elements. The first elements of the subarray is a column symbol, and the second element is a hash of information about that column. The hash should include the following keys:

:allow_null :: Whether NULL/nil is an allowed value for this column. Used by the Sequel::Model typecasting code.
:comment:: The comment of the column.
:db_type :: The type of column the database provided, as a string. Used by the schema_dumper plugin for a more specific type translation.
:default :: The default value of the column, as either a string or nil. Uses a database specific format. Used by the schema_dumper plugin for converting to a ruby value.
:primary_key :: Whether this column is one of the primary key columns for the table. Used by the Sequel::Model code to determine primary key columns.
Expand Down
5 changes: 4 additions & 1 deletion lib/sequel/adapters/shared/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def schema_parse_table(table_name, opts)
im = input_identifier_meth(opts[:dataset])
table = SQL::Identifier.new(im.call(table_name))
table = SQL::QualifiedIdentifier.new(im.call(opts[:schema]), table) if opts[:schema]
metadata_dataset.with_sql("DESCRIBE ?", table).map do |row|
metadata_dataset.with_sql("SHOW FULL COLUMNS FROM ?", table).map do |row|
extra = row.delete(:Extra)
if row[:primary_key] = row.delete(:Key) == 'PRI'
row[:auto_increment] = !!(extra.to_s =~ /auto_increment/i)
Expand All @@ -577,8 +577,11 @@ def schema_parse_table(table_name, opts)
row[:generated] = !!(extra.to_s =~ /VIRTUAL|STORED|PERSISTENT/i)
end
row[:allow_null] = row.delete(:Null) == 'YES'
row[:collation] = row.delete(:Collation)
row[:comment] = row.delete(:Comment)
row[:default] = row.delete(:Default)
row[:db_type] = row.delete(:Type)
row[:privileges] = row.delete(:Privileges)
row[:type] = schema_column_type(row[:db_type])
row[:extra] = extra
[m.call(row.delete(:Field)), row]
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/adapters/shared/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ def _schema_ds
pg_attribute[:attname].as(:name),
SQL::Cast.new(pg_attribute[:atttypid], :integer).as(:oid),
SQL::Cast.new(basetype[:oid], :integer).as(:base_oid),
pg_description[:description].as(:comment),
SQL::Function.new(:format_type, basetype[:oid], pg_type[:typtypmod]).as(:db_base_type),
SQL::Function.new(:format_type, pg_type[:oid], pg_attribute[:atttypmod]).as(:db_type),
SQL::Function.new(:pg_get_expr, pg_attrdef[:adbin], pg_class[:oid]).as(:default),
Expand All @@ -1090,6 +1091,7 @@ def _schema_ds
left_outer_join(Sequel[:pg_type].as(:elementtype), :typarray=>Sequel[:pg_type][:oid]).
left_outer_join(:pg_attrdef, :adrelid=>Sequel[:pg_class][:oid], :adnum=>Sequel[:pg_attribute][:attnum]).
left_outer_join(:pg_index, :indrelid=>Sequel[:pg_class][:oid], :indisprimary=>true).
left_outer_join(:pg_description, :objoid => Sequel[:pg_attribute][:attrelid]).
where{{pg_attribute[:attisdropped]=>false}}.
where{pg_attribute[:attnum] > 0}.
order{pg_attribute[:attnum]}
Expand Down

0 comments on commit 020b436

Please sign in to comment.