rails-snowflake 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f0727305e2203bd51c71c99e038f7902d0abdf9f283bcf0dca93495c5f851bf
4
- data.tar.gz: 751f6d5c2e3329ad5be5c6104f8cc164af707181e01f3d428673736980f0539e
3
+ metadata.gz: 20b88f3269b47cff232d8328e024ae405f53a9c659d2ad56bbe9eb9cc29449ba
4
+ data.tar.gz: 2e6163a490501f5859d60adeaf31f1ddc87ddea9cba69b0eb7c936aac5801346
5
5
  SHA512:
6
- metadata.gz: 05771bac80703b818c3c86021624f0d68ce23ceb5c34cd2423de1d2ce65674cc3824540f706d56593b0f97b6bc6afcd7c01b7e542e18a24a625a777323979957
7
- data.tar.gz: 287eccd9577239bb6d2543383c3979483f52dc6272e6077681b56f7b86cf0ef94abbe8a11da01e7b90e7254c506d1e732253d048d003b2e79c345fc726ea8c13
6
+ metadata.gz: 9daf3fd3b436e7833d900d8d83751a2d48024428a4532828a4618f5f19fa7183ba094d967b7eb7b74216d3ca76f495409aef9e07924baae89f07e18c2e1224a3
7
+ data.tar.gz: 9cfbb4cea7a2d62d5114a5838f64fceca5517641f1797059073255ab1f376fb08fbba276eb04de9bd7d43fb06f38c6e4fa26b6726b1d9aa8556fb3bcf9e18235
data/README.md CHANGED
@@ -115,8 +115,8 @@ execute("ALTER TABLE table_name ALTER COLUMN id SET DEFAULT timestamp_id('table_
115
115
 
116
116
  ## Requirements
117
117
 
118
- - **Database**: PostgreSQL (uses PostgreSQL-specific functions)
119
- - **Rails**: 7.2+ (may work with earlier versions)
118
+ - **Database**: PostgreSQL >= 15
119
+ - **Rails**: 7.1+
120
120
  - **Ruby**: 3.2+
121
121
 
122
122
  ## How it Works
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
 
3
5
  APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
@@ -5,16 +5,14 @@ module Rails
5
5
  module ColumnMethods
6
6
  def snowflake(name, **options)
7
7
  if name == :id && !options[:primary_key]
8
- raise ArgumentError, "Cannot use t.snowflake :id directly. Use `create_table` with `id: false` and then `t.snowflake :id, primary_key: true`"
8
+ raise Error, "Cannot use t.snowflake :id directly. Use `create_table` with `id: false` and then `t.snowflake :id, primary_key: true`"
9
9
  end
10
10
 
11
- table_name = @name
12
-
13
- unless table_name
14
- raise ArgumentError, "Could not determine table name for Snowflake column. Make sure you're using it within a `create_table` block."
11
+ unless @name
12
+ raise Error, "Could not determine table name for Snowflake column. Make sure you're using it within a `create_table` block."
15
13
  end
16
14
 
17
- options[:default] = -> { "timestamp_id('#{table_name}'::text)" }
15
+ options[:default] = -> { "timestamp_id('#{@name}'::text)" }
18
16
 
19
17
  column(name, :bigint, **options)
20
18
  end
@@ -1,40 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Hook into Rails database tasks to ensure snowflake sequences exist
3
+ # Ensures that any tables using a snowflake primary key (:id with timestamp_id default)
4
+ # have their backing <table>_id_seq sequences present. This is necessary for tables
5
+ # created with `id: false` and a subsequent `t.snowflake :id, primary_key: true` where
6
+ # Rails does not auto-create the normal sequence.
4
7
  def ensure_snowflake_sequences
5
- return unless defined?(ActiveRecord::Base)
6
-
7
8
  begin
8
- if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
9
- Rails.logger.debug "Rails::Snowflake: Ensure sequences exist for `timestamp_id` columns"
10
- Rails::Snowflake::Id.ensure_id_sequences_exist
9
+ # Avoid establishing a new connection too early; skip if not connected yet.
10
+ return unless ActiveRecord::Base.connection_pool.connected?
11
+
12
+ adapter = ActiveRecord::Base.connection.adapter_name
13
+ unless adapter == "PostgreSQL"
14
+ Rails.logger.debug("Rails::Snowflake: Skipping sequence check (adapter=#{adapter})") if defined?(Rails)
15
+ return
11
16
  end
12
- rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
13
- Rails.logger.warn "Rails::Snowflake: Could not ensure sequences: #{e.message}"
14
- end
15
- end
16
17
 
17
- # Enhance existing Rails database tasks by adding our hook to them
18
- if Rake::Task.task_defined?("db:migrate")
19
- Rake::Task["db:migrate"].enhance do
20
- ensure_snowflake_sequences
21
- end
22
- end
18
+ Rails.logger.debug "Rails::Snowflake: Ensuring sequences for timestamp_id columns" if defined?(Rails)
23
19
 
24
- if Rake::Task.task_defined?("db:schema:load")
25
- Rake::Task["db:schema:load"].enhance do
26
- ensure_snowflake_sequences
20
+ Rails::Snowflake::Id.ensure_id_sequences_exist
21
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished => e
22
+ Rails.logger.debug "Rails::Snowflake: Skipping sequence ensure (#{e.class}: #{e.message})" if defined?(Rails)
23
+ rescue StandardError => e
24
+ Rails.logger.warn "Rails::Snowflake: Unexpected error while ensuring sequences: #{e.class}: #{e.message}" if defined?(Rails)
27
25
  end
28
26
  end
29
27
 
30
- if Rake::Task.task_defined?("db:structure:load")
31
- Rake::Task["db:structure:load"].enhance do
32
- ensure_snowflake_sequences
33
- end
34
- end
28
+ def enhance_snowflake_db_task(name)
29
+ return unless Rake::Task.task_defined?(name)
35
30
 
36
- if Rake::Task.task_defined?("db:seed")
37
- Rake::Task["db:seed"].enhance do
31
+ Rake::Task[name].enhance do
38
32
  ensure_snowflake_sequences
39
33
  end
40
34
  end
35
+
36
+ # Enhance a broad set of lifecycle tasks; migrate first so tests pick it up.
37
+ %w[
38
+ db:migrate
39
+ db:setup
40
+ db:prepare
41
+ db:reset
42
+ db:schema:load
43
+ db:structure:load
44
+ db:seed
45
+ db:test:prepare
46
+ ].each { |t| enhance_snowflake_db_task(t) }
@@ -26,12 +26,6 @@ module Rails
26
26
  def create_migration_file
27
27
  migration_template "install_snowflake_id.rb.erb", File.join(db_migrate_path, "install_snowflake_id.rb")
28
28
  end
29
-
30
- private
31
-
32
- def migration_version
33
- "[#{ActiveRecord::VERSION::STRING.to_f}]"
34
- end
35
29
  end
36
30
  end
37
31
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class InstallSnowflakeId < ActiveRecord::Migration<%= migration_version %>
3
+ class InstallSnowflakeId < ActiveRecord::Migration[<%= ActiveRecord::VERSION::MAJOR %>.<%= ActiveRecord::VERSION::MINOR %>]
4
4
  def up
5
5
  # Create the timestamp_id PostgreSQL function
6
6
  Rails::Snowflake::Id.define_timestamp_id
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rails
2
4
  module Snowflake
3
5
  class Railtie < ::Rails::Railtie
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rails
2
4
  module Snowflake
3
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # desc "Explaining what the task does"
2
4
  # task :snowflake_id do
3
5
  # # Task goes here
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-snowflake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Eduardo Kowalski
@@ -15,14 +15,20 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '7.2'
18
+ version: '7.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '8.2'
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
22
25
  requirements:
23
26
  - - ">="
24
27
  - !ruby/object:Gem::Version
25
- version: '7.2'
28
+ version: '7.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '8.2'
26
32
  description: A Rails plugin that provides a simple way to generate unique Snowflake
27
33
  IDs for your ActiveRecord models.
28
34
  email: