Skip to content

Commit

Permalink
Check created_at (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored Jul 10, 2024
1 parent 92cff65 commit cc899f6
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy:
matrix:
ruby:
- '3.2.2'
- '3.3.3'
database:
- mysql
- sqlite3
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require:
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.2.1
TargetRubyVersion: 3.3.3
NewCops: enable

Style/StringLiterals:
Expand Down
13 changes: 9 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-02-09 11:25:12 UTC using RuboCop version 1.59.0.
# on 2024-07-10 09:34:11 UTC using RuboCop version 1.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 23

# Offense count: 3
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 38
Max: 42

# Offense count: 2
# Configuration parameters: IgnoredMetadata.
Expand All @@ -32,7 +37,7 @@ RSpec/ExampleLength:
RSpec/MultipleExpectations:
Max: 2

# Offense count: 6
# Offense count: 7
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 6
Expand All @@ -42,7 +47,7 @@ RSpec/SubjectStub:
Exclude:
- 'spec/integrations/migrations/mysql/fx_migration_spec.rb'

# Offense count: 4
# Offense count: 6
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

## [0.1.3] - 2024-07-10

- Fix Zeitwerk::NameError
- Check if created_at is present

## [0.1.2] - 2024-02-09

Configure The Behaviours When UUID Is Invalid
Expand Down
50 changes: 26 additions & 24 deletions lib/uuid_v7/patches/mysql/fk_helper.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# frozen_string_literal: true

module Alliantist
module Migrations
module UuidFkHelper
# update_fk_uuid_between_table(parent_table_name: :engines, child_table_name: :pistons)
def update_fk_uuid_between_table(parent_table_name:, child_table_name:)
table_name_parent = parent_table_name.to_s.to_sym # :engines
table_name_child = child_table_name.to_s.to_sym # :pistons
foreign_key_id = :"#{table_name_parent.to_s.singularize}_id" # :engine_id
foreign_key_uuid = :"#{table_name_parent.to_s.singularize}_uuid" # :engine_uuid
module UuidV7
module Patches
module Mysql
module FkHelper
# update_fk_uuid_between_table(parent_table_name: :engines, child_table_name: :pistons)
def update_fk_uuid_between_table(parent_table_name:, child_table_name:)
table_name_parent = parent_table_name.to_s.to_sym # :engines
table_name_child = child_table_name.to_s.to_sym # :pistons
foreign_key_id = :"#{table_name_parent.to_s.singularize}_id" # :engine_id
foreign_key_uuid = :"#{table_name_parent.to_s.singularize}_uuid" # :engine_uuid

add_column table_name_child, foreign_key_uuid, :binary, limit: 16, null: true # :pistons, :engine_uuid
add_column table_name_child, foreign_key_uuid, :binary, limit: 16, null: true # :pistons, :engine_uuid

# Update the foreign_key in child table
connection.execute <<-SQL.squish
UPDATE #{table_name_child} child
JOIN #{table_name_parent} parent ON child.#{foreign_key_id} = parent.id
SET child.#{foreign_key_uuid} = parent.uuid;
SQL
# Update the foreign_key in child table
connection.execute <<-SQL.squish
UPDATE #{table_name_child} child
JOIN #{table_name_parent} parent ON child.#{foreign_key_id} = parent.id
SET child.#{foreign_key_uuid} = parent.uuid;
SQL

# change_column_null :pistons, :engine_uuid, false
change_column_null table_name_child, foreign_key_uuid, false
# change_column_null :pistons, :engine_uuid, false
change_column_null table_name_child, foreign_key_uuid, false

# add_index :pistons, :engine_uuid
add_index table_name_child, foreign_key_uuid
# add_index :pistons, :engine_uuid
add_index table_name_child, foreign_key_uuid

# add_foreign_key :pistons, :engines, column: :engine_uuid, primary_key: :uuid, type: :binary
add_foreign_key table_name_child, table_name_parent, column: foreign_key_uuid, primary_key: :uuid, type: :binary
# add_foreign_key :pistons, :engines, column: :engine_uuid, primary_key: :uuid, type: :binary
add_foreign_key table_name_child, table_name_parent, column: foreign_key_uuid, primary_key: :uuid, type: :binary

# remove_column table_name_child, foreign_key_id
# rename_column table_name_child, foreign_key_uuid, foreign_key_id
# remove_column table_name_child, foreign_key_id
# rename_column table_name_child, foreign_key_uuid, foreign_key_id
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/uuid_v7/patches/mysql/fx_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def populate_uuid_field(table_name:, column_name:)
END
SQL

# Check for NULL created_at values
result = connection.exec_query(<<~SQL)
SELECT COUNT(*) AS count FROM #{table_name} WHERE created_at IS NULL;
SQL

raise ActiveRecord::RecordInvalid, "There are records with NULL created_at in #{table_name}" if (result.rows[0][0]).positive?

connection.execute <<~SQL
UPDATE #{table_name} SET #{column_name} = uuid_v7(created_at) WHERE #{column_name} IS NULL;
SQL
Expand Down
2 changes: 1 addition & 1 deletion lib/uuid_v7/types/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Data < ActiveModel::Type::Binary::Data
def initialize(value)
@value = value

super(value)
super
end

def hex
Expand Down
2 changes: 1 addition & 1 deletion lib/uuid_v7/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module UuidV7
VERSION = "0.1.2"
VERSION = "0.1.3"
end
2 changes: 1 addition & 1 deletion spec/uuid_v7/types/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Types
let(:undashed_uuid) { uuid.delete("-") }

it "serialize into Data object" do
expect(base_type.serialize(uuid)).is_a?(Data)
expect(base_type.serialize(uuid).is_a?(UuidV7::Types::Base::Data)).to be true
end

it "stripes the dashes" do
Expand Down
2 changes: 1 addition & 1 deletion uuid_v7.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |spec|

spec.homepage = "https://github.com/joel/uuid_v7"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.2.1"
spec.required_ruby_version = ">= 3.3.3"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down

0 comments on commit cc899f6

Please sign in to comment.