planetscale_rails 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b6092e8239253b330f4864fa3388c9826c326493d8d585a47e8bcb310e7dce2
4
- data.tar.gz: b7457fdeb6ee47845266c63cd4baf13e80ee76746a121bf9644f3b5b9ad677c1
3
+ metadata.gz: 6b12ba63e8011c5a5838a651e7c6c89f4784bdf0729e571e1ce438aecf257dad
4
+ data.tar.gz: 2b90fe7b6861488cff954a6793a1fd30793b2c5690805ea0c1558663da2245ea
5
5
  SHA512:
6
- metadata.gz: f104e35e0864e821d6e885d9f0d5b7af5067c5bea40c69440b60c1fc3d7dea6ecd2bcf3aad11dae9a606ca1d7b8d649989bcd4633ec5f805ba68c90d7bbd5bfa
7
- data.tar.gz: 20aba5b8f3f8b9c7f15c0a95bc0f18bbea1a371258e293d1264599e91e015d6a721cb874594c8372cad990f9552dff724810fdaaeb867b0b223b961f1afe39a1
6
+ metadata.gz: ff86c5cc8e0f75a5455ad9672decf0e3be090405fc5169c431a7f03c2960ba4ffb330bb26feade3a3ff0a2cafd97b1717ad8de89ac8a00860c8360eb33508638
7
+ data.tar.gz: 1460c3295ae6c8cb006d0a4150e15d7bf02d1eca4e7974b795a096e79ecb7b33f1f9c7f6890ed307394310c9bd5bf5acd229656dacd2132fbc567d4f0cdb4e5a
data/README.md CHANGED
@@ -90,6 +90,15 @@ We recommend using GitHub Actions to automate the creation of PlanetScale branch
90
90
 
91
91
  If your application has minimal data and schema changes are a low risk event, then running `psdb:migrate` directly against production is perfectly fine. As your datasize grows and your application becomes busier, the risk of schema changes increase and we highly recommend using the deploy request flow. It's the best way available to safely migrate your schema.
92
92
 
93
+ ## How to safely drop a column
94
+
95
+ Before dropping the column in your database, there are a couple steps to take in your application to safely remove the column without any risk to your production application.
96
+
97
+ 1. Remove all references to the column in your code.
98
+ 2. Add the column name to `self.ignored_columns += %w(column_name)` in the model. [More on ignored_columns](https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-ignored_columns-3D).
99
+ 3. Deploy this code to production. This ensures your application is no longer making use of the column in production and it's removed from the schema cache.
100
+ 4. Follow the PlanetScale deploy request flow to drop the column.
101
+
93
102
  ## Usage with GitHub Actions
94
103
 
95
104
  See the [GitHub Actions examples](actions-example.md) doc for ways to automate your schema migrations with PlanetScale + Actions.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PlanetscaleRails
4
+ module Migration
5
+ module Current
6
+ # Allows users to set the `keyspace` option in their migration file.
7
+ # If the migration is being run against PlanetScale (i.e. `ENABLE_PSDB` is set), then we prepend the keyspace to the table name.
8
+ #
9
+ # For local MySQL databases, the keyspace is ignored.
10
+ def create_table(table_name, **options)
11
+ if ENV["ENABLE_PSDB"] && options[:keyspace].present?
12
+ table_name = "#{options[:keyspace]}.#{table_name}"
13
+ super(table_name, **options.except(:keyspace))
14
+ else
15
+ super(table_name, **options)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -7,8 +7,35 @@ require "colorize"
7
7
 
8
8
  databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
9
9
 
10
+ def find_git_directory
11
+ return ENV["GIT_DIR"] if ENV["GIT_DIR"] && !ENV["GIT_DIR"].empty?
12
+
13
+ # it's not set as an env, so let's try to find it
14
+ current_path = Pathname.new(Dir.pwd)
15
+
16
+ until current_path.root?
17
+ git_dir = current_path.join(".git")
18
+ return current_path.to_s if git_dir.exist? # this can be a directory or a file (worktree)
19
+
20
+ current_path = current_path.parent
21
+ end
22
+
23
+ nil
24
+ end
25
+
26
+ def load_pscale_config
27
+ if File.exist?(".pscale.yml")
28
+ return YAML.load_file(".pscale.yml")
29
+ end
30
+
31
+ # otherwise, look for a git root directory and load it from there
32
+ git_dir = find_git_directory
33
+ pscale_yaml_path = File.join(git_dir, ".pscale.yml")
34
+ YAML.load_file(pscale_yaml_path)
35
+ end
36
+
10
37
  def puts_deploy_request_instructions
11
- ps_config = YAML.load_file(".pscale.yml")
38
+ ps_config = load_pscale_config
12
39
  database = ps_config["database"]
13
40
  branch = ps_config["branch"]
14
41
  org = ps_config["org"]
@@ -25,7 +52,7 @@ def delete_password
25
52
  password_id = ENV["PSCALE_PASSWORD_ID"]
26
53
  return unless password_id
27
54
 
28
- ps_config = YAML.load_file(".pscale.yml")
55
+ ps_config = load_pscale_config
29
56
  database = ps_config["database"]
30
57
  branch = ps_config["branch"]
31
58
 
@@ -56,7 +83,7 @@ namespace :psdb do
56
83
  end
57
84
 
58
85
  def create_connection_string
59
- ps_config = YAML.load_file(".pscale.yml")
86
+ ps_config = load_pscale_config
60
87
  database = ps_config["database"]
61
88
  branch = ps_config["branch"]
62
89
 
@@ -85,7 +112,7 @@ namespace :psdb do
85
112
  adapter = "trilogy"
86
113
  end
87
114
 
88
- url = "#{adapter}://#{username}:#{password}@#{host}:3306/#{database}?ssl_mode=VERIFY_IDENTITY"
115
+ url = "#{adapter}://#{username}:#{password}@#{host}:3306/@primary?ssl_mode=VERIFY_IDENTITY"
89
116
 
90
117
  # Check common CA paths for certs.
91
118
  ssl_ca_path = %w[/etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/ca-bundle.pem /etc/ssl/cert.pem].find { |f| File.exist?(f) }
@@ -108,6 +135,7 @@ namespace :psdb do
108
135
  task "create_creds" => %i[environment check_ci] do
109
136
  ENV["PSCALE_DATABASE_URL"] = create_connection_string
110
137
  ENV["DISABLE_SCHEMA_DUMP"] = "true"
138
+ ENV["ENABLE_PSDB"] = "true"
111
139
  end
112
140
 
113
141
  desc "Connects to the current PlanetScale branch and runs rails db:migrate"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlanetscaleRails
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support"
4
+
3
5
  require_relative "planetscale_rails/version"
6
+ require_relative "planetscale_rails/migration"
4
7
 
5
8
  module PlanetscaleRails
6
9
  require "planetscale_rails/railtie" if defined?(Rails)
7
10
  end
11
+
12
+ ActiveSupport.on_load(:active_record) do
13
+ ActiveRecord::Migration::Current.prepend(PlanetscaleRails::Migration::Current)
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planetscale_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Coutermarsh
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-01-15 00:00:00.000000000 Z
12
+ date: 2024-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize
@@ -71,6 +71,7 @@ files:
71
71
  - bin/setup
72
72
  - lib/Rakefile
73
73
  - lib/planetscale_rails.rb
74
+ - lib/planetscale_rails/migration.rb
74
75
  - lib/planetscale_rails/railtie.rb
75
76
  - lib/planetscale_rails/tasks/psdb.rake
76
77
  - lib/planetscale_rails/version.rb