Skip to main content
edited tags
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
Source Link
gstackoverflow
  • 593
  • 1
  • 4
  • 16

Implement simplified liquibase

I attended job interview and on live codeing session was asked to implement code based on some requirements. There are quite long text description but idea that we have to accept some file system path and run some files if they satisfy some name pattern and depends on pattern SqlRunner should call different methos. I am free to create interfaces or can use any existing java API.

I've created following code:

class MigrationProcessor {
    private static final String[] STOP_FOLDER_NAMES = new String[]{"tmp", "system"};

    private SqlRunner sqlRunner;
    private FileSystemReader fileSystemReader;

    public void process(Path path) {
        for (FileSystemSObject fileSystemSObject : fileSystemReader.getAllObjects(path)) {
            if (fileSystemSObject.isFile()) {
                processFile(fileSystemSObject);
            } else if (fileSystemSObject.isFolder()) {
                processFolder(fileSystemSObject);
            }
        }
    }

    private void processFolder(FileSystemSObject fileSystemSObject) {
        if (Arrays.asList(STOP_FOLDER_NAMES).contains(fileSystemSObject.getName())) {
            System.out.println("skipped processing of folder " + fileSystemSObject.getName());
        } else {
            process(fileSystemSObject.getPath());
        }
    }

    private void processFile(FileSystemSObject fileSystemSObject) {
        String fileName = fileSystemSObject.getName();
        ScriptType scriptType = ScriptType.getTypeByName(fileName);
        if (scriptType == null) {
            System.out.println("Ignore file " + fileSystemSObject.getName());
        } else {
            switch (scriptType) {
                case SQL:
                    sqlRunner.runSql(fileName);
                    break;
                case CREATE_INDEX:
                    sqlRunner.createIndex(fileName);
                    break;
                case RUN_STORED_PROCEDURE:
                    sqlRunner.runStoredProcedure(fileName);
                    break;
            }
        }
    }
}

interface SqlRunner {
    void runSql(String fileName);

    void createIndex(String fileName);

    void runStoredProcedure(String fileName);

}

interface FileSystemReader {
    Iterable<FileSystemSObject> getAllObjects(Path path);
}

interface FileSystemSObject {
    boolean isFolder();

    boolean isFile();

    String getName();

    Path getPath();
}

enum ScriptType {
    SQL,
    CREATE_INDEX,
    RUN_STORED_PROCEDURE;

    public static ScriptType getTypeByName(String name) {
        if (name.startsWith("create_index_") && name.endsWith(".sql")) {
            return CREATE_INDEX;
        } else if (name.startsWith("run_procedure_") && name.endsWith(".sql")) {
            return RUN_STORED_PROCEDURE;
        } else if (name.endsWith(".sql")) {
            return SQL;
        }
        return null;
    }
}

please review