Skip to content

Commit 0f83638

Browse files
Add PostgreSQL ActiveRecord Sample (GoogleCloudPlatform#559)
* copy mysql sample app as template * updated database.yml and app.yaml * update readme * updated region tag * Update cloud-sql/postgres/activerecord/README.md Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * addressed PR feedback and added GAE standard deployment instructions * update license headers Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
1 parent c158dd8 commit 0f83638

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1670
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore all logfiles and tempfiles.
11+
/log/*
12+
/tmp/*
13+
!/log/.keep
14+
!/tmp/.keep
15+
16+
# Ignore uploaded files in development
17+
/storage/*
18+
!/storage/.keep
19+
20+
/node_modules
21+
/yarn-error.log
22+
23+
/public/assets
24+
.byebug_history
25+
26+
**/*.sqlite3
27+
28+
# Ignore master key for decrypting credentials and more.
29+
/config/master.key
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
source "https://rubygems.org"
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5+
gem "rails", "~> 5.2.3"
6+
# Use mysql as the database for Active Record
7+
gem "pg", "~> 0.21.0"
8+
# Use Puma as the app server
9+
gem "puma", "~> 3.11"
10+
# Use SCSS for stylesheets
11+
gem "sass-rails", "~> 5.0"
12+
# Use Uglifier as compressor for JavaScript assets
13+
gem "uglifier", ">= 1.3.0"
14+
# See https://github.com/rails/execjs#readme for more supported runtimes
15+
# gem 'mini_racer', platforms: :ruby
16+
17+
# Use CoffeeScript for .coffee assets and views
18+
gem "coffee-rails", "~> 4.2"
19+
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
20+
gem "turbolinks", "~> 5"
21+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
22+
gem "jbuilder", "~> 2.5"
23+
# Use Redis adapter to run Action Cable in production
24+
# gem 'redis', '~> 4.0'
25+
# Use ActiveModel has_secure_password
26+
# gem 'bcrypt', '~> 3.1.7'
27+
28+
# Use ActiveStorage variant
29+
# gem 'mini_magick', '~> 4.8'
30+
31+
# Use Capistrano for deployment
32+
# gem 'capistrano-rails', group: :development
33+
34+
# Reduces boot times through caching; required in config/boot.rb
35+
gem "bootsnap", ">= 1.1.0", require: false
36+
37+
group :development, :test do
38+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
39+
gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
40+
end
41+
42+
group :development do
43+
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
44+
gem "listen", ">= 3.0.5", "< 3.2"
45+
gem "web-console", ">= 3.3.0"
46+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
47+
gem "spring"
48+
gem "spring-watcher-listen", "~> 2.0.0"
49+
end
50+
51+
group :test do
52+
# Adds support for Capybara system testing and selenium driver
53+
gem "capybara", ">= 2.15"
54+
gem "selenium-webdriver"
55+
# Easy installation and use of chromedriver to run system tests with Chrome
56+
gem "chromedriver-helper"
57+
end
58+
59+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
60+
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
61+
62+
gem "sqlite3", "~> 1.4"
63+
64+
gem "rspec-rails", "~> 3.8"
65+
gem "rspec_junit_formatter"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Connecting to Cloud SQL - MySQL
2+
3+
## Before you begin
4+
5+
1. If you haven't already, set up a Ruby Development Environment by following the [ruby setup guide](https://cloud.google.com/ruby/docs/setup) and
6+
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
7+
8+
1. Create a 2nd Gen Cloud SQL Instance by following these
9+
[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string,
10+
database user, and database password that you create.
11+
12+
1. Create a database for your application by following these
13+
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database
14+
name.
15+
16+
1. Create a service account with the 'Cloud SQL Client' permissions by following these
17+
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
18+
Download a JSON key to use to authenticate your connection.
19+
20+
21+
22+
## Running locally
23+
24+
Use the information noted in the previous steps:
25+
```bash
26+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
27+
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
28+
export DB_USER='my-db-user'
29+
export DB_PASS='my-db-pass'
30+
export DB_NAME='my_db'
31+
```
32+
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
33+
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe.
34+
35+
Then, download and install the `cloud_sql_proxy` by
36+
following the instructions
37+
[here](https://cloud.google.com/sql/docs/mysql/authorize-proxy#installing_the). Once the
38+
proxy has been downloaded, use the following commands to create the `/cloudsql`
39+
directory and give the user running the proxy the appropriate permissions:
40+
```bash
41+
sudo mkdir /cloudsql
42+
sudo chown -R $USER /cloudsql
43+
```
44+
45+
Once the `/cloudsql` directory is ready, use the following command to start the proxy in the
46+
background:
47+
```bash
48+
./cloud_sql_proxy -dir=/cloudsql --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS
49+
```
50+
Note: Make sure to run the command under a user with write access in the
51+
`/cloudsql` directory. This proxy will use this folder to create a unix socket
52+
the application will use to connect to Cloud SQL.
53+
54+
Next, setup install the requirements:
55+
```bash
56+
bundle install
57+
```
58+
59+
Then, setup and seed the database:
60+
```bash
61+
bundle exec rails db:setup
62+
bundle exec rails db:seed
63+
```
64+
65+
Finally, start the application:
66+
```bash
67+
bundle exec rails s
68+
```
69+
70+
Navigate towards `http://localhost:3000` to verify your application is running correctly.
71+
72+
## Deploy to Google App Engine Standard
73+
74+
To allow your app to connect to your Cloud SQL instance when the app is deployed, add the user, password, database, and instance connection name variables from Cloud SQL to the related environment variables in the `app.standard.yaml` file. The deployed application will connect via unix sockets.
75+
76+
```
77+
env_variables:
78+
DB_USER: MY_DB_USER
79+
DB_PASS: MY_DB_PASSWORD
80+
DB_NAME: MY_DATABASE
81+
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
82+
CLOUD_SQL_CONNECTION_NAME: <MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>
83+
```
84+
85+
SECRET_KEY_BASE can be found by running:
86+
```bash
87+
bundle exec rails secret
88+
```
89+
90+
To deploy to App Engine Standard, run the following command:
91+
```bash
92+
gcloud app deploy app.standard.yaml
93+
```
94+
95+
96+
## Google App Engine Flexible
97+
98+
To run on GAE-Flex, create an App Engine project by following the setup for these
99+
[instructions](https://cloud.google.com/appengine/docs/flexible/ruby/quickstart).
100+
101+
First, update `app.flexible.yaml` with the correct values to pass the environment
102+
variables into the runtime.
103+
104+
SECRET_KEY_BASE can be found by running:
105+
```bash
106+
bundle exec rails secret
107+
```
108+
109+
To deploy to App Engine Flexible, run the following command:
110+
```bash
111+
gcloud app deploy app.flexible.yaml
112+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require_relative "config/application"
5+
6+
Rails.application.load_tasks
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2020 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
entrypoint: bundle exec rackup --port $PORT
16+
env: flex
17+
runtime: ruby
18+
19+
env_variables:
20+
SECRET_KEY_BASE: [SECRET_KEY]
21+
RAILS_ENV: production
22+
INSTANCE_CONNECTION_NAME: [<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>]
23+
DB_USER: [MY_DB_USER]
24+
DB_PASS: [MY_DB_PASS]
25+
DB_NAME: [MY_DB]
26+
27+
beta_settings:
28+
cloud_sql_instances: [YOUR_INSTANCE_CONNECTION_NAME]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
entrypoint: bundle exec rackup --port $PORT
16+
runtime: ruby25
17+
18+
env_variables:
19+
SECRET_KEY_BASE: [SECRET_KEY]
20+
RAILS_ENV: production
21+
INSTANCE_CONNECTION_NAME: [<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>]
22+
DB_USER: [MY_DB_USER]
23+
DB_PASS: [MY_DB_PASS]
24+
DB_NAME: [MY_DB]
25+
26+
beta_settings:
27+
cloud_sql_instances: [YOUR_INSTANCE_CONNECTION_NAME]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//= link_tree ../images
2+
//= link_directory ../javascripts .js
3+
//= link_directory ../stylesheets .css

cloud-sql/postgres/activerecord/app/assets/images/.keep

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
5+
// vendor/assets/javascripts directory can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
9+
//
10+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11+
// about supported directives.
12+
//
13+
//= require rails-ujs
14+
//= require activestorage
15+
//= require turbolinks
16+
//= require_tree .
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Action Cable provides the framework to deal with WebSockets in Rails.
2+
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
3+
//
4+
//= require action_cable
5+
//= require_self
6+
//= require_tree ./channels
7+
8+
(function() {
9+
this.App || (this.App = {});
10+
11+
App.cable = ActionCable.createConsumer();
12+
13+
}).call(this);

0 commit comments

Comments
 (0)