Skip to content

Commit 278664b

Browse files
author
remi Taylor
authored
Merge pull request #94 from GoogleCloudPlatform/cloud-sql-flex-updates
Update AppEngine CloudSQL second generation sample
2 parents 8c33a06 + 75b0a1b commit 278664b

File tree

26 files changed

+120
-49
lines changed

26 files changed

+120
-49
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ env:
3939
- TEST_DIR=translate
4040
- TEST_DIR=language
4141
- TEST_DIR=appengine/endpoints
42+
- TEST_DIR=appengine/cloudsql
4243
- TEST_DIR=vision
4344

4445
before_install:

appengine/analytics/spec/google_analytics_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
Capybara.current_driver = :poltergeist
2121

22-
RSpec.describe "Google Analytics on Google App Engine", type: :feature do
22+
describe "Google Analytics on Google App Engine", type: :feature do
2323
before :all do
2424
@url = E2E.url
2525
end

appengine/cloudsql/.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--tty --color --format documentation --order random

appengine/cloudsql/Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START dependencies]
1516
source "https://rubygems.org"
1617

17-
# [START dependencies]
1818
gem "mysql2"
19+
gem "sequel"
1920
# [END dependencies]
2021

2122
gem "sinatra"
22-
gem "sequel"
2323

2424
group :test do
2525
gem "rspec"
2626
gem "capybara"
2727
gem "poltergeist"
28+
gem "rack-test"
29+
gem "sqlite3"
2830
end

appengine/cloudsql/Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GEM
4747
rack (~> 1.5)
4848
rack-protection (~> 1.4)
4949
tilt (>= 1.3, < 3)
50+
sqlite3 (1.3.12)
5051
tilt (2.0.5)
5152
websocket-driver (0.6.4)
5253
websocket-extensions (>= 0.1.0)
@@ -61,9 +62,11 @@ DEPENDENCIES
6162
capybara
6263
mysql2
6364
poltergeist
65+
rack-test
6466
rspec
6567
sequel
6668
sinatra
69+
sqlite3
6770

6871
BUNDLED WITH
69-
1.12.5
72+
1.13.3

appengine/cloudsql/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SDK use the following command:
1515
--activation-policy=ALWAYS \
1616
--tier=db-n1-standard-1
1717

18-
where `[YOUR_INSTANCE_NAME]` is a name of your choice.
18+
where `[YOUR_INSTANCE_NAME]` is a name of your choice, composed of lowercase letters, numbers, and hyphens; must start with a letter.
1919

2020
1. Set the root password on your Cloud SQL instance:
2121

@@ -57,7 +57,11 @@ create a [new user][user] and [database][database] for your application:
5757
`MYSQL_DATABASE` environment variables. This allows the app to connect to your
5858
Cloud SQL instance through the proxy.
5959

60-
1. Update the values in in `app.yaml` with your instance configuration.
60+
1. Update the values in `app.yaml` with your instance configuration.
61+
62+
1. Install dependencies
63+
64+
bundle install
6165

6266
1. Finally, run `create_tables.rb` to ensure that the database is properly
6367
configured and to create the tables needed for the sample.

appengine/cloudsql/app.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
require "sinatra"
1818
require "sequel"
1919

20-
# [START connect]
2120
DB = Sequel.mysql2 user: ENV["MYSQL_USER"],
2221
password: ENV["MYSQL_PASSWORD"],
2322
database: ENV["MYSQL_DATABASE"],
2423
socket: ENV["MYSQL_SOCKET_PATH"]
25-
# [END connect]
2624

2725
get "/" do
26+
# Store a hash of the visitor's ip address
27+
visitor_ip = Digest::SHA256.hexdigest request.ip
28+
2829
# Save visit in database
29-
DB[:visits].insert(
30-
user_ip: Digest::SHA256.hexdigest(request.ip),
31-
timestamp: Time.now
32-
)
30+
DB[:visits].insert user_ip: visitor_ip, timestamp: Time.now
31+
32+
# Retrieve the latest 10 visit records from the database
33+
visits = DB[:visits].limit(10).order Sequel.desc(:timestamp)
3334

3435
response.write "Last 10 visits:\n"
3536

36-
DB[:visits].order(Sequel.desc(:timestamp)).limit(10).each do |visit|
37+
visits.each do |visit|
3738
response.write "Time: #{visit[:timestamp]} Addr: #{visit[:user_ip]}\n"
3839
end
3940

