activerecord-dsql-adapter 0.1.4 → 0.1.7

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: a2a6400458dc398fea1b2033dd3fe8c8e488808c71f459dfe693cc905b8ceebc
4
- data.tar.gz: 50eceb300ff89ca6f93aff37165794a32760a3d242f84be60779d0a8a00d3302
3
+ metadata.gz: 81afc0eadfb8cdb952f08d241f353bb1f1c5d46a50a6b09d565077b1c246eb1a
4
+ data.tar.gz: 7d7f733127949fcb07b775341a5eee5d53a87b5c557e614e3c2bc72bd99b7c7b
5
5
  SHA512:
6
- metadata.gz: abcb7ee0f219fbf922ee539b03becb54bdcdb600a0685bde8415c0886319296c90ea8c15d5ee0ccd8428f162c1d1b2bb597d42e4c1e751ab14f8bfd8e91d8987
7
- data.tar.gz: 664df396b1edb20293c1a22bec8647a321f0f4224e8e6e352ed78b51568774fee07dcee443b48691d6ce25cf4eef6f694066792db34b8e50b0867ab9cd7c2d91
6
+ metadata.gz: ce09be3cd960b9fecb8030eba9f1a211e8ca45724756b38d532659a91657fc9301813c9f1bff0ab1fd2c1872c26bd8537f35ece7aa8d17291f012647219f47e2
7
+ data.tar.gz: 4ae7f099010634f3bf477789557b0815610eb270a5370918d99eb7386f9c35ead5bef2f22e03e5d54d48abae246f354af7d7e3c66ceb9f106ac6a3449eebc5f1
data/README.md CHANGED
@@ -11,12 +11,17 @@ bundle add activerecord-dsql-adapter
11
11
  ```
12
12
 
13
13
  ## Usage
14
-
14
+ Below is a suggested database configuration.
15
+ Optimistic Concurrency Conflicts can be automatically retried.
16
+ Generated tokens for DSQL expire after 1 hour so any connections should be invalidated before then.
15
17
  ```
16
18
  # config/database.yml
17
19
  development:
18
20
  adapter: dsql
19
21
  host: abc123.dsql.us-east-1.on.aws
22
+ occ_max_retries: 3
23
+ max_lifetime_minutes: 45
24
+ sslmode: require
20
25
  ```
21
26
 
22
27
  Credentials available in ENV or inside your `~/.aws/config` file will be used to generate an appropriate AWS signature as a password:
@@ -35,6 +40,81 @@ dsql-example(dev)> ActiveRecord::Base.connection.execute("SELECT 1")
35
40
  => #<PG::Result:0x00000001238b39a8 status=PGRES_TUPLES_OK ntuples=1 nfields=1 cmd_tuples=1>
36
41
  ```
37
42
 
