From 51dc07907ed389cd5a7f4336fdfc88bba25df803 Mon Sep 17 00:00:00 2001 From: Julien <364924+Bahanix@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:08:40 +0100 Subject: [PATCH] MySQL and Postgres schema_parse_table: retrieve comments --- doc/reflection.rdoc | 1 + lib/sequel/adapters/shared/mysql.rb | 5 ++++- lib/sequel/adapters/shared/postgres.rb | 1 + spec/adapters/mysql_spec.rb | 9 +++++++++ spec/adapters/postgres_spec.rb | 6 ++++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/reflection.rdoc b/doc/reflection.rdoc index 907c26b197..00181e2f8b 100644 --- a/doc/reflection.rdoc +++ b/doc/reflection.rdoc @@ -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. diff --git a/lib/sequel/adapters/shared/mysql.rb b/lib/sequel/adapters/shared/mysql.rb index 57414faa44..9d5e91228e 100644 --- a/lib/sequel/adapters/shared/mysql.rb +++ b/lib/sequel/adapters/shared/mysql.rb @@ -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) @@ -577,10 +577,13 @@ 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[: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 diff --git a/lib/sequel/adapters/shared/postgres.rb b/lib/sequel/adapters/shared/postgres.rb index 5b128dbced..8ffac22c4e 100644 --- a/lib/sequel/adapters/shared/postgres.rb +++ b/lib/sequel/adapters/shared/postgres.rb @@ -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), diff --git a/spec/adapters/mysql_spec.rb b/spec/adapters/mysql_spec.rb index 7c3b21fc43..a1a759e04b 100644 --- a/spec/adapters/mysql_spec.rb +++ b/spec/adapters/mysql_spec.rb @@ -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 ["", 'blah'] + end + it "should include an :auto_increment schema attribute if auto incrementing" do @db.create_table(:dolls) do primary_key :n4 diff --git a/spec/adapters/postgres_spec.rb b/spec/adapters/postgres_spec.rb index e1c8519ee2..44297f5b57 100644 --- a/spec/adapters/postgres_spec.rb +++ b/spec/adapters/postgres_spec.rb @@ -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 if DB.server_version >= 120000 + 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)