package com.github.arteam.jdit.maintenance;

import org.skife.jdbi.v2.*;

/**
 * Date: 1/1/16
 * Time: 7:05 PM
 * <p/>
 * Maintenance operations against a PostgreSQL database
 *
 * @author Artem Prigoda
 */
class PostgresDatabaseMaintenance implements DatabaseMaintenance {

    private Handle handle;

    public PostgresDatabaseMaintenance(Handle handle) {
        this.handle = handle;
    }

    public void sweepData() {
        handle.useTransaction(new TransactionConsumer() {
            @Override
            public void useTransaction(Handle h, TransactionStatus transactionStatus) throws Exception {
                Batch batch = h.createBatch();
                Query<String> tableForeignKeys = h.createQuery(
                        "select 'alter table \"' || relname || '\" drop constraint \"'|| conname ||'\"' " +
                        "from pg_constraint " +
                        "inner join pg_class on conrelid=pg_class.oid " +
                        "inner join pg_namespace on pg_namespace.oid=pg_class.relnamespace " +
                        "where pg_constraint.contype = 'f' " +
                        "order by nspname, relname, conname")
                        .mapTo(String.class);
                for (String alterTable : tableForeignKeys) {
                    batch.add(alterTable);
                }
                Query<String> tableNames = h.createQuery("select tablename from pg_tables " +
                        "where tableowner = (select current_user) " +
                        "and schemaname = 'public'")
                        .mapTo(String.class);
                for (String tableName : tableNames) {
                    batch.add(String.format("delete from \"%s\"", tableName));
                }
                Query<String> sequenceNames = h.createQuery("select sequence_name from information_schema.sequences " +
                        "where sequence_schema='public' " +
                        "and sequence_catalog = (select current_catalog)")
                        .mapTo(String.class);
                for (String sequenceName : sequenceNames) {
                    batch.add(String.format("alter sequence \"%s\" restart with 1", sequenceName));
                }
                batch.execute();
            }
        });
    }

    public void dropTablesAndSequences() {
        handle.useTransaction(new TransactionConsumer() {
            @Override
            public void useTransaction(Handle h, TransactionStatus transactionStatus) throws Exception {
                String currentUser = h.createQuery("select current_user")
                        .mapTo(String.class)
                        .first();
                h.execute(String.format("drop owned by \"%s\"", currentUser));
            }
        });
    }

    private static class TableForeignKey {
        private String table;
        private String foreignKey;

        private TableForeignKey(String table) {
            this.table = table;
        }
    }
}

@@ -2787,213 +2787,7 @@
    %7D
-%0A%0A    private static class TableForeignKey %7B%0A        private String table;%0A        private String foreignKey;%0A%0A        private TableForeignKey(String table) %7B%0A            this.table = table;%0A        %7D%0A    %7D
 %0A%7D%0A

Remove unused class TableForeignKey