package leigh.gnusto;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

/**
 * This class holds a single specific SQL migration. The migration is associated with a schema by name, as
 * well as the version of that schema.
 *
 * @author Alex Leigh
 */
public abstract class SqlMigration {
    private final String name;
    private final int step;

    public SqlMigration(String name, int ver) {
        this.name = name;
        this.step = ver;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SqlMigration that = (SqlMigration) o;
        return step == that.step && Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, step);
    }

    public String getName() {
        return name;
    }

    public int getStep() {
        return step;
    }

    /**
     * Return an input stream representing the script for this migration to the caller. It is not necessary to buffer
     * the stream.
     */
    abstract InputStream getInputStream() throws FileNotFoundException;

    @Override
    public String toString() {
        return "SqlMigration{" +
                "name='" + name + '\'' +
                ", step=" + step +
                '}';
    }
}
