Skip to content

Commit

Permalink
#69 - dbmigration adding delimiter $$ for Microsoft SQL Server proced…
Browse files Browse the repository at this point in the history
…ures script - change to use GO
  • Loading branch information
rbygrave committed Jul 24, 2019
1 parent 09ca161 commit 2af8ea4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 36 deletions.
23 changes: 8 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.ebean</groupId>
<artifactId>ebean-migration</artifactId>
<version>11.17.2-SNAPSHOT</version>
<version>11.18.1-SNAPSHOT</version>

<parent>
<groupId>org.avaje</groupId>
Expand Down Expand Up @@ -73,6 +73,13 @@ mvn install:install-file -Dfile=/some/path/to/ojdbc7.jar -DgroupId=oracle \
<!-- <scope>test</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>org.avaje.composite</groupId>
<artifactId>logback</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.ebean.test</groupId>
<artifactId>ebean-test-docker</artifactId>
Expand All @@ -94,20 +101,6 @@ mvn install:install-file -Dfile=/some/path/to/ojdbc7.jar -DgroupId=oracle \
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/io/ebean/migration/ddl/DdlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -14,7 +14,7 @@ public class DdlParser {
/**
* Break up the sql in reader into a list of statements using the semi-colon and $$ delimiters;
*/
public List<String> parse(StringReader reader) {
public List<String> parse(Reader reader) {

try {
BufferedReader br = new BufferedReader(reader);
Expand Down Expand Up @@ -43,6 +43,9 @@ static class StatementsSeparator {

private static final String EOL = "\n";

private static final String GO = "GO";
private static final String PROCEDURE = " PROCEDURE ";

ArrayList<String> statements = new ArrayList<>();

boolean trimDelimiter;
Expand All @@ -51,6 +54,7 @@ static class StatementsSeparator {

StringBuilder sb = new StringBuilder();

int lineCount;
int quoteCount;

void lineContainsDollars(String line) {
Expand All @@ -74,11 +78,21 @@ void endOfStatement(String line) {
sb.append(line);
statements.add(sb.toString().trim());
quoteCount = 0;
lineCount = 0;
inDbProcedure = false;
sb = new StringBuilder();
}

/**
* Process the next line of the script.
*/
void nextLine(String line) {

if (line.trim().equals(GO)) {
endOfStatement("");
return;
}

if (line.contains("$$")) {
lineContainsDollars(line);
return;
Expand All @@ -94,6 +108,11 @@ void nextLine(String line) {
return;
}

if (lineCount == 0 && isStartDbProcedure(line)) {
inDbProcedure = true;
}

lineCount++;
quoteCount += countQuotes(line);
if (hasOddQuotes()) {
// must continue
Expand Down Expand Up @@ -123,6 +142,13 @@ void nextLine(String line) {
}
}

/**
* Return true if the start of DB procedure is detected.
*/
private boolean isStartDbProcedure(String line) {
return line.length() > 26 && line.substring(0, 26).toUpperCase().contains(PROCEDURE);

This comment has been minimized.

Copy link
@rPraml

rPraml Jul 25, 2019

Contributor

@rbygrave this is a bit fragile, becase it will detect only

CREATE OR ALTER PROCEDURE

and not

CREATE   OR   ALTER   PROCEDURE

but should work in most cases

}

/**
* Return true if the count of quotes is odd.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void sqlServer_migration() throws SQLException {
readQuery(connection, "select * from m3");
}

sqlServerContainer.stopRemove();
// sqlServerContainer.stopRemove();
}

@Test
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/io/ebean/migration/ddl/DdlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.util.List;

Expand Down Expand Up @@ -31,6 +33,31 @@ public void parse_functionWithInlineComments() {
assertThat(stmts).containsExactly(plpgsql);
}

@Test
public void parse_sqlserver_create_procs() throws FileNotFoundException {

FileReader fr = new FileReader("src/test/resources/dbmig_sqlserver/I__create_procs.sql");
final List<String> statements = parser.parse(fr);

assertThat(statements).hasSize(11);
assertThat(statements.get(0)).isEqualTo("if not exists (select name from sys.types where name = 'ebean_bigint_tvp')\n" +
"create type ebean_bigint_tvp as table (c1 bigint)");

assertThat(statements.get(6)).isEqualTo("if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp')\n" +
"create type ebean_nvarchar_tvp as table (c1 nvarchar(max))");

assertThat(statements.get(8)).isEqualTo("CREATE OR ALTER PROCEDURE usp_ebean_drop_default_constraint @tableName nvarchar(255), @columnName nvarchar(255)\n" +
"AS SET NOCOUNT ON\n" +
"declare @tmp nvarchar(1000)\n" +
"BEGIN\n" +
" select @tmp = t1.name from sys.default_constraints t1\n" +
" join sys.columns t2 on t1.object_id = t2.default_object_id\n" +
" where t1.parent_object_id = OBJECT_ID(@tableName) and t2.name = @columnName;\n" +
"\n" +
" if @tmp is not null EXEC('alter table ' + @tableName +' drop constraint ' + @tmp);\n" +
"END");
}

@Test
public void parse_ignoresEmptyLines() {

Expand Down
57 changes: 39 additions & 18 deletions src/test/resources/dbmig_sqlserver/I__create_procs.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
-- Initial script to create stored procedures etc for sqlserver platform

-- create table-value-parameters
if not exists (select name from sys.types where name = 'ebean_bigint_tvp')
create type ebean_bigint_tvp as table (c1 bigint);
create type ebean_bigint_tvp as table
(
c1 bigint
)
GO
if not exists (select name from sys.types where name = 'ebean_float_tvp')
create type ebean_float_tvp as table (c1 float);
create type ebean_float_tvp as table
(
c1 float
)
GO
if not exists (select name from sys.types where name = 'ebean_bit_tvp')
create type ebean_bit_tvp as table (c1 bit);
create type ebean_bit_tvp as table
(
c1 bit
)
GO
if not exists (select name from sys.types where name = 'ebean_date_tvp')
create type ebean_date_tvp as table (c1 date);
create type ebean_date_tvp as table
(
c1 date
)
GO
if not exists (select name from sys.types where name = 'ebean_time_tvp')
create type ebean_time_tvp as table (c1 time);
create type ebean_time_tvp as table
(
c1 time
)
GO
if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp')
create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier);
create type ebean_uniqueidentifier_tvp as table
(
c1 uniqueidentifier
)
GO
if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp')
create type ebean_nvarchar_tvp as table (c1 nvarchar(max));
create type ebean_nvarchar_tvp as table
(
c1 nvarchar(max)
)
GO

delimiter $$
--
-- PROCEDURE: usp_ebean_drop_indices TABLE, COLUMN
-- deletes all indices referring to TABLE.COLUMN
Expand All @@ -42,9 +66,8 @@ BEGIN
CLOSE index_cursor;
DEALLOCATE index_cursor;
END
$$
GO

delimiter $$
--
-- PROCEDURE: usp_ebean_drop_default_constraint TABLE, COLUMN
-- deletes the default constraint, which has a random name
Expand All @@ -59,9 +82,8 @@ BEGIN

if @tmp is not null EXEC('alter table ' + @tableName +' drop constraint ' + @tmp);
END
$$
GO

delimiter $$
--
-- PROCEDURE: usp_ebean_drop_constraints TABLE, COLUMN
-- deletes constraints and foreign keys refering to TABLE.COLUMN
Expand Down Expand Up @@ -93,9 +115,8 @@ BEGIN
CLOSE name_cursor;
DEALLOCATE name_cursor;
END
$$
GO

delimiter $$
--
-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN
-- deletes the column annd ensures that all indices and constraints are dropped first
Expand All @@ -111,4 +132,4 @@ BEGIN
set @sql = 'alter table ' + @tableName + ' drop column ' + @columnName;
EXECUTE(@sql);
END
$$
GO

0 comments on commit 2af8ea4

Please sign in to comment.