43
+ ```
44
+ rails dbconsole -p
45
+ psql (14.13 (Homebrew), server 16.5)
46
+ SSL connection (protocol: TLSv1.3, cipher: TLS_AES_128_GCM_SHA256, bits: 128, compression: off)
47
+ Type "help" for help.
48
+
49
+ [postgres] >
50
+ ```
51
+
52
+ ```
53
+ $ rails generate scaffold create_posts title:string body:text
54
+ invoke active_record
55
+ create db/migrate/20241204112954_create_posts.rb
56
+ create app/models/post.rb
57
+ invoke resource_route
58
+ route resources :posts
59
+ invoke scaffold_controller
60
+ create app/controllers/posts_controller.rb
61
+ invoke erb
62
+ create app/views/posts
63
+ create app/views/posts/index.html.erb
64
+ create app/views/posts/edit.html.erb
65
+ create app/views/posts/show.html.erb
66
+ create app/views/posts/new.html.erb
67
+ create app/views/posts/_form.html.erb
68
+ create app/views/posts/_post.html.erb
69
+ invoke resource_route
70
+ invoke helper
71
+ create app/helpers/posts_helper.rb
72
+
73
+ $ rails db:migrate
74
+ == 20241204112954 CreatePosts: migrating ======================================
75
+ -- create_table(:posts)
76
+ -> 0.3887s
77
+ == 20241204112954 CreatePosts: migrated (0.3889s) =============================
78
+
79
+ $ rails console
80
+ Loading development environment (Rails 7.2.2)
81
+
82
+ pry(main)> post = Post.create!(title: "Hello, world!")
83
+ TRANSACTION (218.9ms) BEGIN
84
+ Post Create (530.9ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["title", "Hello, world!"], ["body", nil], ["created_at", "2024-12-04 11:37:41.238422"], ["updated_at", "2024-12-04 11:37:41.238422"]]
85
+ TRANSACTION (276.7ms) COMMIT
86
+ => #<Post:0x00000001267f30d8 id: "5b642ccc-c5e1-4d7c-828b-70fc107bf6e9", title: "Hello, world!", body: nil, created_at: "2024-12-04 11:37:41.238422000 +0000", updated_at: "2024-12-04 11:37:41.238422000 +0000">
87
+
88
+ pry(main)> post.update!(body: "Should probably write something...")
89
+ TRANSACTION (221.5ms) BEGIN
90
+ Post Update (503.8ms) UPDATE "posts" SET "body" = $1, "updated_at" = $2 WHERE "posts"."id" = $3 [["body", "Should probably write something..."], ["updated_at", "2024-12-04 11:37:54.659673"], ["id", "5b642ccc-c5e1-4d7c-828b-70fc107bf6e9"]]
91
+ TRANSACTION (258.7ms) COMMIT
92
+ => true
93
+
94
+ pry(main)> post.destroy!
95
+ TRANSACTION (217.6ms) BEGIN
96
+ Post Destroy (439.5ms) DELETE FROM "posts" WHERE "posts"."id" = $1 [["id", "5b642ccc-c5e1-4d7c-828b-70fc107bf6e9"]]
97
+ TRANSACTION (230.3ms) COMMIT
98
+ => #<Post:0x00000001267f30d8 id: "5b642ccc-c5e1-4d7c-828b-70fc107bf6e9", title: "Hello, world!", body: "Should probably write something...", created_at: "2024-12-04 11:37:41.238422000 +0000", updated_at: "2024-12-04 11:37:54.659673000 +0000">
99
+ ```
100
+
101
+ ```ruby
102
+ # db/schema.rb
103
+
104
+ ActiveRecord::Schema[7.2].define(version: 2024_12_04_112954) do
105
+ create_table "posts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
106
+ t.string "title"
107
+ t.text "body"
108
+ t.datetime "created_at", null: false
109
+ t.datetime "updated_at", null: false
110
+
111
+ t.unique_constraint ["id"], name: "posts_id_key"
112
+ end
113
+ end
114
+ ```
115
+
116
+ DSQL does not support `CREATE DATABASE` so `db:create` and `db:drop` do not work. But `db:prepare` can load the schema for you.
117
+
38
118
  ## Development
39
119
 
