Skip to content

Commit b68af2e

Browse files
committed
PR feedback
1 parent 31a3b07 commit b68af2e

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/main/java/de/rwth/idsg/steve/repository/DataImportExportRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public interface DataImportExportRepository {
3232

3333
void exportCsv(Writer writer, Table<?> table) throws IOException;
3434

35+
void beforeImport();
36+
void afterImport();
3537
void importCsv(InputStream in, Table<?> table) throws IOException;
3638
}

src/main/java/de/rwth/idsg/steve/repository/impl/DataImportExportRepositoryImpl.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.jooq.CSVFormat;
2525
import org.jooq.Cursor;
2626
import org.jooq.DSLContext;
27+
import org.jooq.LoaderError;
2728
import org.jooq.SQLDialect;
2829
import org.jooq.Table;
2930
import org.jooq.TableField;
3031
import org.jooq.impl.DSL;
3132
import org.springframework.stereotype.Repository;
33+
import org.springframework.util.CollectionUtils;
3234

3335
import java.io.IOException;
3436
import java.io.InputStream;
@@ -73,6 +75,23 @@ public void exportCsv(Writer writer, Table<?> table) throws IOException {
7375
}
7476
}
7577

78+
@Override
79+
public void beforeImport() {
80+
SQLDialect dialectFamily = ctx.configuration().dialect().family();
81+
82+
boolean isOk = dialectFamily == SQLDialect.MYSQL || dialectFamily == SQLDialect.MARIADB;
83+
if (!isOk) {
84+
throw new IllegalStateException("Unsupported dialect " + dialectFamily);
85+
}
86+
87+
ctx.execute("set foreign_key_checks=0");
88+
}
89+
90+
@Override
91+
public void afterImport() {
92+
ctx.execute("set foreign_key_checks=1");
93+
}
94+
7695
/**
7796
* https://www.jooq.org/doc/latest/manual/sql-execution/importing/importing-api/
7897
* https://www.jooq.org/doc/latest/manual/sql-execution/importing/importing-sources/importing-source-csv/
@@ -89,6 +108,13 @@ public void importCsv(InputStream in, Table<?> table) throws IOException {
89108
.fieldsCorresponding()
90109
.execute();
91110

111+
if (!CollectionUtils.isEmpty(loader.errors())) {
112+
for (LoaderError error : loader.errors()) {
113+
log.error("Exception happened", error.exception());
114+
}
115+
throw new RuntimeException("There were errors loading data into table " + table.getName());
116+
}
117+
92118
int processed = loader.processed();
93119

94120
log.info("Imported '{}' with processedRows={}, storedRows={}, errorCount={}, errorMessages={}",
@@ -141,13 +167,7 @@ private void resetAutoIncrement(Table<?> table) {
141167

142168
long nextVal = ((Number) maxId).longValue() + 1;
143169

144-
SQLDialect dialectFamily = ctx.configuration().dialect().family();
145-
146-
if (dialectFamily == SQLDialect.MYSQL || dialectFamily == SQLDialect.MARIADB) {
147-
ctx.execute("ALTER TABLE " + table.getName() + " AUTO_INCREMENT = " + nextVal);
148-
} else {
149-
log.warn("Auto increment not supported for dialect family {}", dialectFamily);
150-
}
170+
ctx.execute(DSL.sql("ALTER TABLE {0} AUTO_INCREMENT = {1}", table, DSL.val(nextVal)));
151171
}
152172

153173
}

src/main/java/de/rwth/idsg/steve/service/DataImportExportService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public void exportZip(OutputStream out, ExportType exportType) throws IOExceptio
140140
}
141141

142142
public void importZip(InputStream in) throws IOException {
143+
dataImportExportRepository.beforeImport();
144+
143145
try (ZipInputStream zipIn = new ZipInputStream(in)) {
144146
ZipEntry entry;
145147

@@ -158,12 +160,16 @@ public void close() throws IOException {
158160
};
159161

160162
dataImportExportRepository.importCsv(nonClosingStream, table);
163+
} catch (RuntimeException ex) {
164+
throw ex;
161165
} catch (Exception e) {
162-
log.error(e.getMessage(), e);
166+
throw new RuntimeException(e);
163167
}
164168
}
165169
zipIn.closeEntry();
166170
}
171+
} finally {
172+
dataImportExportRepository.afterImport();
167173
}
168174
}
169175

0 commit comments

Comments
 (0)