planetscale_rails 0.1.4 → 0.2.0

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: 1fac50d24f5d87d2be01f128dd42f63b75023d7761d5516c92ac8ad92f776fae
4
- data.tar.gz: 29a0fc6de3302ccd5b228c458facf7516147c2ca78b5f1717979bfbd6d8534d8
3
+ metadata.gz: 73da9fbd9cc5287b3a3088298413ff860891d8020ee1357fa4c0940ec113c6e2
4
+ data.tar.gz: f954e8a28f986965d20219cf631bc95030c6277f2652ade883f378c9bde26194
5
5
  SHA512:
6
- metadata.gz: 3993c4de9fb27abda2d3f9506f28a8f8c8b12312540fdee58d7838d37fb8af25fba31b2b7c291915e9f1488fc8586708f516e6149dede3fe26a4c22ce5e44cb9
7
- data.tar.gz: 93bcfb6f5d2f2bc0096de75029a2c12de9db97ab1fa591aa50e37d8d1548c23142a2dbda0079d51b8078d160f3535ba8bed3965042331e7b447136d69abdd997
6
+ metadata.gz: f964ab82aa2ce073f464d7d9e353714cae00a37f20a134a95a1650a7f9b78f69441297b639c702ddd48b2163ca363307d552567297536631263aae87c5b17e9e
7
+ data.tar.gz: 43537d76a32c25583c46d607e06b24b3f84ceb099e2083b13bbed6409bf43c0a74eb082b2682c32ff63748d45fbd8193d6517510338867dc05909753a603f274
data/.rubocop.yml CHANGED
@@ -12,6 +12,12 @@ Style/StringLiteralsInInterpolation:
12
12
  Layout/LineLength:
13
13
  Max: 180
14
14
 
15
+ Metrics/MethodLength:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Enabled: false
20
+
15
21
  Metrics/BlockLength:
16
22
  Enabled: false
17
23
 
data/Gemfile.lock CHANGED
@@ -1,74 +1,74 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- planetscale_rails (0.1.1)
4
+ planetscale_rails (0.2.0)
5
5
  colorize (~> 0.8.1)
6
- rails (~> 7.0)
6
+ rails (>= 6.0, < 8)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actioncable (7.0.4.2)
12
- actionpack (= 7.0.4.2)
13
- activesupport (= 7.0.4.2)
11
+ actioncable (7.0.6)
12
+ actionpack (= 7.0.6)
13
+ activesupport (= 7.0.6)
14
14
  nio4r (~> 2.0)
15
15
  websocket-driver (>= 0.6.1)
16
- actionmailbox (7.0.4.2)
17
- actionpack (= 7.0.4.2)
18
- activejob (= 7.0.4.2)
19
- activerecord (= 7.0.4.2)
20
- activestorage (= 7.0.4.2)
21
- activesupport (= 7.0.4.2)
16
+ actionmailbox (7.0.6)
17
+ actionpack (= 7.0.6)
18
+ activejob (= 7.0.6)
19
+ activerecord (= 7.0.6)
20
+ activestorage (= 7.0.6)
21
+ activesupport (= 7.0.6)
22
22
  mail (>= 2.7.1)
23
23
  net-imap
24
24
  net-pop
25
25
  net-smtp
26
- actionmailer (7.0.4.2)
27
- actionpack (= 7.0.4.2)
28
- actionview (= 7.0.4.2)
29
- activejob (= 7.0.4.2)
30
- activesupport (= 7.0.4.2)
26
+ actionmailer (7.0.6)
27
+ actionpack (= 7.0.6)
28
+ actionview (= 7.0.6)
29
+ activejob (= 7.0.6)
30
+ activesupport (= 7.0.6)
31
31
  mail (~> 2.5, >= 2.5.4)
32
32
  net-imap
33
33
  net-pop
34
34
  net-smtp
35
35
  rails-dom-testing (~> 2.0)
36
- actionpack (7.0.4.2)
37
- actionview (= 7.0.4.2)
38
- activesupport (= 7.0.4.2)
39
- rack (~> 2.0, >= 2.2.0)
36
+ actionpack (7.0.6)
37
+ actionview (= 7.0.6)
38
+ activesupport (= 7.0.6)
39
+ rack (~> 2.0, >= 2.2.4)
40
40
  rack-test (>= 0.6.3)
