rails-snowflake 0.1.2 → 0.2.0

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: b9748211e158cfa6d669e478b09f34c1349b3f73d1e424ed0c2cebdbeab0e612
4
- data.tar.gz: 295bfd2e78582989bd6c943d57480f14f9b4279e475fd58327eb9b31c4d6fb1b
3
+ metadata.gz: a0645a18c52266f1b80a691e7e5fcb07af9cc2ba5c4c5a82cac3a425e7ffe19b
4
+ data.tar.gz: 58b81ade2ed8ffd1367dcf504637a494034e6cc667f6be5ee33ac5b3afaadd5d
5
5
  SHA512:
6
- metadata.gz: 4df342f41de3d1fc1efcb1112cf86587bd4721565ba6dadc115fc3653624f3980326017b0a4a0d5ffaac72198e392615a0e6e064ea24f3f8d2f997fa14b736e2
7
- data.tar.gz: ce95567f740c3e23afd2b6946784226ed13d8d0f38eb92091c11e3377817623a8746b85921218317b08dea51df91b04b6ed15e0a330af727987c3e848a8d8041
6
+ metadata.gz: b5c30b3c3eb18d0d9c7a7911b2abb082976f773ae0f6a83c53be1865314f211a30f70cdd970d9c8324969eaa1f080e932f073eefa492acb2f9205c1b544e70c1
7
+ data.tar.gz: 5a294aa27bb5e3f396dc58445d1b8d5ed9528fd11fc31eab454b7590accd33a2573348484355b3e2cf0645a924a87a03dc8f7ecaf29c9797a273ab512da90b49
data/README.md CHANGED
@@ -30,14 +30,13 @@ rails generate rails_snowflake:install
30
30
 
31
31
  ## Quick Start
32
32
 
33
- **That's it!** Just use `t.snowflake` in your migrations and everything works automatically.
33
+ **That's it!** Just use `id: :snowflake` or `t.snowflake` in your migrations and everything works automatically.
34
34
 
35
35
  ### For Snowflake ID as primary key:
36
36
  ```ruby
37
- class CreateUsers < ActiveRecord::Migration[8.0]
37
+ class CreateUsers < ActiveRecord::Migration
38
38
  def change
39
- create_table :users, id: false do |t|
40
- t.snowflake :id, primary_key: true # Snowflake primary key
39
+ create_table :users, id: :snowflake do |t|
41
40
  t.string :name
42
41
  t.timestamps
43
42
  end
@@ -45,11 +44,8 @@ class CreateUsers < ActiveRecord::Migration[8.0]
45
44
  end
46
45
  ```
47
46
 
