Skip to content

Commit

Permalink
Fix creation of stored procedures that contain insert statements
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Sep 19, 2023
1 parent cbf0cc9 commit 7a6e650
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def query_requires_identity_insert?(sql)
end

def insert_sql?(sql)
!(sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)/i).nil?
!(sql =~ /\A\s*(INSERT|EXEC sp_executesql N'INSERT)/i).nil?
end

def identity_columns(table_name)
Expand Down
18 changes: 18 additions & 0 deletions test/cases/migration_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,22 @@ class MigrationTestSQLServer < ActiveRecord::TestCase
refute_includes schemas, { "name" => "some schema" }
end
end

describe 'creating stored procedure' do
it 'stored procedure contains inserts are created successfully' do
sql = <<-SQL
CREATE OR ALTER PROCEDURE do_some_task
AS
IF NOT EXISTS(SELECT * FROM sys.objects WHERE type = 'U' AND name = 'SomeTableName')
BEGIN
CREATE TABLE SomeTableName (SomeNum int PRIMARY KEY CLUSTERED);
INSERT INTO SomeTableName(SomeNum) VALUES(1);
END
SQL

assert_nothing_raised { connection.execute(sql) }
ensure
connection.execute("DROP PROCEDURE IF EXISTS dbo.do_some_task;")
end
end
end

0 comments on commit 7a6e650

Please sign in to comment.