41
41
  rails-dom-testing (~> 2.0)
42
42
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
- actiontext (7.0.4.2)
44
- actionpack (= 7.0.4.2)
45
- activerecord (= 7.0.4.2)
46
- activestorage (= 7.0.4.2)
47
- activesupport (= 7.0.4.2)
43
+ actiontext (7.0.6)
44
+ actionpack (= 7.0.6)
45
+ activerecord (= 7.0.6)
46
+ activestorage (= 7.0.6)
47
+ activesupport (= 7.0.6)
48
48
  globalid (>= 0.6.0)
49
49
  nokogiri (>= 1.8.5)
50
- actionview (7.0.4.2)
51
- activesupport (= 7.0.4.2)
50
+ actionview (7.0.6)
51
+ activesupport (= 7.0.6)
52
52
  builder (~> 3.1)
53
53
  erubi (~> 1.4)
54
54
  rails-dom-testing (~> 2.0)
55
55
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
- activejob (7.0.4.2)
57
- activesupport (= 7.0.4.2)
56
+ activejob (7.0.6)
57
+ activesupport (= 7.0.6)
58
58
  globalid (>= 0.3.6)
59
- activemodel (7.0.4.2)
60
- activesupport (= 7.0.4.2)
61
- activerecord (7.0.4.2)
62
- activemodel (= 7.0.4.2)
63
- activesupport (= 7.0.4.2)
64
- activestorage (7.0.4.2)
65
- actionpack (= 7.0.4.2)
66
- activejob (= 7.0.4.2)
67
- activerecord (= 7.0.4.2)
68
- activesupport (= 7.0.4.2)
59
+ activemodel (7.0.6)
60
+ activesupport (= 7.0.6)
61
+ activerecord (7.0.6)
62
+ activemodel (= 7.0.6)
63
+ activesupport (= 7.0.6)
64
+ activestorage (7.0.6)
65
+ actionpack (= 7.0.6)
66
+ activejob (= 7.0.6)
67
+ activerecord (= 7.0.6)
68
+ activesupport (= 7.0.6)
69
69
  marcel (~> 1.0)
70
70
  mini_mime (>= 1.1.0)
71
- activesupport (7.0.4.2)
71
+ activesupport (7.0.6)
72
72
  concurrent-ruby (~> 1.0, >= 1.0.2)
73
73
  i18n (>= 1.6, < 2)
74
74
  minitest (>= 5.1)
@@ -83,12 +83,12 @@ GEM
83
83
  erubi (1.12.0)
84
84
  globalid (1.1.0)
85
85
  activesupport (>= 5.0)
86
- i18n (1.12.0)
86
+ i18n (1.14.1)
87
87
  concurrent-ruby (~> 1.0)
88
88
  json (2.6.3)
89
- loofah (2.19.1)
89
+ loofah (2.21.3)
90
90
  crass (~> 1.0.2)
91
- nokogiri (>= 1.5.9)
91
+ nokogiri (>= 1.12.0)
92
92
  mail (2.8.1)
93
93
  mini_mime (>= 0.1.1)
94
94
  net-imap
@@ -97,8 +97,8 @@ GEM
97
97
  marcel (1.0.2)
98
98
  method_source (1.0.0)
99
99
  mini_mime (1.1.2)
100
- minitest (5.18.0)
101
- net-imap (0.3.4)
100
+ minitest (5.19.0)
101
+ net-imap (0.3.7)
102
102
  date
103
103
  net-protocol
104
104
  net-pop (0.1.2)
@@ -107,40 +107,40 @@ GEM
107
107
  timeout
108
108
  net-smtp (0.3.3)
109
109
  net-protocol
110
- nio4r (2.5.8)
111
- nokogiri (1.14.2-x86_64-darwin)
112
- racc (~> 1.4)
113
- nokogiri (1.14.2-x86_64-linux)
110
+ nio4r (2.5.9)
111
+ nokogiri (1.15.3-x86_64-darwin)
114
112
  racc (~> 1.4)
115
113
  parallel (1.22.1)
116
114
  parser (3.2.1.0)
117
115
  ast (~> 2.4.1)
118
- racc (1.6.2)
119
- rack (2.2.6.3)
120
- rack-test (2.0.2)
116
+ racc (1.7.1)
117
+ rack (2.2.7)
118
+ rack-test (2.1.0)
121
119
  rack (>= 1.3)
