Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4227 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Schema dumper output incorrect for date columns with default values

Reported by: bhelmkamp@gmail.com Assigned to: David
Priority: high Milestone: 1.2
Component: ActiveRecord Version: 1.0.0
Severity: major Keywords:
Cc:

Description

If you create a table that has a date column with a default date, when the schema dumper tries to write out schema.rb, it will come out like this:

create_table "members", :force => true do |t|
  [snip]
  t.column "entered_seat", :date, :default => #<Date: 4172615/2,0,2299161>, :null => false
  [snip]
end

The problem is rooted in line 90 of schema_dumper.rb which states:

tbl.print ", :default => #{column.default.inspect}" if !column.default.nil?

Attachments

4227.diff (1.1 kB) - added by Caio Chassot <caio@v2studio.com> on 03/14/06 18:29:15.
patch
4227.2.diff (1.1 kB) - added by Caio Chassot <caio@v2studio.com> on 03/14/06 18:37:52.
patch v2
4227.3.diff (1.2 kB) - added by Caio Chassot <caio@v2studio.com> on 03/18/06 08:49:31.
patch v3
4227.4.diff (1.1 kB) - added by Caio Chassot <caio@v2studio.com> on 03/18/06 08:50:02.
patch v4

Change History

03/14/06 18:08:12 changed by Caio Chassot <caio@v2studio.com>

  • keywords set to needs_review.

The output for time and datetime is also wrong, although in a different way:

    t.column "datetime_test", :datetime, :default => Tue May 24 19:12:00 BRT 2005
    t.column "date_test", :date, :default => #<Date: 4907029/2,0,2299161>
    t.column "time_test", :time, :default => Sat Jan 01 19:12:00 BRST 2000

03/14/06 18:29:15 changed by Caio Chassot <caio@v2studio.com>

  • attachment 4227.diff added.

patch

03/14/06 18:31:42 changed by Caio Chassot <caio@v2studio.com>

  • summary changed from Schema dumper output incorrect for date columns with default values to [PATCH] Schema dumper output incorrect for date columns with default values.

I patched it.

I went for readability but before that I came up with the following code. Sad to throw it away, I'm posting it here:

default = column.default
if [Date, Time].any?{ |k| default.is_a?(k) }
  fmt_string = %w( %Y-%m-%d %H:%M ).zip(column.type.to_s.match(/(date)?(time)?/).captures).
    select{ |part, test| test }.map{ |part, test| part }.join(" ")
  default = default.strftime("'#{fmt_string}'") 
end
tbl.print ", :default => #{default}" if !default.nil?

03/14/06 18:33:15 changed by Caio Chassot <caio@v2studio.com>

  • summary changed from [PATCH] Schema dumper output incorrect for date columns with default values to Schema dumper output incorrect for date columns with default values.

buggy patch. sorry.

03/14/06 18:37:52 changed by Caio Chassot <caio@v2studio.com>

  • attachment 4227.2.diff added.

patch v2

03/14/06 18:38:50 changed by Caio Chassot <caio@v2studio.com>

  • summary changed from Schema dumper output incorrect for date columns with default values to [PATCH] Schema dumper output incorrect for date columns with default values.

ok, final patch is in.

03/14/06 18:42:36 changed by Caio Chassot <caio@v2studio.com>

Should I put timezone info in the generated date values?

03/14/06 20:56:15 changed by Caio Chassot <caio@v2studio.com>

If you decide to implement #4241, there's a combined patch in that ticket that includes this one modified to work with that code.

03/18/06 03:11:41 changed by anonymous

  • keywords deleted.

03/18/06 08:49:31 changed by Caio Chassot <caio@v2studio.com>

  • attachment 4227.3.diff added.

patch v3

03/18/06 08:50:02 changed by Caio Chassot <caio@v2studio.com>

  • attachment 4227.4.diff added.

patch v4

03/18/06 08:52:53 changed by Caio Chassot <caio@v2studio.com>

err... forgot the seconds.

Re my own question on timezone, I guess the answer is no as rails generally assumes all dates are in the same timezone.

03/24/06 22:11:45 changed by magnus@enarsson.nu

I had the same problem after upgrading to 1.1 RC1 as new default for 'schema_format' is ':ruby'. Came to the same conclusion, and the patch v4 solves the problem:

+1 solves the problem without side-effects for me!

10/21/06 22:53:43 changed by obrie

  • status changed from new to closed.
  • resolution set to fixed.

(In [4650]) Schema dumper quotes date :default values.