From 791f20167bf22ca43853d6cbbf29367d9b7ac96d Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 19 Apr 2017 12:08:38 +0900 Subject: Use `quoted_scope` rather than `@config[:database]` to respect current database Related #28399. --- .../lib/active_record/connection_adapters/mysql/schema_dumper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index 3e0afd9761..b66db0f6fb 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -59,9 +59,10 @@ module ActiveRecord $~[:expression].inspect end else + scope = quoted_scope(column.table_name) sql = "SELECT generation_expression FROM information_schema.columns" \ - " WHERE table_schema = #{quote(@config[:database])}" \ - " AND table_name = #{quote(column.table_name)}" \ + " WHERE table_schema = #{scope[:schema]}" \ + " AND table_name = #{scope[:name]}" \ " AND column_name = #{quote(column.name)}" select_value(sql, "SCHEMA").inspect end -- cgit v1.2.3 From 7695f35baa0f45c5742de03a9b26595b2bc49008 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 19 Apr 2017 12:55:24 +0900 Subject: Fix `extract_expression_for_virtual_column` for MariaDB I'm not sure why `Mysql2VirtualColumnTest#test_schema_dumping` passed previously. But now the test not pass at least in MariaDB 10.1.9. I fixed the regexp to respect `COLLATE`. ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/adapters/mysql2/virtual_column_test.rb -n test_schema_dumping Using mysql2 Run options: -n test_schema_dumping --seed 7131 F Finished in 0.466304s, 2.1445 runs/s, 4.2890 assertions/s. 1) Failure: Mysql2VirtualColumnTest#test_schema_dumping [test/cases/adapters/mysql2/virtual_column_test.rb:55]: Expected /t\.virtual\s+"upper_name",\s+type: :string,\s+as: "UPPER\(`name`\)"$/i to match "# This file is auto-generated from the current state of the database. Instead\n# of editing this file, please use the migrations feature of Active Record to\n# incrementally modify your database, and then regenerate this schema definition.\n#\n# Note that this schema.rb definition is the authoritative source for your\n# database schema. If you need to create the application database on another\n# system, you should be using db:schema:load, not running all the migrations\n# from scratch. The latter is a flawed and unsustainable approach (the more migrations\n# you'll amass, the slower it'll run and the greater likelihood for issues).\n#\n# It's strongly recommended that you check this file into your version control system.\n\nActiveRecord::Schema.define(version: 0) do\n\n create_table \"virtual_columns\", force: :cascade, options: \"ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci\" do |t|\n t.string \"name\"\n t.virtual \"upper_name\", type: :string, as: \n t.virtual \"name_length\", type: :integer, as: \"LENGTH(`name`)\", stored: true\n end\n\nend\n". 1 runs, 2 assertions, 1 failures, 0 errors, 0 skips ``` ``` > select @@version; +--------------------+ | @@version | +--------------------+ | 10.1.9-MariaDB-log | +--------------------+ 1 row in set (0.00 sec) ``` --- .../lib/active_record/connection_adapters/mysql/schema_dumper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index b66db0f6fb..e2ba0ba1a0 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -55,7 +55,7 @@ module ActiveRecord def extract_expression_for_virtual_column(column) if mariadb? create_table_info = create_table_info(column.table_name) - if %r/#{quote_column_name(column.name)} #{Regexp.quote(column.sql_type)} AS \((?.+?)\) #{column.extra}/m =~ create_table_info + if %r/#{quote_column_name(column.name)} #{Regexp.quote(column.sql_type)}(?: COLLATE \w+)? AS \((?.+?)\) #{column.extra}/ =~ create_table_info $~[:expression].inspect end else -- cgit v1.2.3