From b77847c5ab3f200b7df915019e08abf23493adb4 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 10:29:37 +0000 Subject: [PATCH 1/6] TECH-18196: default timestamps to have null: false --- lib/declare_schema/dsl.rb | 6 ++-- .../migration_generator_spec.rb | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/declare_schema/dsl.rb b/lib/declare_schema/dsl.rb index 2589595c..e1a7bf0f 100644 --- a/lib/declare_schema/dsl.rb +++ b/lib/declare_schema/dsl.rb @@ -17,9 +17,9 @@ def initialize(model, **options) attr_reader :model - def timestamps - field(:created_at, :datetime, null: true) - field(:updated_at, :datetime, null: true) + def timestamps(**options) + field(:created_at, :datetime, null: false, **options) + field(:updated_at, :datetime, null: false, **options) end def optimistic_lock diff --git a/spec/lib/declare_schema/migration_generator_spec.rb b/spec/lib/declare_schema/migration_generator_spec.rb index b75d249f..2d068273 100644 --- a/spec/lib/declare_schema/migration_generator_spec.rb +++ b/spec/lib/declare_schema/migration_generator_spec.rb @@ -471,8 +471,8 @@ class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlo expect(Generators::DeclareSchema::Migration::Migrator.run).to( migrate_up(<<~EOS.strip) - add_column :adverts, :created_at, :datetime, null: true - add_column :adverts, :updated_at, :datetime, null: true + add_column :adverts, :created_at, :datetime, null: false + add_column :adverts, :updated_at, :datetime, null: false add_column :adverts, :lock_version, :integer#{lock_version_limit}, null: false, default: 1 EOS .and(migrate_down(<<~EOS.strip)) @@ -486,6 +486,30 @@ class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlo Advert.field_specs.delete(:created_at) Advert.field_specs.delete(:lock_version) + ### Timestamps with null: true + + # `updated_at` and `created_at` can be declared with the shorthand `timestamps` passed with null: true override + + class Advert < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock + declare_schema do + timestamps null: true + end + end + + expect(Generators::DeclareSchema::Migration::Migrator.run).to( + migrate_up(<<~EOS.strip) + add_column :adverts, :created_at, :datetime, null: true + add_column :adverts, :updated_at, :datetime, null: true + EOS + .and(migrate_down(<<~EOS.strip)) + remove_column :adverts, :updated_at + remove_column :adverts, :created_at + EOS + ) + + Advert.field_specs.delete(:updated_at) + Advert.field_specs.delete(:created_at) + ### Indices # You can add an index to a field definition From 201f481baa47fd1c739c420a620904c695a3fcac Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 10:37:53 +0000 Subject: [PATCH 2/6] TECH-18196: patch bug where validations fail if validate methods on values exist with non-zero arity --- lib/declare_schema/model.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/declare_schema/model.rb b/lib/declare_schema/model.rb index d1fbd538..e2ecf465 100644 --- a/lib/declare_schema/model.rb +++ b/lib/declare_schema/model.rb @@ -310,7 +310,7 @@ def _add_validations_for_field(name, type, args, options) # Support for custom validations if (type_class = DeclareSchema.to_class(type)) - if type_class.public_method_defined?("validate") + if type_class.public_method_defined?("validate") && type_class.instance_method("validate").arity.zero? validate do |record| v = record.send(name)&.validate record.errors.add(name, v) if v.is_a?(String) From 9c74bf2e00fd8424e6fd5c3017fde05743a486da Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 10:41:15 +0000 Subject: [PATCH 3/6] TECH-18196: update version and changelog --- CHANGELOG.md | 8 ++++++++ Gemfile.lock | 2 +- lib/declare_schema/version.rb | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 522f3fc4..e213e18d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.3.3] - Unreleased +### Changed +- The `timestamps` DSL method to create `created_at` and `updated_at` columns now defaults to `null: false` for `datetime` columns +- The `timestamps` DSL method to allow additional options to be passed to the `datetime` fields + +### Fixed +- Fixed a bug where `#validate` methods on core object classes with required arguments were cuasing model validations to fail + ## [2.3.2] - 2025-02-21 ### Fixed - Removed require of `activesupport/proxy_object` which is removed in Rails 8.0 diff --git a/Gemfile.lock b/Gemfile.lock index f69ffdc4..25fdcc72 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - declare_schema (2.3.2) + declare_schema (2.3.3) rails (>= 6.0) GEM diff --git a/lib/declare_schema/version.rb b/lib/declare_schema/version.rb index dca75add..a2ecb6f4 100644 --- a/lib/declare_schema/version.rb +++ b/lib/declare_schema/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DeclareSchema - VERSION = "2.3.2" + VERSION = "2.3.3" end From 69fff1a388d968f08d6db95bf7a4985312e9955e Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 13:27:59 +0000 Subject: [PATCH 4/6] TECH-18196: pre-release for testing --- Gemfile.lock | 2 +- lib/declare_schema/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 25fdcc72..9d14db57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - declare_schema (2.3.3) + declare_schema (2.3.3.pre.1) rails (>= 6.0) GEM diff --git a/lib/declare_schema/version.rb b/lib/declare_schema/version.rb index a2ecb6f4..b397947a 100644 --- a/lib/declare_schema/version.rb +++ b/lib/declare_schema/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DeclareSchema - VERSION = "2.3.3" + VERSION = "2.3.3.pre.1" end From fd8668ea578748cf02fb002ae8d5885acae4d762 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 17:44:01 +0000 Subject: [PATCH 5/6] TECH-18196: move to major version bump --- CHANGELOG.md | 2 +- Gemfile.lock | 2 +- lib/declare_schema/version.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e213e18d..264ed12d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.3.3] - Unreleased +## [3.0.0] - Unreleased ### Changed - The `timestamps` DSL method to create `created_at` and `updated_at` columns now defaults to `null: false` for `datetime` columns - The `timestamps` DSL method to allow additional options to be passed to the `datetime` fields diff --git a/Gemfile.lock b/Gemfile.lock index 9d14db57..87c618db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - declare_schema (2.3.3.pre.1) + declare_schema (3.0.0.pre.1) rails (>= 6.0) GEM diff --git a/lib/declare_schema/version.rb b/lib/declare_schema/version.rb index b397947a..a553b391 100644 --- a/lib/declare_schema/version.rb +++ b/lib/declare_schema/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DeclareSchema - VERSION = "2.3.3.pre.1" + VERSION = "3.0.0.pre.1" end From d952e73c12a02ed30e4c4ae77c5be4a35d95f01c Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Tue, 8 Apr 2025 20:06:42 +0200 Subject: [PATCH 6/6] TECH-18196: fix changelog typo Co-authored-by: Tristan Starck --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 264ed12d..29369b4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0 - The `timestamps` DSL method to allow additional options to be passed to the `datetime` fields ### Fixed -- Fixed a bug where `#validate` methods on core object classes with required arguments were cuasing model validations to fail +- Fixed a bug where `#validate` methods on core object classes with required arguments was causing model validations to fail ## [2.3.2] - 2025-02-21 ### Fixed