Skip to content
Permalink
Browse files

Fix for FUNCTION/PROCEDURE

  • Loading branch information...
Genki Sugawara
Genki Sugawara committed May 27, 2015
1 parent 638df10 commit 0a666d74a9015efd382644159351488a1d304733
@@ -15,3 +15,4 @@ mkmf.log
test.rb
Grantfile
*.grant
/_site/
@@ -205,7 +205,14 @@ def quote_user(user, host)
end

def quote_object(object)
object.split('.', 2).map {|i| i == '*' ? i : "`#{i}`" }.join('.')
if object =~ /\A(FUNCTION|PROCEDURE)\s+/i
object_type, object = object.split(/\s+/, 2)
object_type << ' '
else
object_type = ''
end

object_type + object.split('.', 2).map {|i| i == '*' ? i : "`#{i}`" }.join('.')
end

def quote_identifier(identifier)
@@ -0,0 +1,94 @@
describe 'Gratan::Client#apply' do
before(:each) do
mysql do |cli|
drop_database(cli)
create_database(cli)
create_function(cli, :my_func)
create_procedure(cli, :my_prcd)
end
end

context 'when func -> prcd' do
subject { client }

before do
apply(subject) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "FUNCTION #{TEST_DATABASE}.my_func" do
grant "EXECUTE"
end
end
RUBY
}

end

it do
apply(subject) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "PROCEDURE #{TEST_DATABASE}.my_prcd" do
grant "EXECUTE"
end
end
RUBY
}

expect(show_grants).to match_array [
"GRANT USAGE ON *.* TO 'scott'@'%'",
"GRANT EXECUTE ON PROCEDURE `#{TEST_DATABASE}`.`my_prcd` TO 'scott'@'%'"
]
end
end

context 'when prcd -> func' do
subject { client }

before do
apply(subject) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "PROCEDURE #{TEST_DATABASE}.my_prcd" do
grant "EXECUTE"
end
end
RUBY
}

end

it do
apply(subject) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "FUNCTION #{TEST_DATABASE}.my_func" do
grant "EXECUTE"
end
end
RUBY
}

expect(show_grants).to match_array [
"GRANT USAGE ON *.* TO 'scott'@'%'",
"GRANT EXECUTE ON FUNCTION `#{TEST_DATABASE}`.`my_func` TO 'scott'@'%'"
]
end
end
end
@@ -0,0 +1,59 @@
describe 'Gratan::Client#apply' do
context 'when create user with function' do
subject { client }

it do
create_functions(:foo) do
result = apply(subject) {
<<-RUBY
user 'scott', 'localhost' do
on '*.*' do
grant 'USAGE'
end
on 'FUNCTION #{TEST_DATABASE}.foo' do
grant 'EXECUTE'
end
end
RUBY
}

expect(result).to be_truthy

expect(show_grants).to match_array [
"GRANT USAGE ON *.* TO 'scott'@'localhost'",
"GRANT EXECUTE ON FUNCTION `#{TEST_DATABASE}`.`foo` TO 'scott'@'localhost'"
]
end
end
end

context 'when create user with procedure' do
subject { client }

it do
create_procedures(:foo) do
result = apply(subject) {
<<-RUBY
user 'scott', 'localhost' do
on '*.*' do
grant 'USAGE'
end
on 'PROCEDURE #{TEST_DATABASE}.foo' do
grant 'EXECUTE'
end
end
RUBY
}

expect(result).to be_truthy

expect(show_grants).to match_array [
"GRANT USAGE ON *.* TO 'scott'@'localhost'",
"GRANT EXECUTE ON PROCEDURE `#{TEST_DATABASE}`.`foo` TO 'scott'@'localhost'"
]
end
end
end
end
@@ -0,0 +1,69 @@
describe 'Gratan::Client#export' do
context 'when function exists' do
let(:grantfile) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "FUNCTION #{TEST_DATABASE}.my_func" do
grant "EXECUTE"
end
end
RUBY
}

subject { client }

before do
mysql do |cli|
drop_database(cli)
create_database(cli)
create_function(cli, :my_func)
end

apply(subject) do
grantfile
end
end

it do
expect(subject.export.strip).to eq grantfile.strip
end
end

context 'when procedure exists' do
let(:grantfile) {
<<-RUBY
user "scott", "%" do
on "*.*" do
grant "USAGE"
end
on "PROCEDURE #{TEST_DATABASE}.my_prcd" do
grant "EXECUTE"
end
end
RUBY
}

subject { client }

before do
mysql do |cli|
drop_database(cli)
create_database(cli)
create_procedure(cli, :my_prcd)
end

apply(subject) do
grantfile
end
end

it do
expect(subject.export.strip).to eq grantfile.strip
end
end
end
@@ -49,6 +49,14 @@ def create_table(client, table)
client.query("CREATE TABLE #{TEST_DATABASE}.#{table} (id INT)")
end

def create_function(client, func)
client.query("CREATE FUNCTION #{TEST_DATABASE}.#{func}() RETURNS INT RETURN 1")
end

def create_procedure(client, prcd)
client.query("CREATE PROCEDURE #{TEST_DATABASE}.#{prcd}() SELECT 1")
end

def create_tables(*tables)
mysql do |client|
begin
@@ -62,6 +70,32 @@ def create_tables(*tables)
end
end

def create_functions(*funcs)
mysql do |client|
begin
drop_database(client)
create_database(client)
funcs.each {|i| create_function(client, i) }
yield
ensure
drop_database(client)
end
end
end

def create_procedures(*prcds)
mysql do |client|
begin
drop_database(client)
create_database(client)
prcds.each {|i| create_procedure(client, i) }
yield
ensure
drop_database(client)
end
end
end

def select_users(client)
users = []

0 comments on commit 0a666d7

Please sign in to comment.
You can’t perform that action at this time.