Skip to content

Commit

Permalink
reformat java files
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Aug 22, 2023
1 parent f8e723d commit d163869
Show file tree
Hide file tree
Showing 28 changed files with 534 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import name.nkonev.r2dbc.migrate.reader.MigrateResource;
import reactor.core.publisher.Flux;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public boolean isPremigration() {
@Override
public String toString() {
return "MigrationInfo{" +
"version=" + version +
", description='" + description + '\'' +
", splitByLine=" + splitByLine +
", transactional=" + transactional +
", premigration=" + premigration +
'}';
"version=" + version +
", description='" + description + '\'' +
", splitByLine=" + splitByLine +
", transactional=" + transactional +
", premigration=" + premigration +
'}';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,57 @@

import io.r2dbc.spi.Connection;
import io.r2dbc.spi.Statement;

import java.util.List;

public class H2Queries implements SqlQueries {

private final String migrationsSchema;
private final String migrationsTable;
public H2Queries(String migrationsSchema, String migrationsTable) {
this.migrationsSchema = migrationsSchema;
this.migrationsTable = migrationsTable;
}

private boolean schemaIsDefined() {
return !StringUtils.isEmpty(migrationsSchema);
}

private String quoteAsObject(String input) {
return "\"" + input + "\"";
}

private String withMigrationsTable(String template) {
if (schemaIsDefined()) {
return String.format(template, quoteAsObject(migrationsSchema) + "." + quoteAsObject(migrationsTable));
} else {
return String.format(template, quoteAsObject(migrationsTable));
}
}

@Override
public List<String> createInternalTables() {
return List.of(
withMigrationsTable("create table if not exists %s (id int primary key, description text)")
);
}

@Override
public String getMaxMigration() {
return withMigrationsTable("select max(id) as max from %s");
}

public String insertMigration() {
return withMigrationsTable("insert into %s(id, description) values ($1, $2)");
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("$1", migrationInfo.getVersion())
.bind("$2", migrationInfo.getDescription());
}
private final String migrationsSchema;
private final String migrationsTable;

public H2Queries(String migrationsSchema, String migrationsTable) {
this.migrationsSchema = migrationsSchema;
this.migrationsTable = migrationsTable;
}

private boolean schemaIsDefined() {
return !StringUtils.isEmpty(migrationsSchema);
}

private String quoteAsObject(String input) {
return "\"" + input + "\"";
}

private String withMigrationsTable(String template) {
if (schemaIsDefined()) {
return String.format(template, quoteAsObject(migrationsSchema) + "." + quoteAsObject(migrationsTable));
} else {
return String.format(template, quoteAsObject(migrationsTable));
}
}

@Override
public List<String> createInternalTables() {
return List.of(
withMigrationsTable("create table if not exists %s (id int primary key, description text)")
);
}

@Override
public String getMaxMigration() {
return withMigrationsTable("select max(id) as max from %s");
}

public String insertMigration() {
return withMigrationsTable("insert into %s(id, description) values ($1, $2)");
}

@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("$1", migrationInfo.getVersion())
.bind("$2", migrationInfo.getDescription());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@

public class H2TableLocker extends AbstractTableLocker implements Locker {

private final String migrationsSchema;
private final String migrationsLockTable;

public H2TableLocker(String migrationsSchema, String migrationsLockTable) {
this.migrationsSchema = migrationsSchema;
this.migrationsLockTable = migrationsLockTable;
}

private boolean schemaIsDefined() {
return !StringUtils.isEmpty(migrationsSchema);
}

private String quoteAsObject(String input) {
return "\"" + input + "\"";
}

private String withMigrationsLockTable(String template) {
if (schemaIsDefined()) {
return String.format(template, quoteAsObject(migrationsSchema) + "." + quoteAsObject(migrationsLockTable));
} else {
return String.format(template, quoteAsObject(migrationsLockTable));
private final String migrationsSchema;
private final String migrationsLockTable;

public H2TableLocker(String migrationsSchema, String migrationsLockTable) {
this.migrationsSchema = migrationsSchema;
this.migrationsLockTable = migrationsLockTable;
}

private boolean schemaIsDefined() {
return !StringUtils.isEmpty(migrationsSchema);
}

private String quoteAsObject(String input) {
return "\"" + input + "\"";
}

private String withMigrationsLockTable(String template) {
if (schemaIsDefined()) {
return String.format(template, quoteAsObject(migrationsSchema) + "." + quoteAsObject(migrationsLockTable));
} else {
return String.format(template, quoteAsObject(migrationsLockTable));
}
}

@Override
public List<String> createInternalTables() {
return List.of(
withMigrationsLockTable("create table if not exists %s (id int primary key, locked boolean not null)"),
withMigrationsLockTable("insert into %s(id, locked) select * from (select 1, false) x ") + withMigrationsLockTable(" where not exists(select * from %s)")
);
}

@Override
public io.r2dbc.spi.Statement tryAcquireLock(Connection connection) {
return connection.createStatement(withMigrationsLockTable("update %s set locked = true where id = 1 and locked = false"));
}

@Override
public io.r2dbc.spi.Statement releaseLock(Connection connection) {
return connection.createStatement(withMigrationsLockTable("update %s set locked = false where id = 1"));
}
}

@Override
public List<String> createInternalTables() {
return List.of(
withMigrationsLockTable("create table if not exists %s (id int primary key, locked boolean not null)"),
withMigrationsLockTable("insert into %s(id, locked) select * from (select 1, false) x ")+withMigrationsLockTable(" where not exists(select * from %s)")
);
}

@Override
public io.r2dbc.spi.Statement tryAcquireLock(Connection connection) {
return connection.createStatement(withMigrationsLockTable("update %s set locked = true where id = 1 and locked = false"));
}

@Override
public io.r2dbc.spi.Statement releaseLock(Connection connection) {
return connection.createStatement(withMigrationsLockTable("update %s set locked = false where id = 1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ private String withMigrationsTable(String template) {
public List<String> createInternalTables() {
if (schemaIsDefined()) {
return List.of(
String.format("if not exists (select * from sys.tables t join sys.schemas s on (t.schema_id = s.schema_id) where s.name = %s and t.name = %s) ", quoteAsString(migrationsSchema), quoteAsString(migrationsTable))
+ String.format("create table %s.%s (id int primary key, description text)", quoteAsObject(migrationsSchema), quoteAsObject(migrationsTable))
String.format("if not exists (select * from sys.tables t join sys.schemas s on (t.schema_id = s.schema_id) where s.name = %s and t.name = %s) ", quoteAsString(migrationsSchema), quoteAsString(migrationsTable))
+ String.format("create table %s.%s (id int primary key, description text)", quoteAsObject(migrationsSchema), quoteAsObject(migrationsTable))

);
} else {
return List.of(
String.format("if not exists (SELECT 1 FROM sys.Tables WHERE Name = N%s AND Type = N'U') ", quoteAsString(migrationsTable))
+ String.format("create table %s (id int primary key, description text)", quoteAsObject(migrationsTable))
String.format("if not exists (SELECT 1 FROM sys.Tables WHERE Name = N%s AND Type = N'U') ", quoteAsString(migrationsTable))
+ String.format("create table %s (id int primary key, description text)", quoteAsObject(migrationsTable))
);
}

Expand All @@ -64,8 +64,8 @@ public String insertMigration() {
@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("@id", migrationInfo.getVersion())
.bind("@descr", migrationInfo.getDescription());
.createStatement(insertMigration())
.bind("@id", migrationInfo.getVersion())
.bind("@descr", migrationInfo.getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private String withMigrationsLockTable(String template) {
public List<String> createInternalTables() {
if (schemaIsDefined()) {
return List.of(
String.format("if not exists (select * from sys.tables t join sys.schemas s on (t.schema_id = s.schema_id) where s.name = %s and t.name = %s) ", quoteAsString(migrationsSchema), quoteAsString(migrationsLockTable))
+ String.format("create table %s.%s (id int primary key, locked bit not null)", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable)),
String.format("if not exists (select * from sys.tables t join sys.schemas s on (t.schema_id = s.schema_id) where s.name = %s and t.name = %s) ", quoteAsString(migrationsSchema), quoteAsString(migrationsLockTable))
+ String.format("create table %s.%s (id int primary key, locked bit not null)", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable)),

String.format("if not exists (select * from %s.%s where id = 1) ", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable)) // dbo.migrations_lock
+ String.format("insert into %s.%s(id, locked) values (1, 'false')", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable))
String.format("if not exists (select * from %s.%s where id = 1) ", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable)) // dbo.migrations_lock
+ String.format("insert into %s.%s(id, locked) values (1, 'false')", quoteAsObject(migrationsSchema), quoteAsObject(migrationsLockTable))
);
} else {
return List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import io.r2dbc.spi.Connection;
import io.r2dbc.spi.Statement;

import java.util.List;

public class MariadbQueries implements SqlQueries {

private final String migrationsSchema;
private final String migrationsTable;

public MariadbQueries(String migrationsSchema, String migrationsTable) {
this.migrationsSchema = migrationsSchema;
this.migrationsTable = migrationsTable;
Expand Down Expand Up @@ -48,9 +50,9 @@ public String insertMigration() {
@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
return connection
.createStatement(insertMigration())
.bind(0, migrationInfo.getVersion())
.bind(1, migrationInfo.getDescription());
.createStatement(insertMigration())
.bind(0, migrationInfo.getVersion())
.bind(1, migrationInfo.getDescription());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class MariadbSessionLocker implements Locker {
private final int timeout; // seconds

public MariadbSessionLocker(String migrationsSchema, String migrationsLockTable) {
this.lockname = migrationsSchema + "." + migrationsLockTable;
this.lockname = migrationsSchema + "." + migrationsLockTable;
this.timeout = 5; // seconds
}

public MariadbSessionLocker(String migrationsSchema, String migrationsLockTable, int timeoutSec) {
this.lockname = migrationsSchema + "." + migrationsLockTable;
this.lockname = migrationsSchema + "." + migrationsLockTable;
this.timeout = timeoutSec; // seconds
}

Expand All @@ -36,27 +36,27 @@ public List<String> createInternalTables() {
@Override
public io.r2dbc.spi.Statement tryAcquireLock(Connection connection) {
return connection.createStatement("select get_lock(?, ?) as lock_result")
.bind(0, lockname)
.bind(1, timeout);
.bind(0, lockname)
.bind(1, timeout);
}

@Override
public io.r2dbc.spi.Statement releaseLock(Connection connection) {
return connection.createStatement("select release_lock(?)")
.bind(0, lockname);
.bind(0, lockname);
}

@Override
public Mono<? extends Object> extractResultOrError(Mono<? extends Result> lockStatement) {
return lockStatement.flatMap(o -> Mono.from(o.map(getResultSafely("lock_result", Integer.class, 0))))
.flatMap(anInteger -> {
if (anInteger == 0) {
return Mono.error(new RuntimeException("False result"));
} else {
return Mono.just(anInteger);
}
}).doOnSuccess(aBoolean -> {
LOGGER.info("Acquiring database-specific lock {}", aBoolean);
});
.flatMap(anInteger -> {
if (anInteger == 0) {
return Mono.error(new RuntimeException("False result"));
} else {
return Mono.just(anInteger);
}
}).doOnSuccess(aBoolean -> {
LOGGER.info("Acquiring database-specific lock {}", aBoolean);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public String insertMigration() {
@Override
public Statement createInsertMigrationStatement(Connection connection, FilenameParser.MigrationInfo migrationInfo) {
return connection
.createStatement(insertMigration())
.bind("id", migrationInfo.getVersion())
.bind("descr", migrationInfo.getDescription());
.createStatement(insertMigration())
.bind("id", migrationInfo.getVersion())
.bind("descr", migrationInfo.getDescription());
}
}
Loading

0 comments on commit d163869

Please sign in to comment.