Skip to content

Commit

Permalink
Merge pull request #109 from AdaptiveScale/release-1.7.2
Browse files Browse the repository at this point in the history
Release 1.7.2
  • Loading branch information
nbesimi authored Nov 29, 2022
2 parents 54b1a55 + 47ccfed commit f42679b
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Parameter | Description
Example:
```yaml
---
safeMode: false
databaseType: bigquery
tables:
- name: "profiles"
Expand Down Expand Up @@ -384,6 +385,8 @@ Parameter | Description
-h, --help | Show the help message and exit.
-c, --config CONFIG_FILE | YAML config file. If none is supplied it will use main.conf in the current directory if it exists.
-s, --source CONNECTION_NAME | The source connection is used to specify which models and connection to use.
-m, --model MODEL_FILE (Optional) | The model file to use for apply. Default is `model.yaml`


Example:
```
Expand Down Expand Up @@ -412,6 +415,7 @@ Parameter | Description
Example:
```yaml
---
safeMode: false
databaseType: "mysql"
tables:
- name: "actor"
Expand Down Expand Up @@ -469,12 +473,15 @@ Parameter | Description
-h, --help | Show the help message and exit.
-c, --config CONFIG_FILE | YAML config file. If none is supplied it will use main.conf in the current directory if it exists.
-s, --source CONNECTION_NAME | The source connection is used to specify which models and connection to use.
-m, --model MODEL_FILE (Optional) | The model file to use for apply. Default is `model.yaml`


Example:

(Actual database)
```yaml
---
safeMode: false
databaseType: "mysql"
tables:
- name: "actor"
Expand All @@ -500,6 +507,7 @@ tables:
(Expected database)
```yaml
---
safeMode: false
databaseType: "mysql"
tables:
- name: "actor"
Expand Down Expand Up @@ -539,6 +547,11 @@ tables:

Description: Our actual database does not contain `first_name` so we expect it to alter the table and add the column, inside the source directory there will be the executed DDL and a snapshot of the current database.


### Safety Operation
In `model.yaml` you can find the attribute `safeMode` which is by default disabled (false). If you want to prevent any DROP operation during
`apply` command, set `safeMode: true`.

## Copyright and License Information
Unless otherwise specified, all content, including all source code files and documentation files in this repository are:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

allprojects {
group = 'com.adaptivescale'
version = '1.7.1'
version = '1.7.2'
sourceCompatibility = 11
targetCompatibility = 11
}
Expand Down
4 changes: 2 additions & 2 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
implementation project(':test')

implementation group: 'info.picocli', name: 'picocli', version: '4.6.3'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.5'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3'
Expand Down Expand Up @@ -54,4 +54,4 @@ jar {
it.isDirectory() ? it : zipTree(it)
}
}
}
}
38 changes: 31 additions & 7 deletions cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@
@Slf4j
@CommandLine.Command(name = "cli",
mixinStandardHelpOptions = true,
version = "1.7.1",
version = "1.7.2",
description = "Declarative Database Management - DDL Transpiler"
)
class Cli implements Callable<Void> {

public static final String DEFAULT_MODEL_YAML = "model.yaml";

@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

Expand All @@ -82,7 +85,7 @@ private void extract(@CommandLine.Option(names = {"-s", "--source"}, required =
Files.createDirectory(sourceWorkspace);

Database result = SourceGeneratorFactory.sourceGenerator(source).generate(source);
YamlModelOutput yamlInputModel = new YamlModelOutput("model.yaml", sourceWorkspace);
YamlModelOutput yamlInputModel = new YamlModelOutput(DEFAULT_MODEL_YAML, sourceWorkspace);
yamlInputModel.write(result);
log.info("Successfully written input database yaml ({}).", yamlInputModel.getFilePath());

Expand Down Expand Up @@ -155,7 +158,8 @@ private void compile(@CommandLine.Option(names = {"-s", "--source"}) String sour

@CommandLine.Command(name = "apply", description = "Get current model and compare with state of database," +
" generate ddl for changes and apply to database. ", mixinStandardHelpOptions = true)
private void apply(@CommandLine.Option(names = {"-s", "--source"}, required = true) String sourceName) throws Exception {
private void apply(@CommandLine.Option(names = {"-s", "--source"}, required = true) String sourceName,
@CommandLine.Option(names = {"-m", "--model"}, defaultValue = DEFAULT_MODEL_YAML) String model) throws Exception {
requireConfig(config);

Connection source = getSourceConnection(sourceName);
Expand All @@ -166,7 +170,7 @@ private void apply(@CommandLine.Option(names = {"-s", "--source"}, required = tr
" models for translation", sourceWorkspace, sourceName));
}

List<Database> databases = getDatabases(sourceWorkspace)
List<Database> databases = getDatabaseForModel(sourceWorkspace, model)
.map(AbstractMap.SimpleImmutableEntry::getValue)
.collect(Collectors.toList());

Expand All @@ -186,6 +190,12 @@ private void apply(@CommandLine.Option(names = {"-s", "--source"}, required = tr
return;
}

if (changes.stream().filter(change -> change.getStatus().equals(Change.Status.DROP)).findFirst().isPresent() &&
expectedDatabase.getSafeMode()) {
log.info("Not going to perform the changes because there are DROP operations and the safe mode is enabled.");
return;
}

ChangeHandler handler = DDLFactory.changeHandler(source.getDbType());
String ddl = handler.createDDLForChanges(changes);

Expand Down Expand Up @@ -283,7 +293,7 @@ private void extractDbtModels(Connection connection, Path sourceWorkspace) throw
List<Database> databases = getDatabases(sourceWorkspace).map(AbstractMap.SimpleImmutableEntry::getValue).collect(Collectors.toList());

DbtModel dbtModel = DbtModelGenerator.dbtModelGenerator(connection, databases);
DbtYamlModelOutput dbtYamlModelOutput = new DbtYamlModelOutput("model.yaml", dbtWorkspace);
DbtYamlModelOutput dbtYamlModelOutput = new DbtYamlModelOutput(DEFAULT_MODEL_YAML, dbtWorkspace);
dbtYamlModelOutput.write(dbtModel);

Map<String, String> dbtSQLTables = DbtModelGenerator.dbtSQLGenerator(dbtModel);
Expand All @@ -294,7 +304,8 @@ private void extractDbtModels(Connection connection, Path sourceWorkspace) throw
}

@CommandLine.Command(name = "diff", description = "Show difference between local model and database", mixinStandardHelpOptions = true)
private void diff(@CommandLine.Option(names = {"-s", "--source"}) String sourceName) throws Exception {
private void diff(@CommandLine.Option(names = {"-s", "--source"}) String sourceName,
@CommandLine.Option(names = {"-m", "--model"}, defaultValue=DEFAULT_MODEL_YAML) String model) throws Exception {
requireConfig(config);
Connection sourceConnection = getSourceConnection(sourceName);

Expand All @@ -304,7 +315,7 @@ private void diff(@CommandLine.Option(names = {"-s", "--source"}) String sourceN
" models for translation", sourceWorkspace, sourceName));
}

List<Database> databases = getDatabases(sourceWorkspace)
List<Database> databases = getDatabaseForModel(sourceWorkspace, model)
.map(AbstractMap.SimpleImmutableEntry::getValue)
.collect(Collectors.toList());

Expand Down Expand Up @@ -404,6 +415,19 @@ private Stream<FileNameAndDatabasePair> getDatabases(Path directory) throws IOEx
});
}

private Stream<FileNameAndDatabasePair> getDatabaseForModel(Path directory, String model) throws IOException {
return Files.list(directory)
.filter(path -> FilenameUtils.getName(path.toString()).equals(model) && !Files.isDirectory(path))
.map(path -> {
try {
Database input = new ObjectMapper(new YAMLFactory()).readValue(path.toFile(), Database.class);
return new FileNameAndDatabasePair(path.getFileName().toString(), input);
} catch (Exception exception) {
throw new RuntimeException(exception);
}
});
}

private Function<FileNameAndDatabasePair, FileNameAndDatabasePair> translateDatabases(Translator<Database, Database> translator) {
return fileNameAndModelPair -> {
try {
Expand Down
Loading

0 comments on commit f42679b

Please sign in to comment.