Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public static void dropTables(Connection connection) throws SQLException {
}
}

public static int truncateTable(Connection connection) throws SQLException {
int numOfTruncatedTables = 0;
try (Statement st = connection.createStatement()) {
for (String tableName : SqlDmlDdl.ALL_CREATES.keySet()) {
if (tableExists(connection, tableName)) {
st.executeUpdate("TRUNCATE TABLE " + tableName);
numOfTruncatedTables++;
}
}
connection.commit();
}
return numOfTruncatedTables;
}

private static boolean tableExists(Connection connection, String tableName) throws SQLException {
DatabaseMetaData meta = connection.getMetaData();
if (meta.storesUpperCaseIdentifiers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,24 @@ void dropTables() {
.as("dropping tables again should not throw")
.doesNotThrowAnyException();
}

@Test
@Order(2)
void truncateTables() {
assertThatCode(
() -> {
try (Connection conn = dataSource.getConnection()) {
JdbcHelper.truncateTable(conn);
}
})
.doesNotThrowAnyException();
assertThatCode(
() -> {
try (Connection conn = dataSource.getConnection()) {
JdbcHelper.truncateTable(conn);
}
})
.as("truncating tables again should not throw")
.doesNotThrowAnyException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.projectnessie.gc.tool.cli.commands.DeleteLiveSets;
import org.projectnessie.gc.tool.cli.commands.JdbcCreateSchema;
import org.projectnessie.gc.tool.cli.commands.JdbcDumpSchema;
import org.projectnessie.gc.tool.cli.commands.JdbcTruncateTables;
import org.projectnessie.gc.tool.cli.commands.ListDeferredDeletions;
import org.projectnessie.gc.tool.cli.commands.ListLiveSets;
import org.projectnessie.gc.tool.cli.commands.MarkAndSweep;
Expand Down Expand Up @@ -57,7 +58,8 @@
JdbcDumpSchema.class,
JdbcCreateSchema.class,
CompletionScript.class,
ThirdPartyLicenses.class
ThirdPartyLicenses.class,
JdbcTruncateTables.class
})
public class CLI {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.gc.tool.cli.commands;

import static java.lang.System.out;

import java.sql.Connection;
import javax.sql.DataSource;
import org.projectnessie.gc.contents.jdbc.JdbcHelper;
import org.projectnessie.gc.tool.cli.Closeables;
import org.projectnessie.gc.tool.cli.options.EnvironmentDefaultProvider;
import org.projectnessie.gc.tool.cli.options.JdbcOptions;
import picocli.CommandLine;

@CommandLine.Command(
name = "truncate",
aliases = {"clean-tables"},
mixinStandardHelpOptions = true,
defaultValueProvider = EnvironmentDefaultProvider.class,
description =
"Truncate nessie-gc tables to avoid increasing storage of DB, "
+ "must not be used with the in-memory contents-storage.")
public class JdbcTruncateTables extends BaseCommand {
@CommandLine.ArgGroup(multiplicity = "1", exclusive = false)
JdbcOptions jdbc;

@Override
protected Integer call(Closeables closeables) throws Exception {
DataSource dataSource = closeables.maybeAdd(jdbc.createDataSource());
int truncateTableCount;
try (Connection conn = dataSource.getConnection()) {
truncateTableCount = JdbcHelper.truncateTable(conn);
}
out.printf("number of `%s` tables truncated", truncateTableCount);
return truncateTableCount > 0 ? 0 : 1;
}
}
10 changes: 10 additions & 0 deletions gc/gc-tool/src/test/java/org/projectnessie/gc/tool/TestCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ static Stream<Arguments> optionErrors() {
arguments(
asList("list-deferred", "--live-set-id=00000000-0000-0000-0000-000000000000"),
"Error: Missing required argument (specify one of these): ([--inmemory] | [[--jdbc]"),
arguments(
singletonList("truncate"),
"Error: Missing required argument(s): ([--jdbc] --jdbc-url=<url>"),
// No live-set-id
arguments(
asList("sweep", "--jdbc-url", "jdbc:foo//bar"),
Expand Down Expand Up @@ -470,4 +473,11 @@ public void completionScript(@TempDir Path dir) throws Exception {
soft.assertThat(run.getExitCode()).as(run::getErr).isEqualTo(1);
soft.assertThat(run.getErr()).contains("File already exists.");
}

@Test
@Order(10)
public void truncateTables() throws Exception {
RunCLI run = RunCLI.run("truncate", "--jdbc-url", JDBC_URL);
soft.assertThat(run.getExitCode()).as(run::getErr).isEqualTo(0);
}
}