Skip to content

Commit

Permalink
Substitution in SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Dec 1, 2024
1 parent a1a75b8 commit eda7a7d
Show file tree
Hide file tree
Showing 28 changed files with 293 additions and 196 deletions.
21 changes: 0 additions & 21 deletions development.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
# Reset & open PostgreSQL logs
```
cd ./docker
docker-compose down -v; docker-compose up -d; docker-compose logs -f postgresql
```

# Open MS SQL shell
```bash
docker-compose exec mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'yourStrong(!)Password'
```

# Open PostgreSQL shell
```
docker-compose exec postgresql psql -U r2dbc
```

# Open MySQL shell
```
docker-compose exec mysql mysql -umysql-user -pmysql-password -Dr2dbc
```

# Making a release
```bash
export JAVA_HOME=/usr/lib/jvm/java-17
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>name.nkonev.r2dbc-migrate</groupId>
<artifactId>r2dbc-migrate-parent</artifactId>
<version>3.1.3-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<name>r2dbc-migrate parent project</name>
<description>R2DBC migration tool and library</description>
<packaging>pom</packaging>
Expand Down Expand Up @@ -51,6 +51,7 @@
<maven.gpg.plugin>3.0.1</maven.gpg.plugin>
<reflections.version>0.10.2</reflections.version>
<logcaptor.version>2.9.0</logcaptor.version>
<commons.text.version>1.12.0</commons.text.version>
</properties>

<modules>
Expand Down
14 changes: 10 additions & 4 deletions r2dbc-migrate-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<parent>
<artifactId>r2dbc-migrate-parent</artifactId>
<groupId>name.nkonev.r2dbc-migrate</groupId>
<version>3.1.3-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<artifactId>r2dbc-migrate-resource-reader-api</artifactId>
<groupId>name.nkonev.r2dbc-migrate</groupId>
<version>3.1.3-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
Expand All @@ -25,6 +25,12 @@
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons.text.version}</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
Expand Down Expand Up @@ -84,13 +90,13 @@
<dependency>
<artifactId>r2dbc-migrate-resource-reader-reflections</artifactId>
<groupId>name.nkonev.r2dbc-migrate</groupId>
<version>3.1.3-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>r2dbc-migrate-resource-reader-spring</artifactId>
<groupId>name.nkonev.r2dbc-migrate</groupId>
<version>3.1.3-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BunchOfResourcesEntry {
private Boolean splitByLine; // only for JUST_FILE
private Boolean transactional; // only for JUST_FILE
private Boolean premigration; // only for JUST_FILE

private Boolean substitute; // only for JUST_FILE

public BunchOfResourcesEntry() {
}
Expand Down Expand Up @@ -47,6 +47,15 @@ public void setType(BunchOfResourcesType type) {
this.type = type;
}

// only for JUST_FILE
public Boolean getSubstitute() {
return substitute;
}

public void setSubstitute(Boolean substitute) {
this.substitute = substitute;
}

@Override
public String toString() {
return "BunchOfResourcesEntry{" +
Expand Down Expand Up @@ -103,12 +112,13 @@ public static BunchOfResourcesEntry ofConventionallyNamedFiles(String... resourc
return e;
}

public static BunchOfResourcesEntry ofJustFile(long version, String description, String resourcePath) {
public static BunchOfResourcesEntry ofJustFile(long version, String description, String resourcePath, boolean substitute) {
var e = new BunchOfResourcesEntry();
e.setVersion(version);
// e.setType(BunchOfResourcesType.JUST_FILE); set in setVersion()
e.setResourcePath(resourcePath);
e.setDescription(description);
e.setSubstitute(substitute);
return e;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String insertMigration() {
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
public Statement createInsertMigrationStatement(Connection connection, MigrationMetadata migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("$1", migrationInfo.getVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String insertMigration() {
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
public Statement createInsertMigrationStatement(Connection connection, MigrationMetadata migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("@id", migrationInfo.getVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String insertMigration() {
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
public Statement createInsertMigrationStatement(Connection connection, MigrationMetadata migrationInfo) {
return connection
.createStatement(insertMigration())
.bind(0, migrationInfo.getVersion())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package name.nkonev.r2dbc.migrate.core;

public class MigrationMetadata {
private final long version;
private final String description;
private final boolean splitByLine;
private final boolean transactional;
private final boolean premigration;
private final boolean substitute;

public MigrationMetadata(long version, String description, boolean splitByLine, boolean transactional, boolean premigration, boolean substitute) {
this.version = version;
this.description = description;
this.splitByLine = splitByLine;
this.transactional = transactional;
this.premigration = premigration;
this.substitute = substitute;
}

public String getDescription() {
return description;
}

public long getVersion() {
return version;
}

public boolean isSplitByLine() {
return splitByLine;
}

public boolean isTransactional() {
return transactional;
}

public boolean isPremigration() {
return premigration;
}

public boolean isSubstitute() {
return substitute;
}

@Override
public String toString() {
return "MigrationMetadata{" +
"version=" + version +
", description='" + description + '\'' +
", splitByLine=" + splitByLine +
", transactional=" + transactional +
", premigration=" + premigration +
", substitute=" + substitute +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package name.nkonev.r2dbc.migrate.core;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public abstract class MigrationMetadataFactory {

public static MigrationMetadata getMigrationMetadata(String filename) {
final String sql = ".sql";
if (filename == null || !filename.endsWith(sql)) {
throw new RuntimeException("File name should ends with " + sql);
}
String substring = filename.substring(0, filename.length() - sql.length());
String[] array = substring.split("__");
if (array.length == 3) {
String modifiersRaw = array[2];
List<String> modifiers = Arrays.asList(modifiersRaw.split(","));
boolean nonTransactional = modifiers.contains("nontransactional");
boolean premigration = modifiers.contains("premigration");
boolean split = modifiers.contains("split");
boolean substitute = modifiers.contains("substitute");
return new MigrationMetadata(getVersion(array[0]), getDescription(array[1]), split, !nonTransactional, premigration, substitute);
} else if (array.length == 2) {
// no split
return new MigrationMetadata(getVersion(array[0]), getDescription(array[1]), false, true, false, false);
} else {
throw new RuntimeException("Invalid file name '" + filename + "'");
}
}

public static MigrationMetadata getMigrationMetadata(long version, String description, Boolean splitByLine, Boolean transactional, Boolean premigration, Boolean substitute) {
return new MigrationMetadata(
version,
description,
Optional.ofNullable(splitByLine).orElse(false),
Optional.ofNullable(transactional).orElse(false),
Optional.ofNullable(premigration).orElse(false),
Optional.ofNullable(substitute).orElse(false)
);
}

private static long getVersion(String vPart) {
String v = vPart.replace("V", "");
return Long.parseLong(v);
}

private static String getDescription(String descriptionPart) {
return descriptionPart.replace("_", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String insertMigration() {
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
public Statement createInsertMigrationStatement(Connection connection, MigrationMetadata migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("id", migrationInfo.getVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String insertMigration() {
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
public Statement createInsertMigrationStatement(Connection connection, MigrationMetadata migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("$1", migrationInfo.getVersion())
Expand Down
Loading

0 comments on commit eda7a7d

Please sign in to comment.