Skip to content
Browse files

mysql support for ccng

This involved patching the case_insensitive extention we did, plus
fixing up a few expectations in the model specs around what the
underlying db should return for constraint violations.

Change-Id: Ic8ca839f7e73853131a3b36998329c4be646e2dd
  • Loading branch information...
1 parent 4fc75ef commit 9e2b225add4be607b4f099d2744fdf04915a51fb David Sabeti & Pat Bozeman committed with David Sabeti & Pat Bozeman Feb 1, 2013
View
1 Gemfile
@@ -4,6 +4,7 @@ gem "rake"
gem "bcrypt-ruby"
gem 'eventmachine', "~> 1.0.0"
gem 'fog'
+gem "mysql2"
gem "redis"
gem "rfc822"
gem "sequel"
View
2 Gemfile.lock
@@ -101,6 +101,7 @@ GEM
multi_json (1.3.6)
multi_xml (0.5.1)
multipart-post (1.1.5)
+ mysql2 (0.3.11)
nats (0.4.24)
daemons (>= 1.1.5)
eventmachine (>= 0.12.10)
@@ -184,6 +185,7 @@ DEPENDENCIES
machinist (~> 1.0.6)
membrane (~> 0.0.2)
mock_redis
+ mysql2
pg
rake
redis
View
6 lib/cloud_controller/db.rb
@@ -20,7 +20,7 @@ class DB
#
# @return [Sequel::Database]
def self.connect(logger, opts)
- connection_options = {}
+ connection_options = { :sql_mode => [:strict_trans_tables, :strict_all_tables, :no_zero_in_date] }
[:max_connections, :pool_timeout].each do |key|
connection_options[key] = opts[key] if opts[key]
end
@@ -34,6 +34,10 @@ def self.connect(logger, opts)
db.logger = logger
db.sql_log_level = opts[:log_level] || :debug2
+ if db.database_type == :mysql
+ Sequel::MySQL.default_collate = "latin1_general_cs"
+ end
+
validate_sqlite_version(db) if using_sqlite
VCAP::SequelVarz.start(db)
db
View
15 lib/vcap/sequel_case_insensitive_string_monkeypatch.rb
@@ -53,6 +53,21 @@ def case_insensitive_string_column_opts
end
module Sequel
+ module Mysql2
+ class Database
+ # Mysql is case insensitive by default
+ def case_insensitive_string_column_type
+ "VARCHAR(255)"
+ end
+
+ def case_insensitive_string_column_opts
+ { :collate => "latin1_general_ci" }
+ end
+ end
+ end
+end
+
+module Sequel
module Schema
class Generator
def String(name, opts = {})
View
14 spec/models/helpers/unique_attributes.rb
@@ -57,7 +57,7 @@ module VCAP::CloudController::ModelSpecHelper
desc = "[#{desc}]" if opts[:unique_attributes].length > 1
context "with duplicate #{desc}" do
let(:column_list) do
- attrs = opts[:unique_attributes].map do |v|
+ opts[:unique_attributes].map do |v|
if described_class.associations.include?(v.to_sym)
v = v.to_s.concat("_id")
end
@@ -70,7 +70,14 @@ module VCAP::CloudController::ModelSpecHelper
end
let(:db_exception_match) do
- "columns? #{column_list.join(", ")} .* not unique".sub("uaa_id", "guid")
+ case described_class.db.database_type
+ when :mysql
+ "Duplicate entry"
+ when :sqlite
+ "columns? #{column_list.join(", ")} .* not unique".sub("uaa_id", "guid")
+ else
+ ".*"
+ end
end
let(:dup_opts) do
@@ -80,9 +87,10 @@ module VCAP::CloudController::ModelSpecHelper
initial_template = described_class.make
orig_opts = creation_opts_from_obj(initial_template, new_opts)
+
initial_template.destroy
- orig_obj = described_class.make orig_opts
+ described_class.make orig_opts
orig_opts
end
View
2 spec/sequel_plugins/vcap_normalization_spec.rb
@@ -4,7 +4,7 @@
describe "Sequel::Plugins::VcapNormalization" do
before do
- db = Sequel.sqlite
+ reset_database
db.create_table :test do
primary_key :id
View
2 spec/sequel_plugins/vcap_relations_spec.rb
@@ -4,7 +4,7 @@
describe "Sequel::Plugins::VcapRelations" do
before do
- db = Sequel.sqlite
+ reset_database
db.create_table :owners do
primary_key :id
View
2 spec/sequel_plugins/vcap_serialization_spec.rb
@@ -4,7 +4,7 @@
describe "Sequel::Plugins::VcapSerialization" do
before do
- db = Sequel.sqlite
+ reset_database
db.create_table :test do
primary_key :id
View
34 spec/spec_helper.rb
@@ -23,7 +23,7 @@ def initialize
File.unlink(log_filename) if File.exists?(log_filename)
Steno.init(Steno::Config.new(:default_log_level => "debug",
:sinks => [Steno::Sink::IO.for_file(log_filename)]))
- VCAP::CloudController::DB.apply_migrations(db)
+ reset_database
end
def spec_dir
@@ -42,14 +42,28 @@ def log_filename
artifact_filename("spec.log")
end
- def reset_database
- db.execute("PRAGMA foreign_keys = OFF")
- db.tables.each do |table|
- db.drop_table(table)
+ def without_foreign_key_checks
+ case db.database_type
+ when :sqlite
+ db.execute("PRAGMA foreign_keys = OFF")
+ yield
+ db.execute("PRAGMA foreign_keys = ON")
+ when :mysql
+ db.execute("SET foreign_key_checks = 0")
+ yield
+ db.execute("SET foreign_key_checks = 1")
+ else
+ raise "Unknown db"
end
+ end
- db.execute("PRAGMA foreign_keys = ON")
- VCAP::CloudController::DB.apply_migrations(db)
+ def reset_database
+ without_foreign_key_checks do
+ db.tables.each do |table|
+ db.drop_table(table)
+ end
+ VCAP::CloudController::DB.apply_migrations(db)
+ end
end
def db
@@ -83,6 +97,10 @@ def db_logger
$spec_env = VCAP::CloudController::SpecEnvironment.new
module VCAP::CloudController::SpecHelper
+ def db
+ $spec_env.db
+ end
+
def reset_database
$spec_env.reset_database
VCAP::CloudController::Models::QuotaDefinition.populate_from_config(config)
@@ -198,7 +216,7 @@ def with_em_and_thread(opts = {}, &blk)
RSpec::Matchers.define :be_recent do |expected|
match do |actual|
- actual.should be_within(2).of(Time.now)
+ actual.should be_within(5).of(Time.now)
end
end
View
8 spec/vcap/rest_api/query_spec.rb
@@ -17,7 +17,7 @@ class Book < Sequel::Model
end
before do
- db = Sequel.sqlite
+ reset_database
db.create_table :authors do
primary_key :id
@@ -40,9 +40,11 @@ class Book < Sequel::Model
Book.set_dataset(db[:books])
(num_authors - 1).times do |i|
- a = Author.create(:num_val => i, :str_val => "str #{i}")
+ # mysql does typecasting of strings to ints, so start values at 0
+ # so that the query using string tests don't find the 0 values.
+ a = Author.create(:num_val => i + 1, :str_val => "str #{i}")
books_per_author.times do |j|
- a.add_book(Book.create(:num_val => j, :str_val => "str #{i} #{j}"))
+ a.add_book(Book.create(:num_val => j + 1, :str_val => "str #{i} #{j}"))
end
end
View
37 spec/vcap/sequel_context_insensitive_string_monkeypatch_spec.rb
@@ -4,19 +4,16 @@
require "vcap/sequel_case_insensitive_string_monkeypatch"
describe "String :name" do
- before do
- @db = Sequel.sqlite
- end
-
context "with default options" do
before do
- @db.create_table :test do
+ table_name = Sham.name.to_sym
+ db.create_table table_name do
primary_key :id
String :str, :unique => true
end
@c = Class.new(Sequel::Model)
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[table_name])
@c.create(:str => "abc")
end
@@ -32,13 +29,15 @@
context "with :context_insensitive => false" do
before do
- @db.create_table :test do
+ table_name = Sham.name.to_sym
+
+ db.create_table table_name do
primary_key :id
String :str, :unique => true, :case_insensitive => false
end
@c = Class.new(Sequel::Model)
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[table_name])
@c.create(:str => "abc")
end
@@ -54,7 +53,9 @@
context "with :context_insensitive => true" do
before do
- @db.create_table :test do
+ table_name = Sham.name.to_sym
+
+ db.create_table table_name do
primary_key :id
String :str, :unique => true, :case_insensitive => true
end
@@ -64,7 +65,7 @@ def validate
validates_unique :str
end
end
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[table_name])
@c.create(:str => "abc")
end
@@ -88,21 +89,23 @@ def validate
context "alter table set_column_type" do
before do
- @db.create_table :test do
+ @table_name = Sham.name.to_sym
+
+ db.create_table @table_name do
primary_key :id
String :str, :unique => true
end
end
context "with defaults" do
it "should not result in a case sensitive column" do
- @db.alter_table :test do
+ db.alter_table @table_name do
set_column_type :str, String
end
@c = Class.new(Sequel::Model) do
end
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[@table_name])
@c.create(:str => "abc")
@c.dataset[:str => "abc"].should_not be_nil
@c.dataset[:str => "ABC"].should be_nil
@@ -111,13 +114,13 @@ def validate
context "with :context_insensitive => false" do
it "should not result in a case sensitive column" do
- @db.alter_table :test do
+ db.alter_table @table_name do
set_column_type :str, String
end
@c = Class.new(Sequel::Model) do
end
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[@table_name])
@c.create(:str => "abc")
@c.dataset[:str => "abc"].should_not be_nil
@c.dataset[:str => "ABC"].should be_nil
@@ -126,13 +129,13 @@ def validate
context "with :context_insensitive => true" do
it "should change the column" do
- @db.alter_table :test do
+ db.alter_table @table_name do
set_column_type :str, String, :case_insensitive => true
end
@c = Class.new(Sequel::Model) do
end
- @c.set_dataset(@db[:test])
+ @c.set_dataset(db[@table_name])
@c.create(:str => "abc")
@c.dataset[:str => "abc"].should_not be_nil
@c.dataset[:str => "ABC"].should_not be_nil

0 comments on commit 9e2b225

Please sign in to comment.
Something went wrong with that request. Please try again.