48
- **Note**: When using `t.snowflake :id` directly, Rails will complain about redefining the primary key. Always use `create_table :table_name, id: false` when you want a snowflake primary key.
49
-
50
- ### For additional snowflake columns (non-primary key):
51
47
  ```ruby
52
- class CreatePosts < ActiveRecord::Migration[8.0]
48
+ class CreatePosts < ActiveRecord::Migration
53
49
  def change
54
50
  create_table :posts do |t|
55
51
  t.string :title
@@ -116,7 +112,7 @@ execute("ALTER TABLE table_name ALTER COLUMN id SET DEFAULT timestamp_id('table_
116
112
  ## Requirements
117
113
 
118
114
  - **Database**: PostgreSQL >= 15
119
- - **Rails**: 7.1+
115
+ - **Rails**: 7.2+
120
116
  - **Ruby**: 3.2+
121
117
 
122
118
  ## How it Works
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Snowflake
5
+ module NativeDatabaseType
6
+ def native_database_types
7
+ super.merge(
8
+ snowflake: { name: "bigint" }
9
+ )
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,17 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "types/snowflake"
4
+ require_relative "table_definition"
5
+ require_relative "native_database_type"
6
+
3
7
  module Rails
4
8
  module Snowflake
5
9
  class Railtie < ::Rails::Railtie
6
- initializer "snowflake_id.register_field_type" do
10
+ initializer "snowflake.register_table_definitions" do
7
11
  ActiveSupport.on_load(:active_record) do
8
- ActiveRecord::ConnectionAdapters::TableDefinition.prepend(Rails::Snowflake::ColumnMethods)
12
+ ActiveRecord::Type.register(:snowflake, Rails::Snowflake::Types::Snowflake, adapter: :postgresql)
9
13
 
10
- if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
11
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:snowflake] = { name: "bigint" }
12
- else
13
- raise "Rails::Snowflake: Unsupported database adapter. Only PostgreSQL is supported."
14
- end
14
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Rails::Snowflake::NativeDatabaseType)
15
+ ActiveRecord::ConnectionAdapters::TableDefinition.prepend(Rails::Snowflake::TableDefinition)
16
+ ActiveRecord::ConnectionAdapters::Table.prepend(Rails::Snowflake::TableDefinition)
15
17
  end
16
18
  end
17
19
 
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Snowflake
5
+ module TableDefinition
6
+ def primary_key(name, type = :primary_key, **options)
7
+ if type == :snowflake
8
+ options[:default] = snowflake_default_function
9
+ end
10
+
11
+ super
12
+ end
13
+
14
+ def snowflake(column_name, **options)
15
+ if column_name == :id
16
+ raise Error, "Cannot use t.snowflake :id directly. Use `create_table :table_name, id: :snowflake` instead."
17
+ end
18
+
19
+ unless @name
20
+ raise Error, "Could not determine table name for Snowflake column. Make sure you're using it within a `create_table` block."
21
+ end
22
+
23
+ options[:default] = snowflake_default_function
24
+ options[:index] ||= true
25
+
26
+ column(column_name, :snowflake, **options)
27
+ end
28
+
29
+ private
30
+
31
+ def snowflake_default_function
32
+ -> { "timestamp_id('#{@name}'::text)" }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Snowflake
5
+ module Types
6
+ class Snowflake < ActiveRecord::Type::BigInteger
7
+ def type
8
+ :snowflake
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module Snowflake
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -3,7 +3,6 @@
3
3
  # Manually require all Rails::Snowflake modules
4
4
  require_relative "snowflake/version"
5
5
  require_relative "snowflake/id"
6
- require_relative "snowflake/column_methods"
7
6
  require_relative "snowflake/railtie"
8
7
  require_relative "snowflake/generators/install/install_generator"
9
8
 
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.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Eduardo Kowalski
@@ -35,12 +35,14 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - lib/rails/snowflake.rb
38
- - lib/rails/snowflake/column_methods.rb
39
38
  - lib/rails/snowflake/database_tasks.rb
40
39
  - lib/rails/snowflake/generators/install/install_generator.rb
41
40
  - lib/rails/snowflake/generators/install/templates/install_snowflake_id.rb.erb
42
41
  - lib/rails/snowflake/id.rb
42
+ - lib/rails/snowflake/native_database_type.rb
43
43
  - lib/rails/snowflake/railtie.rb
44
+ - lib/rails/snowflake/table_definition.rb
45
+ - lib/rails/snowflake/types/snowflake.rb
44
46
  - lib/rails/snowflake/version.rb
45
47
  - lib/tasks/rails/snowflake_tasks.rake
46
48
  homepage: https://github.com/luizkowalski/snowflake_id/
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rails
4
- module Snowflake
5
- module ColumnMethods
6
- def snowflake(name, **options)
7
- if name == :id && !options[:primary_key]
8
- raise Error, "Cannot use t.snowflake :id directly. Use `create_table` with `id: false` and then `t.snowflake :id, primary_key: true`"
9
- end
10
-
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."
13
- end
14
-
15
- options[:default] = -> { "timestamp_id('#{@name}'::text)" }
16
- options[:index] ||= true
17
-
18
- column(name, :bigint, **options)
19
- end
20
- end
21
- end
22
- end