Skip to content

Commit

Permalink
Issue-56: User and Password not picked from liquibase.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-slobodcicov committed Nov 8, 2020
1 parent 79b3f96 commit 1607619
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 66 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
1. Make your code changes
1. Commit your changes (`git commit -am 'Add some feature/folder'`)
1. Push your changes (`git push origin master`)
1. Create a Pull Request.
1. Create a Pull Request to [main](https://github.com/liquibase/liquibase-mongodb) branch.

## Important Guidelines!

- No acronyms in folder names in order to provide clarity and avoid collisions.
- No acronyms in folder name in order to provide clarity and avoid collisions.
- Do not use camel case or underscores for folder names.
- Arrange your folders in alphabetical order.
- Folder name or folder description should not contain special characters.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Liquibase turned to be the most feasible tool to extend as it allows to define c
}
}
```
For the command line is required to copy to `[liquibase]/lib`
libraries : `jackson-annotations-2.11.3.jar, jackson-core-2.11.3.jar, jackson-databind-2.11.3.jar`

* New properties added
```properties
# If disabled can be used on API which do not support validators (Azure Cosmos DB with Mongo API, Amazon DocumentDB)
Expand Down Expand Up @@ -109,6 +112,7 @@ mongo-java-driver:3.12.7
### Adjust connection string