40
120
  After checking out the repo, run `script/setup` to install dependencies. You can also run `script/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ module DSQL
6
+ module SchemaStatements
7
+ def add_index_options(table_name, column_name, **options) # :nodoc:
8
+ options[:algorithm] = :async
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -5,19 +5,96 @@ require "aws-sdk-dsql"
5
5
  require "active_record"
6
6
  require "active_record/connection_adapters/postgresql_adapter"
7
7
 
8
+ require "aurora_dsql_pg"
9
+
8
10
  module ActiveRecord
9
11
  module ConnectionAdapters
10
12
  class DSQLAdapter < PostgreSQLAdapter
11
13
  ADAPTER_NAME = "DSQL"
12
14
 
15
+ include ActiveRecord::ConnectionAdapters::DSQL::SchemaStatements
16
+
17
+ def max_lifetime_seconds
18
+ if @config[:max_lifetime_minutes]
19
+ @config[:max_lifetime_minutes].to_f * 60
20
+ else
21
+ AuroraDsql::Pg::CONFIG[:max_lifetime].to_i
22
+ end
23
+ end
24
+
25
+ def token_expired?
26
+ !@token_refreshed_at || Time.now.utc - @token_refreshed_at >= max_lifetime_seconds
27
+ end
28
+
29
+ def active?
30
+ return false if token_expired? && !in_transaction?
31
+ super
32
+ end
33
+
34
+ def occ_max_retries
35
+ @config[:occ_max_retries].to_i
36
+ end
37
+
38
+ def occ_retry_config
39
+ AuroraDsql::Pg::OCCRetry::DEFAULT_CONFIG.merge(max_retries: occ_max_retries)
40
+ end
41
+
42
+ private
43
+
44
+ def reconnect
45
+ @raw_connection = nil
46
+ connect
47
+ @token_refreshed_at = Time.now.utc
48
+ end
49
+
50
+ public
51
+
52
+ # we cannot reconnect if in the middle of a transaction
53
+ # as a safety, put padding in max_lifetime_minutes
54
+ def transaction(requires_new: nil, isolation: nil, joinable: true, &block)
55
+ AuroraDsql::Pg::OCCRetry.retry_on_occ(occ_retry_config, logger: ActiveRecord::Base.logger) do
56
+ reconnect! if token_expired?
57
+ super(requires_new:, isolation:, joinable:, &block)
58
+ end
59
+ end
60
+
61
+ def with_raw_connection(allow_retry: false, materialize_transactions: true)
62
+ reconnect! if token_expired? && !in_transaction?
63
+ super(allow_retry:, materialize_transactions:) do |conn|
64
+ yield conn
65
+ end
66
+ end
67
+
13
68
  class << self
14
69
  def new_client(conn_params)
15
- conn_params[:sslmode] ||= "require"
16
- conn_params[:dbname] ||= "postgres"
17
- conn_params[:user] ||= "admin"
18
- conn_params[:password] ||= generate_password(conn_params)
19
-
20
- super(conn_params)
70
+ # AuroraDsql::Pg.connect forces sslmode: verify-full!
71
+ # Instead of using that we re-write it here with an optional sslmode override.
72
+ # In development mode using sslmode: require is easier to set up than verify-full.
73
+ # AuroraDsql::Pg.connect(**conn_params)
74
+
75
+ config = conn_params
76
+ options = {}
77
+ resolved = AuroraDsql::Pg::Config.from(config, **options).resolve
78
+ token = AuroraDsql::Pg::Token.generate(
79
+ host: resolved.host, region: resolved.region,
80
+ user: resolved.user, credentials: resolved.credentials_provider,
81
+ profile: resolved.profile, expires_in: resolved.token_duration
82
+ )
83
+ pg_params = resolved.to_pg_params(password: token)
84
+ pg_params[:sslmode] = conn_params[:sslmode] if conn_params[:sslmode]
85
+ ::PG.connect(pg_params)
86
+ rescue ::PG::Error => error
87
+ if conn_params && conn_params[:dbname] == "postgres"
88
+ raise ActiveRecord::ConnectionNotEstablished, error.message
89
+ elsif conn_params && conn_params[:dbname] && error.message.include?(conn_params[:dbname])
90
+ raise ActiveRecord::NoDatabaseError.db_error(conn_params[:dbname])
91
+ elsif conn_params && conn_params[:user] && error.message.include?(conn_params[:user])
92
+ raise ActiveRecord::DatabaseConnectionError.username_error(conn_params[:user])
93
+ elsif conn_params && conn_params[:host] && error.message.include?(conn_params[:host])
94
+ raise ActiveRecord::DatabaseConnectionError.hostname_error(conn_params[:host])
95
+ else
96
+ raise ActiveRecord::ConnectionNotEstablished, error.message
97
+ end
21
98
  end
22
99
 
23
100
  def dbconsole(config, options = {})
@@ -119,6 +196,10 @@ module ActiveRecord
119
196
  false
120
197
  end
121
198
 
199
+ def index_algorithms
200
+ { async: "ASYNC" }
201
+ end
202
+
122
203
  # Ignore DSQL sys schema.
123
204
  #
124
205
  # https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-systems-tables.html
@@ -12,6 +12,7 @@ module ActiveRecord
12
12
  extend ActiveSupport::Autoload
13
13
 
14
14
  autoload :SchemaDumper
15
+ autoload :SchemaStatements
15
16
  end
16
17
  end
17
18
  end
metadata CHANGED
@@ -1,49 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-dsql-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Cochran
8
- autorequire:
9
8
  bindir: bin
10
- cert_chain:
11
- - |
12
- -----BEGIN CERTIFICATE-----
13
- MIIDKDCCAhCgAwIBAgIBAzANBgkqhkiG9w0BAQsFADA6MQ0wCwYDVQQDDARzajI2
14
- MRQwEgYKCZImiZPyLGQBGRYEc2oyNjETMBEGCgmSJomT8ixkARkWA2NvbTAeFw0y
15
- NDA3MjIwNjA3MjdaFw0yNTA3MjIwNjA3MjdaMDoxDTALBgNVBAMMBHNqMjYxFDAS
16
- BgoJkiaJk/IsZAEZFgRzajI2MRMwEQYKCZImiZPyLGQBGRYDY29tMIIBIjANBgkq
17
- hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsr60Eo/ttCk8GMTMFiPr3GoYMIMFvLak
18
- xSmTk9YGCB6UiEePB4THSSA5w6IPyeaCF/nWkDp3/BAam0eZMWG1IzYQB23TqIM0
19
- 1xzcNRvFsn0aQoQ00k+sj+G83j3T5OOV5OZIlu8xAChMkQmiPd1NXc6uFv+Iacz7
20
- kj+CMsI9YUFdNoU09QY0b+u+Rb6wDYdpyvN60YC30h0h1MeYbvYZJx/iZK4XY5zu
21
- 4O/FL2ChjL2CPCpLZW55ShYyrzphWJwLOJe+FJ/ZBl6YXwrzQM9HKnt4titSNvyU
22
- KzE3L63A3PZvExzLrN9u09kuWLLJfXB2sGOlw3n9t72rJiuBr3/OQQIDAQABozkw
23
- NzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU99dfRjEKFyczTeIz
24
- m3ZsDWrNC80wDQYJKoZIhvcNAQELBQADggEBAKcQ+MfpXXDmqboG36O8mXhJZJ8+
25
- zlUqM1QnoEG8XpH/29py4GzSgs6Sb+v/Z1SEArRDq0r1bKZuWiWxqEDnmSE94+pe
26
- A0Ct6Pv/paEhQCoPkGQNa74cH4tzkVHIA7WSwRL8wOz5vo++YBKH5H36nNqtLV7s
27
- kNJYsbeOL/w2icQ4b0w4KYgcs4bd6yrYdgcgsRd2sCYb2XFHjczYqw4iEwuUenNM
28
- G8WXc9zUMVewWkEjS9s++xFNb/7f4dFNXCoE3pgHjzctt7gYcJVacprsg925LYHO
29
- WsbnbEow7BPlYOZm3rDIsEYpWqxN0lR8BakO2ToJ5RAHXahFu4K4eJtAsLk=
30
- -----END CERTIFICATE-----
31
- date: 2024-12-04 00:00:00.000000000 Z
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
32
11
  dependencies:
33
12
  - !ruby/object:Gem::Dependency
34
13
  name: activerecord
35
14
  requirement: !ruby/object:Gem::Requirement
36
15
  requirements:
37
- - - "~>"
16
+ - - ">="
38
17
  - !ruby/object:Gem::Version
39
18
  version: 7.2.0
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9'
40
22
  type: :runtime
41
23
  prerelease: false
42
24
  version_requirements: !ruby/object:Gem::Requirement
43
25
  requirements:
44
- - - "~>"
26
+ - - ">="
45
27
  - !ruby/object:Gem::Version
46
28
  version: 7.2.0
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
47
32
  - !ruby/object:Gem::Dependency
48
33
  name: pg
49
34
  requirement: !ruby/object:Gem::Requirement
@@ -58,6 +43,20 @@ dependencies:
58
43
  - - "~>"
59
44
  - !ruby/object:Gem::Version
60
45
  version: '1.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: aurora-dsql-ruby-pg
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.1'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.1'
61
60
  - !ruby/object:Gem::Dependency
62
61
  name: aws-sdk-dsql
63
62
  requirement: !ruby/object:Gem::Requirement
@@ -72,7 +71,6 @@ dependencies:
72
71
  - - ">="
73
72
  - !ruby/object:Gem::Version
74
73
  version: '0'
75
- description:
76
74
  email:
77
75
  - sj26@sj26.com
78
76
  executables: []
@@ -82,6 +80,7 @@ files:
82
80
  - README.md
83
81
  - lib/active_record/connection_adapters/dsql/railtie.rb
84
82
  - lib/active_record/connection_adapters/dsql/schema_dumper.rb
83
+ - lib/active_record/connection_adapters/dsql/schema_statements.rb
85
84
  - lib/active_record/connection_adapters/dsql_adapter.rb
86
85
  - lib/active_record/tasks/dsql_database_tasks.rb
87
86
  - lib/activerecord-dsql-adapter.rb
@@ -91,7 +90,6 @@ metadata:
91
90
  homepage_uri: https://github.com/sj26/activerecord-dsql-adapter
92
91
  source_code_uri: https://github.com/sj26/activerecord-dsql-adapter
93
92
  changelog_uri: https://github.com/sj26/activerecord-dsql-adapter/releases
94
- post_install_message:
95
93
  rdoc_options: []
96
94
  require_paths:
97
95
  - lib
@@ -106,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
104
  - !ruby/object:Gem::Version
107
105
  version: '0'
108
106
  requirements: []
109
- rubygems_version: 3.5.16
110
- signing_key:
107
+ rubygems_version: 4.0.21
111
108
  specification_version: 4
112
109
  summary: ActiveRecord adapter for AWS Aurora DSQL (PostgreSQL)
113
110
  test_files: []
checksums.yaml.gz.sig DELETED
Binary file
data.tar.gz.sig DELETED
Binary file
metadata.gz.sig DELETED
Binary file