48 lines
2.6 KiB
Java
48 lines
2.6 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
public class DropOrgId {
|
|
public static void main(String[] args) throws Exception {
|
|
Properties props = new Properties();
|
|
try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); }
|
|
try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) {
|
|
conn.setAutoCommit(false);
|
|
// drop indexes referencing b_org_id on s_log_technical
|
|
String idxSql = "SELECT i.name FROM sys.indexes i " +
|
|
"JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id " +
|
|
"JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id " +
|
|
"WHERE i.object_id = OBJECT_ID('s_log_technical') AND c.name = 'b_org_id'";
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(idxSql)) {
|
|
while (rs.next()) {
|
|
String idx = rs.getString(1);
|
|
st.execute("DROP INDEX [" + idx + "] ON dbo.s_log_technical");
|
|
System.out.println("OK: DROP INDEX " + idx);
|
|
}
|
|
}
|
|
// drop columns
|
|
for (String tbl : new String[]{"s_log_technical", "s_log_login"}) {
|
|
try (Statement st = conn.createStatement()) {
|
|
st.execute("ALTER TABLE dbo." + tbl + " DROP COLUMN b_org_id");
|
|
System.out.println("OK: ALTER TABLE " + tbl + " DROP COLUMN b_org_id");
|
|
}
|
|
}
|
|
conn.commit();
|
|
System.out.println("COMMITTED.");
|
|
// verify
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT t.name, c.name FROM sys.tables t JOIN sys.columns c ON c.object_id = t.object_id " +
|
|
"WHERE c.name = 'b_org_id' ORDER BY t.name")) {
|
|
boolean any = false;
|
|
while (rs.next()) { any = true; System.out.println("STILL EXISTS: " + rs.getString(1) + "." + rs.getString(2)); }
|
|
if (!any) System.out.println("VERIFY: no b_org_id column remains in any table");
|
|
}
|
|
// verify views still valid
|
|
for (String v : new String[]{"v_s_log_login", "v_s_log_technical"}) {
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT TOP 1 1 FROM " + v)) {
|
|
System.out.println("VIEW OK: " + v + " (queryable)");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|