Connection url can be adjusted here: [`url`](./src/test/resources/liquibase.properties)
[Connection String Format](https://docs.mongodb.com/manual/reference/connection-string/)
Run Integration tests by enabling `run-its` profile

### Run integration tests
Expand Down
20 changes: 9 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<maven-help-plugin.version>3.2.0</maven-help-plugin.version>
<jackson-core.version>2.11.3</jackson-core.version>
<snakeyaml.version>1.12</snakeyaml.version>
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -88,11 +91,6 @@
<version>${mongodb-driver.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -199,8 +197,8 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<optimize>true</optimize>
<debug>true</debug>
<encoding>${project.build.sourceEncoding}</encoding>
Expand All @@ -226,7 +224,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>1.8</version>
<version>${maven.compiler.target}</version>
</requireJavaVersion>
</rules>
</configuration>
Expand All @@ -236,12 +234,12 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<version>${build-helper-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<version>${maven-release-plugin.version}</version>
<configuration>
<stagingRepository>/tmp/maven-snapshot</stagingRepository>
<mavenExecutorId>forked-path</mavenExecutorId>
Expand All @@ -257,7 +255,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<version>${nexus-staging-maven-plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import liquibase.logging.Logger;
import liquibase.nosql.changelog.AbstractNoSqlHistoryService;
import liquibase.util.LiquibaseUtil;
import liquibase.util.StringUtil;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

Expand Down Expand Up @@ -192,7 +192,7 @@ public String extractTag(final ChangeSet changeSet) {
for (Change change : changeSet.getChanges()) {
if (change instanceof TagDatabaseChange) {
TagDatabaseChange tagChange = (TagDatabaseChange) change;
tag = StringUtils.trimToNull(tagChange.getTag());
tag = StringUtil.trimToNull(tagChange.getTag());
}
}
return tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
package liquibase.ext.mongodb.database;

import java.sql.*;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import liquibase.Scope;
import liquibase.exception.DatabaseException;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.util.Properties;
import java.util.logging.Logger;

public class MongoClientDriver implements Driver {

@Override
public Connection connect(String url, Properties info) throws SQLException {
return null;
public Connection connect(String url, Properties info) {
//Not applicable for non JDBC DBs
throw new UnsupportedOperationException("Cannot initiate a SQL Connection for a NoSql DB");
}

public MongoClient connect(final ConnectionString connectionString) throws DatabaseException {
final MongoClient client;
try {
client = MongoClients.create(connectionString);
} catch (final Exception e) {
throw new DatabaseException("Connection could not be established to: "
+ connectionString.getConnectionString(), e);
}
return client;
}

@Override
public boolean acceptsURL(String url) throws SQLException {
public boolean acceptsURL(String url) {
return false;
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
return new DriverPropertyInfo[0];
}

Expand All @@ -37,7 +57,7 @@ public boolean jdbcCompliant() {
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
public Logger getParentLogger() {
return (Logger) Scope.getCurrentScope().getLog(getClass());
}
}
39 changes: 33 additions & 6 deletions src/main/java/liquibase/ext/mongodb/database/MongoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
*/

import com.mongodb.ConnectionString;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import liquibase.exception.DatabaseException;
import liquibase.ext.mongodb.statement.BsonUtils;
import liquibase.nosql.database.AbstractNoSqlConnection;
import liquibase.exception.DatabaseException;
import liquibase.util.StringUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.sql.Driver;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;

@Getter
Expand All @@ -45,7 +48,7 @@ public class MongoConnection extends AbstractNoSqlConnection {

public static final int DEFAULT_PORT = 27017;
public static final String MONGO_PREFIX = MongoLiquibaseDatabase.MONGODB_PRODUCT_SHORT_NAME + "://";
public final String MONGO_CONNECTION_STRING_PATTERN = "%s/%s";
public static final String MONGO_CONNECTION_STRING_PATTERN = "%s/%s";

private ConnectionString connectionString;

Expand All @@ -71,16 +74,40 @@ public String getURL() {
return String.join(",", ofNullable(this.connectionString).map(ConnectionString::getHosts).orElse(Collections.emptyList()));
}

@Override
public String getConnectionUserName() {
return ofNullable(this.connectionString).map(ConnectionString::getCredential)
.map(MongoCredential::getUserName).orElse("");
}

@Override
public void open(final String url, final Driver driverObject, final Properties driverProperties) throws DatabaseException {

try {
this.connectionString = new ConnectionString(url);
this.client = MongoClients.create(this.connectionString);

String urlWithCredentials = url;

if (nonNull(driverProperties)) {

final Optional<String> user = Optional.ofNullable(StringUtil.trimToNull(driverProperties.getProperty("user")));
final Optional<String> password = Optional.ofNullable(StringUtil.trimToNull(driverProperties.getProperty("password")));

if (user.isPresent() && password.isPresent()) {
// injects credentials
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
urlWithCredentials = MONGO_PREFIX + user.get() + ":" + password.get() + "@" + StringUtil.trimToEmpty(url).replaceFirst(MONGO_PREFIX, "");
}
}

this.connectionString = new ConnectionString(urlWithCredentials);

this.client = ((MongoClientDriver) driverObject).connect(connectionString);

this.database = this.client.getDatabase(Objects.requireNonNull(this.connectionString.getDatabase()))
.withCodecRegistry(BsonUtils.uuidCodecRegistry());
} catch (final Exception e) {
throw new DatabaseException("Could not open connection to database: " + connectionString.getDatabase(), e);
throw new DatabaseException("Could not open connection to database: "
+ ofNullable(connectionString).map(ConnectionString::getDatabase).orElse("UNKNOWN"), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Date;

import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.EMPTY;

public class MongoChangeLogLock extends DatabaseChangeLogLock{

Expand Down Expand Up @@ -59,7 +58,7 @@ public String toString() {
public static String formLockedBy() {
try {
final String HOST_NAME = NetUtil.getLocalHostName();
final String HOST_DESCRIPTION = ofNullable(System.getProperty("liquibase.hostDescription")).map(v -> "#" + v).orElse(EMPTY);
final String HOST_DESCRIPTION = ofNullable(System.getProperty("liquibase.hostDescription")).map(v -> "#" + v).orElse("");
final String HOST_ADDRESS = NetUtil.getLocalHostAddress();
return HOST_NAME + HOST_DESCRIPTION + " (" + HOST_ADDRESS + ")";
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public void init() throws DatabaseException {
}

if (!hasDatabaseChangeLogTable()) {
getLogger().info("Create Database Change Log Container");
getLogger().info("Create Database Change Log Collection");

// If there is no table in the database for recording change history create one.
this.getLogger().info("Creating database history container with name: "
this.getLogger().info("Creating database history collection with name: "
+ getDatabase().getConnection().getCatalog() + "." + this.getDatabaseChangeLogTableName());
createRepository();
getLogger().info("Created database history container : "
getLogger().info("Created database history collection : "
+ getDatabase().getConnection().getCatalog() + "." + this.getDatabaseChangeLogTableName());
this.hasDatabaseChangeLogTable = TRUE;
}
Expand Down Expand Up @@ -249,7 +249,7 @@ public void destroy() {

if (existsRepository()) {
dropRepository();
getLogger().info("Dropped Container Database Change Log: " + getDatabaseChangeLogTableName());
getLogger().info("Dropped Collection Database Change Log: " + getDatabaseChangeLogTableName());
} else {
getLogger().warning("Cannot Drop Collection Database Change Log as not found: " + getDatabaseChangeLogTableName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public int getDatabaseMinorVersion() throws DatabaseException {
return 0;
}

@Override
public String getConnectionUserName() {
return null;
}

@Override
public void attached(final Database database) {
// Do nothing
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/liquibase/nosql/database/AbstractNoSqlDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public int getPriority() {
return PRIORITY_DATABASE;
}

@Override
public Integer getDefaultScaleForNativeDataType(final String nativeDataType) {
return null;
}

@Override
public boolean supportsInitiallyDeferrableColumns() {
return false;
Expand Down Expand Up @@ -172,16 +167,6 @@ public String escapeViewName(final String catalogName, final String schemaName,
return null;
}

@Override
public void commit() throws DatabaseException {
//TODO: implementation
}

@Override
public void rollback() throws DatabaseException {
//TODO: implementation
}

@Override
public String escapeStringForDatabase(final String string) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void init() throws DatabaseException {
+ (getDatabase().getConnection()).getCatalog() + "." + getDatabaseChangeLogLockTableName());
createRepository();
database.commit();
getLogger().info("Created database lock Container: " + getDatabaseChangeLogLockTableName());
getLogger().info("Created database lock Collection: " + getDatabaseChangeLogLockTableName());
this.hasDatabaseChangeLogLockTable = true;
}
if (!adjustedChangeLogLockTable) {
Expand Down Expand Up @@ -253,8 +253,9 @@ public void reset() {
@Override
public void destroy() {
try {
getLogger().info("Dropping Container Database Change Log Lock: " + getDatabaseChangeLogLockTableName());
getLogger().info("Dropping Collection Database Change Log Lock: " + getDatabaseChangeLogLockTableName());
dropRepository();
getLogger().info("Dropped Collection Database Change Log Lock: " + getDatabaseChangeLogLockTableName());
database.commit();
reset();
} catch (final DatabaseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import liquibase.ext.AbstractMongoIntegrationTest;
import liquibase.ext.mongodb.statement.FindAllStatement;
import liquibase.ext.mongodb.statement.InsertOneStatement;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.junit.jupiter.api.Test;

Expand All @@ -27,8 +26,6 @@ class AdjustChangeLogCollectionStatementIT extends AbstractMongoIntegrationTest

protected FindAllStatement findAllStatement = new FindAllStatement(LOG_COLLECTION_NAME);

protected MongoRanChangeSetToDocumentConverter converter = new MongoRanChangeSetToDocumentConverter();

@Test
void executeToJSTest() {
AdjustChangeLogCollectionStatement adjustChangeLogCollectionStatement =
Expand Down Expand Up @@ -67,8 +64,8 @@ void executeToJSTest() {
adjustChangeLogCollectionStatement.execute(connection);

Optional<Document> options = ofNullable(connection.getDatabase().listCollections().first()).map(c -> (Document) c.get("options"));
assertThat(StringUtils.deleteWhitespace(options.map(Document::toJson).orElse(null)))
.isEqualTo(StringUtils.deleteWhitespace(CreateChangeLogCollectionStatement.OPTIONS));
assertThat(options.map(Document::toJson).orElse(""))
.isEqualToIgnoringWhitespace(CreateChangeLogCollectionStatement.OPTIONS);
}

@Test
Expand Down
Loading

0 comments on commit 1607619

Please sign in to comment.