appengine/cloudsql/create_tables.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START all]
1615
require "sequel"
1716

1817
DB = Sequel.mysql2 user: ENV["MYSQL_USER"],
@@ -25,4 +24,3 @@
2524
String :user_ip
2625
Time :timestamp
2726
end
28-
# [END all]

appengine/cloudsql/spec/cloudsql_spec.rb

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,43 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require File.expand_path("../../../../spec/e2e", __FILE__)
15+
require_relative "../app.rb"
1616
require "rspec"
17-
require "capybara/rspec"
18-
require "capybara/poltergeist"
17+
require "rack/test"
1918

20-
Capybara.current_driver = :poltergeist
19+
describe "Cloud SQL sample", type: :feature do
20+
include Rack::Test::Methods
2121

22-
RSpec.describe "Cloud SQL on Google App Engine", type: :feature do
23-
before :all do
24-
app_yaml = File.expand_path("../../app.yaml", __FILE__)
22+
def app
23+
Sinatra::Application
24+
end
25+
26+
before do
27+
@database = Sequel.sqlite database: ":memory:"
28+
29+
expect(Sequel).to receive(:mysql2).and_return @database
30+
end
2531

26-
configuration = File.read(app_yaml)
27-
configuration.sub! "[YOUR_USER]", ENV["MYSQL_USER"]
28-
configuration.sub! "[YOUR_PASSWORD]", ENV["MYSQL_PASSWORD"]
29-
configuration.sub! "[YOUR_DATABASE]", ENV["MYSQL_DATABASE"]
30-
configuration.sub! "[YOUR_SOCKET_PATH]", ENV["MYSQL_SOCKET_PATH"]
32+
it "can create database schema by running create_tables.rb" do
33+
expect(@database.tables).not_to include :visits
3134

32-
File.write(app_yaml, configuration)
35+
load File.expand_path("../create_tables.rb", __dir__)
3336

34-
@url = E2E.url
37+
expect(@database.tables).to include :visits
3538
end
3639

37-
it "displays recent visits" do
38-
2.times { visit @url }
40+
it "displays hashes of the IP addresses of the top 10 most recent visits" do
41+
load File.expand_path("../create_tables.rb", __dir__)
42+
expect(@database[:visits].count).to eq 0
43+
44+
localhost_user_ip = Digest::SHA256.hexdigest "127.0.0.1"
45+
46+
15.times { get "/" }
3947

40-
expect(page).to have_content "Last 10 visits:"
41-
expect(page).to have_content "Time:"
42-
expect(page).to have_content "Addr:"
48+
expect(@database[:visits].count).to eq 15
49+
expect(@database[:visits].first[:user_ip]).to eq localhost_user_ip
50+
expect(last_response.body).to include "Last 10 visits"
51+
expect(last_response.body).to include "Addr: #{localhost_user_ip}"
52+
expect(last_response.body.scan("Addr:").count).to eq 10
4353
end
4454
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2016 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+
require File.expand_path("../../../../spec/e2e", __FILE__)
16+
require "rspec"
17+
require "capybara/rspec"
18+
require "capybara/poltergeist"
19+
20+
Capybara.current_driver = :poltergeist
21+
22+
describe "Cloud SQL on Google App Engine", type: :feature do
23+
before :all do
24+
skip "End-to-end tests skipped" unless E2E.run?
25+
26+
app_yaml = File.expand_path("../../app.yaml", __FILE__)
27+
28+
configuration = File.read(app_yaml)
29+
configuration.sub! "[YOUR_USER]", ENV["MYSQL_USER"]
30+
configuration.sub! "[YOUR_PASSWORD]", ENV["MYSQL_PASSWORD"]
31+
configuration.sub! "[YOUR_DATABASE]", ENV["MYSQL_DATABASE"]
32+
configuration.sub! "[YOUR_SOCKET_PATH]", ENV["MYSQL_SOCKET_PATH"]
33+
34+
File.write(app_yaml, configuration)
35+
36+
@url = E2E.url
37+
end
38+
39+
it "displays recent visits" do
40+
2.times { visit @url }
41+
42+
expect(page).to have_content "Last 10 visits:"
43+
expect(page).to have_content "Time:"
44+
expect(page).to have_content "Addr:"
45+
end
46+
end

0 commit comments

Comments
 (0)