122
- rails (7.0.4.2)
123
- actioncable (= 7.0.4.2)
124
- actionmailbox (= 7.0.4.2)
125
- actionmailer (= 7.0.4.2)
126
- actionpack (= 7.0.4.2)
127
- actiontext (= 7.0.4.2)
128
- actionview (= 7.0.4.2)
129
- activejob (= 7.0.4.2)
130
- activemodel (= 7.0.4.2)
131
- activerecord (= 7.0.4.2)
132
- activestorage (= 7.0.4.2)
133
- activesupport (= 7.0.4.2)
120
+ rails (7.0.6)
121
+ actioncable (= 7.0.6)
122
+ actionmailbox (= 7.0.6)
123
+ actionmailer (= 7.0.6)
124
+ actionpack (= 7.0.6)
125
+ actiontext (= 7.0.6)
126
+ actionview (= 7.0.6)
127
+ activejob (= 7.0.6)
128
+ activemodel (= 7.0.6)
129
+ activerecord (= 7.0.6)
130
+ activestorage (= 7.0.6)
131
+ activesupport (= 7.0.6)
134
132
  bundler (>= 1.15.0)
135
- railties (= 7.0.4.2)
136
- rails-dom-testing (2.0.3)
137
- activesupport (>= 4.2.0)
133
+ railties (= 7.0.6)
134
+ rails-dom-testing (2.1.1)
135
+ activesupport (>= 5.0.0)
136
+ minitest
138
137
  nokogiri (>= 1.6)
139
- rails-html-sanitizer (1.5.0)
140
- loofah (~> 2.19, >= 2.19.1)
141
- railties (7.0.4.2)
142
- actionpack (= 7.0.4.2)
143
- activesupport (= 7.0.4.2)
138
+ rails-html-sanitizer (1.6.0)
139
+ loofah (~> 2.21)
140
+ nokogiri (~> 1.14)
141
+ railties (7.0.6)
142
+ actionpack (= 7.0.6)
143
+ activesupport (= 7.0.6)
144
144
  method_source
145
145
  rake (>= 12.2)
146
146
  thor (~> 1.0)
@@ -175,15 +175,15 @@ GEM
175
175
  rubocop-ast (1.27.0)
176
176
  parser (>= 3.2.1.0)
177
177
  ruby-progressbar (1.12.0)
178
- thor (1.2.1)
179
- timeout (0.3.2)
178
+ thor (1.2.2)
179
+ timeout (0.4.0)
180
180
  tzinfo (2.0.6)
181
181
  concurrent-ruby (~> 1.0)
182
182
  unicode-display_width (2.4.2)
183
- websocket-driver (0.7.5)
183
+ websocket-driver (0.7.6)
184
184
  websocket-extensions (>= 0.1.0)
185
185
  websocket-extensions (0.1.5)
186
- zeitwerk (2.6.7)
186
+ zeitwerk (2.6.9)
187
187
 
188
188
  PLATFORMS
189
189
  x86_64-darwin-20
