Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL and Postgres schema: retrieve columns' comments #2249

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (MySQL and PostreSQL).
: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
6 changes: 5 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,10 +577,14 @@ 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[:comment] = row.delete(:Comment)
row[:comment] = nil if row[:comment] == ""
row[:default] = row.delete(:Default)
row[:db_type] = row.delete(:Type)
row[:type] = schema_column_type(row[:db_type])
row[:extra] = extra
row.delete(:Collation)
row.delete(:Privileges)
[m.call(row.delete(:Field)), row]
end
end
Expand Down
1 change: 1 addition & 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),
SQL::Function.new(:col_description, pg_class[:oid], pg_attribute[:attnum]).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 Down
9 changes: 9 additions & 0 deletions spec/adapters/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
@db.schema(:dolls).map{|k, v| v[:db_type]}.must_equal %w"blob tinyblob mediumblob longblob blob"
end

it "should returns the columns' comments" do
@db.create_table(:dolls) do
Integer :a
Integer :b
end
@db.run("ALTER TABLE dolls CHANGE b b INT COMMENT 'blah'")
@db.schema(:dolls).map{|k, v| v[:comment]}.must_equal [nil, 'blah']
end

it "should include an :auto_increment schema attribute if auto incrementing" do
@db.create_table(:dolls) do
primary_key :n4
Expand Down
6 changes: 6 additions & 0 deletions spec/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,12 @@ def c.exec_prepared(*); super; nil end
@db.schema(:tmp_dolls).map{|_,v| v[:generated]}.must_equal [false, false, true]
end if DB.server_version >= 120000

it "should include :comment entry in schema for whether the column is commented" do
@db.create_table(:tmp_dolls){Integer :a; Integer :b}
@db.run("COMMENT ON COLUMN tmp_dolls.b IS 'blah'")
@db.schema(:tmp_dolls).map{|_,v| v[:comment]}.must_equal [nil, 'blah']
end

it "should support deferred primary key and unique constraints on columns" do
@db.create_table(:tmp_dolls){primary_key :id, :primary_key_deferrable=>true; Integer :i, :unique=>true, :unique_deferrable=>true}
@db[:tmp_dolls].insert(:i=>10)
Expand Down
Loading