data/README.md CHANGED
@@ -15,7 +15,6 @@ against it. See [usage](#usage) for details.
15
15
  rake psdb:migrate # Migrate the database for current environment
16
16
  rake psdb:rollback # Rollback primary database for current environment
17
17
  rake psdb:schema:load # Load the current schema into the database
18
- rake psdb:setup_pscale # Setup a proxy to connect to PlanetScale
19
18
  ```
20
19
 
21
20
  ## Installation
@@ -34,22 +33,6 @@ And then execute in your terminal:
34
33
  bundle install
35
34
  ```
36
35
 
37
- ### Update database.yml
38
-
39
- In your application's `database.yml`, you'll need to make two changes. This will allow the app to run migrations via a connection setup by the pscale CLI.
40
-
41
- 1. Swap to port 3305 if `ENV['ENABLE_PSDB']` is true.
42
- 2. Use production database name if `ENV['ENABLE_PSDB']` is true.
43
-
44
- Here is an example
45
-
46
- ```
47
- development:
48
- <<: *default
49
- database: <%= ENV['ENABLE_PSDB'] ? 'your_production_db_name' : 'your_development_db_name' %>
50
- port: <%= ENV['ENABLE_PSDB'] ? 3305 : ENV.fetch("DB_PORT", 3306) %>
51
- ```
52
-
53
36
  ## Usage
54
37
 
55
38
  First, make sure you have the [`pscale` CLI installed](https://github.com/planetscale/cli#installation). You'll use `pscale` to create a new branch.
@@ -58,12 +41,13 @@ First, make sure you have the [`pscale` CLI installed](https://github.com/planet
58
41
  that this is the branch you want to migrate.
59
42
 
60
43
  ```
61
- pscale branch switch my-new-branch-name --database my-db-name --create
44
+ pscale branch switch my-new-branch-name --database my-db-name --create --wait
62
45
  ```
63
46
 
64
47
  **Tip:** In your database settings. Enable "Automatically copy migration data." Select "Rails" as the migration framework. This will auto copy your `schema_migrations` table between branches.
65
48
 
66
- 2. Once your branch is ready, you can then use the `psdb` rake task to connect to your branch and run `db:migrate`.
49
+ 2. Once your branch is ready, you can then use the `psdb` rake task to connect to your branch and run `db:migrate`. The command will run against
50
+ the branch specified in your `.pscale.yml` file.
67
51
 
68
52
  ```
69
53
  bundle exec rails psdb:migrate
@@ -75,8 +59,6 @@ If you run multiple databases in your Rails app, you can specify the DB name.
75
59
  bundle exec rails psdb:migrate:primary
76
60
  ```
77
61
 
78
- This will connect to the branch that you created, and run migrations against the branch.
79
-
80
62
  If you make a mistake, you can use `bundle exec rails psdb:rollback` to rollback the changes on your PlanetScale branch.
81
63
 
82
64
  3. Next, you can either open the Deploy Request via the UI. Or the CLI.
data/actions-example.md CHANGED
@@ -18,7 +18,7 @@ Secrets needed to be set:
18
18
  - `PLANETSCALE_SERVICE_TOKEN_ID`
19
19
  - `PLANETSCALE_SERVICE_TOKEN`
20
20
 
21
- The PlanetScale service token must have permission on the database to create/read development branches, as well as create deploy requests.
21
+ The PlanetScale service token must have the `connect_branch`, `create_branch`, `read_branch`, `create_deploy_request`, and `read_deploy_request` permissions on the database.
22
22
 
23
23
  ```yaml
24
24
  name: Run database migrations
@@ -7,18 +7,13 @@ require "colorize"
7
7
 
8
8
  databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
9
9
 
10
- def shared_deps(name = nil)
11
- return [:environment, :check_ci, "setup_pscale:#{name}".to_sym] if name
12
- return %i[environment check_ci] if name.nil?
13
- end
14
-
15
10
  def puts_deploy_request_instructions
16
11
  ps_config = YAML.load_file(".pscale.yml")
17
12
  database = ps_config["database"]
18
13
  branch = ps_config["branch"]
19
14
  org = ps_config["org"]
20
15
 
21
- puts "Create a deploy request for '#{branch}' by running:\n"
16
+ puts "Create a deploy request for '#{branch.colorize(:blue)}' by running:\n"
22
17
  puts " pscale deploy-request create #{database} #{branch} --org #{org}\n\n"
23
18
  end
24
19
 
@@ -26,6 +21,28 @@ def kill_pscale_process
26
21
  Process.kill("TERM", ENV["PSCALE_PID"].to_i) if ENV["PSCALE_PID"]
27
22
  end
28
23
 
24
+ def delete_password
25
+ password_id = ENV["PSCALE_PASSWORD_ID"]
26
+ return unless password_id
27
+
28
+ ps_config = YAML.load_file(".pscale.yml")
29
+ database = ps_config["database"]
30
+ branch = ps_config["branch"]
31
+
32
+ command = "pscale password delete #{database} #{branch} #{password_id} #{ENV["SERVICE_TOKEN_CONFIG"]} --force"
33
+ output = `#{command}`
34
+
35
+ return if $CHILD_STATUS.success?
36
+
37
+ puts "Failed to cleanup credentials used for PlanetScale connection. Password ID: #{password_id}".colorize(:red)
38
+ puts "Command: #{command}"
39
+ puts "Output: #{output}"
40
+ end
41
+
42
+ def db_branch_colorized(database, branch)
43
+ "#{database.colorize(:blue)}/#{branch.colorize(:blue)}"
44
+ end
45
+
29
46
  namespace :psdb do
30
47
  task check_ci: :environment do
31
48
  use_service_token = ENV["PSCALE_SERVICE_TOKEN"] && ENV["PSCALE_SERVICE_TOKEN_ID"]
@@ -38,91 +55,53 @@ namespace :psdb do
38
55
  end
39
56
  end
40
57
 
41
- desc "Setup a proxy to connect to PlanetScale"
42
- task "setup_pscale" => shared_deps do
43
- ENV["ENABLE_PSDB"] = "1"
44
- ENV["DISABLE_PS_GEM"] = "1"
58
+ def create_connection_string
45
59
  ps_config = YAML.load_file(".pscale.yml")
46
60
  database = ps_config["database"]
47
61
  branch = ps_config["branch"]
48
62
 
49
63
  raise "You must have `pscale` installed on your computer".colorize(:red) unless command?("pscale")
50
64
  if branch.blank? || database.blank?
51
- raise "Your branch is not properly setup, please switch to a branch by using the CLI.".colorize(:red)
65
+ raise "Could not determine which PlanetScale branch to use from .pscale.yml. Please switch to a branch by using: `pscale switch database-name branch-name`".colorize(:red)
52
66
  end
53
67
 
54
- r, = PTY.open
68
+ short_hash = SecureRandom.hex(2)[0, 4]
69
+ password_name = "planetscale-rails-#{short_hash}"
70
+ command = "pscale password create #{database} #{branch} #{password_name} -f json #{ENV["SERVICE_TOKEN_CONFIG"]}"
55
71
 
56
- puts "Connecting to PlanetScale..."
72
+ output = `#{command}`
57
73
 
58
- # Spawns the process in the background
59
- pid = Process.spawn("pscale connect #{database} #{branch} --port 3305 #{ENV["SERVICE_TOKEN_CONFIG"]}", out: r)
60
- ENV["PSCALE_PID"] = pid.to_s
74
+ if $CHILD_STATUS.success?
75
+ response = JSON.parse(output)
76
+ puts "Successfully created credentials for PlanetScale #{db_branch_colorized(database, branch)}"
77
+ host = response["access_host_url"]
78
+ username = response["username"]
79
+ password = response["plain_text"]
80
+ ENV["PSCALE_PASSWORD_ID"] = response["id"]
61
81
 
62
- out = ""
63
- start_time = Time.current
64
- time_elapsed = Time.current - start_time
65
- sleep(1)
66
- while out.blank? && time_elapsed < 10.seconds
67
- PTY.check(pid, true)
68
- out = r.gets
69
- time_elapsed = Time.current - start_time
70
- end
82
+ adapter = "mysql2"
71
83
 
72
- raise "Timed out waiting for PlanetScale connection to be established".colorize(:red) if time_elapsed > 10.seconds
73
- ensure
74
- r&.close
75
- end
76
-
77
- namespace :setup_pscale do
78
- ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
79
- desc "Setup a proxy to connect to #{name} in PlanetScale"
80
- task name => shared_deps do
81
- ENV["ENABLE_PSDB"] = "1"
82
- ENV["DISABLE_PS_GEM"] = "1"
83
- ps_config = YAML.load_file(".pscale.yml")
84
- database = ps_config["database"]
85
- branch = ps_config["branch"]
86
-
87
- raise "You must have `pscale` installed on your computer" unless command?("pscale")
88
- if branch.blank? || database.blank?
89
- raise "Your branch is not properly setup, please switch to a branch by using the CLI."
90
- end
91
-
92
- config = Rails.configuration.database_configuration[Rails.env][name]
93
-
94
- raise "Database #{name} is not configured for the current environment" unless config
95
-
96
- r, = PTY.open
97
-
98
- puts "Connecting to PlanetScale..."
99
-
100
- # Spawns the process in the background
101
- pid = Process.spawn("pscale connect #{database} #{branch} --port 3305 #{ENV["SERVICE_TOKEN_CONFIG"]}", out: r)
102
- ENV["PSCALE_PID"] = pid.to_s
103
-
104
- out = ""
105
- start_time = Time.current
106
- time_elapsed = Time.current - start_time
107
- sleep(1)
108
- while out.blank? && time_elapsed < 10.seconds
109
- PTY.check(pid, true)
110
- out = r.gets
111
- time_elapsed = Time.current - start_time
112
- end
113
-
114
- raise "Timed out waiting for PlanetScale connection to be established" if time_elapsed > 10.seconds
115
-
116
- # Comment out for now, this messes up when running migrations.
117
- # Kernel.system("bundle exec rails db:environment:set RAILS_ENV=development")
118
- ensure
119
- r&.close
84
+ if defined?(Trilogy)
85
+ adapter = "trilogy"
120
86
  end
87
+
88
+ "#{adapter}://#{username}:#{password}@#{host}:3306/#{database}?ssl_mode=VERIFY_IDENTITY"
89
+ else
90
+ puts "Failed to create credentials for PlanetScale #{db_branch_colorized(database, branch)}"
91
+ puts "Command: #{command}"
92
+ puts "Output: #{output}"
93
+ puts "Please verify that you have the correct permissions to create a password for this branch."
94
+ exit 1
121
95
  end
122
96
  end
123
97
 
124
- desc "Migrate the database for current environment"
125
- task migrate: %i[environment check_ci setup_pscale] do
98
+ desc "Create credentials for PlanetScale and sets them to ENV['PSCALE_DATABASE_URL']"
99
+ task "create_creds" => %i[environment check_ci] do
100
+ ENV["PSCALE_DATABASE_URL"] = create_connection_string
101
+ end
102
+
103
+ desc "Connects to the current PlanetScale branch and runs rails db:migrate"
104
+ task migrate: %i[environment check_ci create_creds] do
126
105
  db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
127
106
 
128
107
  unless db_configs.size == 1
@@ -130,41 +109,45 @@ namespace :psdb do
130
109
  end
131
110
 
132
111
  puts "Running migrations..."
133
- Kernel.system("bundle exec rails db:migrate")
134
- puts "Finished running migrations\n".colorize(:green)
135
- puts_deploy_request_instructions
112
+
113
+ command = "DATABASE_URL=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:migrate"
114
+ IO.popen(command) do |io|
115
+ io.each_line do |line|
116
+ puts line
117
+ end
118
+ end
119
+
120
+ if $CHILD_STATUS.success?
121
+ puts_deploy_request_instructions
122
+ else
123
+ puts "Failed to run migrations".colorize(:red)
124
+ end
136
125
  ensure
137
- kill_pscale_process
126
+ delete_password
138
127
  end
139
128
 
140
129
  namespace :migrate do
141
130
  ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
142
- desc "Migrate #{name} database for current environment"
143
- task name => shared_deps(name) do
131
+ desc "Connects to the current PlanetScale branch and runs rails db:migrate:#{name}"
132
+ task name => %i[environment check_ci create_creds] do
144
133
  puts "Running migrations..."
145
- # We run it using the Kernel.system here because this properly handles
146
- # when exceptions occur whereas Rake::Task.invoke does not.
147
- Kernel.system("bundle exec rake db:migrate:#{name}")
148
134
 
149
- puts "Finished running migrations\n".colorize(:green)
150
- puts_deploy_request_instructions
151
- ensure
152
- kill_pscale_process
153
- end
154
- end
155
- end
135
+ name_env_key = "#{name.upcase}_DATABASE_URL"
136
+ command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:migrate:#{name}"
156
137
 
157
- namespace :truncate_all do
158
- ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
159
- desc "Truncate all tables in #{name} database for current environment"
160
- task name => shared_deps(name) do
161
- puts "Truncating database..."
162
- # We run it using the Kernel.system here because this properly handles
163
- # when exceptions occur whereas Rake::Task.invoke does not.
164
- Kernel.system("bundle exec rake db:truncate_all")
165
- puts "Finished truncating database."
138
+ IO.popen(command) do |io|
139
+ io.each_line do |line|
140
+ puts line
141
+ end
142
+ end
143
+
144
+ if $CHILD_STATUS.success?
145
+ puts_deploy_request_instructions
146
+ else
147
+ puts "Failed to run migrations".colorize(:red)
148
+ end
166
149
  ensure
167
- kill_pscale_process
150
+ delete_password
168
151
  end
169
152
  end
170
153
  end
@@ -172,31 +155,79 @@ namespace :psdb do
172
155
  namespace :schema do
173
156
  namespace :load do
174
157
  ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
175
- desc "Load the current schema into the #{name} database"
176
- task name => shared_deps(name) do
158
+ desc "Connects to the current PlanetScale branch and runs rails db:schema:load:#{name}"
159
+ task name => %i[environment check_ci create_creds] do
177
160
  puts "Loading schema..."
178
- # We run it using the Kernel.system here because this properly handles
179
- # when exceptions occur whereas Rake::Task.invoke does not.
180
- Kernel.system("bundle exec rake db:schema:load:#{name}")
181
- puts "Finished loading schema."
161
+
162
+ name_env_key = "#{name.upcase}_DATABASE_URL"
163
+ command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rake db:schema:load:#{name}"
164
+
165
+ IO.popen(command) do |io|
166
+ io.each_line do |line|
167
+ puts line
168
+ end
169
+ end
170
+
171
+ unless $CHILD_STATUS.success?
172
+ puts "Failed to load schema".colorize(:red)
173
+ end
182
174
  ensure
183
- kill_pscale_process
175
+ delete_password
184
176
  end
185
177
  end
186
178
  end
187
179
  end
188
180
 
181
+ desc "Connects to the current PlanetScale branch and runs rails db:rollback"
182
+ task rollback: %i[environment check_ci create_creds] do
183
+ db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
184
+
185
+ unless db_configs.size == 1
186
+ raise "Found multiple database configurations, please specify which database you want to rollback using `psdb:rollback:<database_name>`".colorize(:red)
187
+ end
188
+
189
+ command = "DATABASE_URL=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:rollback"
190
+
191
+ IO.popen(command) do |io|
192
+ io.each_line do |line|
193
+ puts line
194
+ end
195
+ end
196
+
197
+ unless $CHILD_STATUS.success?
198
+ puts "Failed to rollback migrations".colorize(:red)
199
+ end
200
+ ensure
201
+ delete_password
202
+ end
203
+
189
204
  namespace :rollback do
190
205
  ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
191
- desc "Rollback #{name} database for current environment"
192
- task name => shared_deps(name) do
206
+ desc "Connects to the current PlanetScale branch and runs rails db:rollback:#{name}"
207
+ task name => %i[environment check_ci create_creds] do
208
+ required_version = Gem::Version.new("6.1.0.0")
209
+ rails_version = Gem::Version.new(Rails.version)
210
+
211
+ if rails_version < required_version
212
+ rails "This version of Rails does not support rollback commands for multi-database Rails apps. Please upgrade to at least Rails 6.1"
213
+ end
214
+
193
215
  puts "Rolling back migrations..."
194
- # We run it using the Kernel.system here because this properly handles
195
- # when exceptions occur whereas Rake::Task.invoke does not.
196
- Kernel.system("bundle exec rake db:rollback:#{name}")
197
- puts "Finished rolling back migrations.".colorize(:green)
216
+
217
+ name_env_key = "#{name.upcase}_DATABASE_URL"
218
+ command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rake db:rollback:#{name}"
219
+
220
+ IO.popen(command) do |io|
221
+ io.each_line do |line|
222
+ puts line
223
+ end
224
+ end
225
+
226
+ unless $CHILD_STATUS.success?
227
+ puts "Failed to rollback migrations".colorize(:red)
228
+ end
198
229
  ensure
199
- kill_pscale_process
230
+ delete_password
200
231
  end
201
232
  end
202
233
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlanetscaleRails
4
- VERSION = "0.1.4"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -29,5 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  spec.add_dependency "colorize", "~> 0.8.1"
32
- spec.add_dependency "rails", "~> 7.0"
32
+ spec.add_dependency "rails", ">= 6.0", "< 8"
33
33
  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.1.4
4
+ version: 0.2.0
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: 2023-07-28 00:00:00.000000000 Z
12
+ date: 2023-11-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize
@@ -29,16 +29,22 @@ dependencies:
29
29
  name: rails
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '6.0'
35
+ - - "<"
33
36
  - !ruby/object:Gem::Version
34
- version: '7.0'
37
+ version: '8'
35
38
  type: :runtime
36
39
  prerelease: false
37
40
  version_requirements: !ruby/object:Gem::Requirement
38
41
  requirements:
39
- - - "~>"
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '6.0'
45
+ - - "<"
40
46
  - !ruby/object:Gem::Version
41
- version: '7.0'
47
+ version: '8'
42
48
  description: A collection of rake tasks to make managing schema migrations with PlanetScale
43
49
  easy
44
50
  email: