package ca.uhn.fhir.jpa.migrate.taskdef;

import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.junit.Test;

import java.sql.SQLException;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;

public class DropColumnTest extends BaseTest {

	public DropColumnTest(Supplier<TestDatabaseDetails> theTestDatabaseDetails) {
		super(theTestDatabaseDetails);
	}

	@Test
	public void testDropColumn() throws SQLException {
		executeSql("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");

		DropColumnTask task = new DropColumnTask("1",  "1");
		task.setTableName("SOMETABLE");
		task.setColumnName("TEXTCOL");
		getMigrator().addTask(task);

		getMigrator().migrate();

		assertThat(JdbcUtils.getColumnNames(getConnectionProperties(), "SOMETABLE"), containsInAnyOrder("PID"));

		// Do it again to make sure there is no error
		getMigrator().migrate();
		getMigrator().migrate();

	}

	@Test
	public void testDropForeignKeyColumn() throws SQLException {
		executeSql("create table PARENT (PID bigint not null, TEXTCOL varchar(255), primary key (PID))");
		executeSql("create table CHILD (PID bigint not null, PARENTREF bigint)");
		executeSql("alter table CHILD add constraint FK_MOM foreign key (PARENTREF) references PARENT(PID)");

		assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "PARENT", "CHILD"), hasSize(1));

		assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), "PARENTREF", "CHILD"), containsInAnyOrder("FK_MOM"));

		DropColumnTask task = new DropColumnTask("1",  "1");
		task.setTableName("CHILD");
		task.setColumnName("PARENTREF");
		getMigrator().addTask(task);

		getMigrator().migrate();

		assertThat(JdbcUtils.getColumnNames(getConnectionProperties(), "CHILD"), containsInAnyOrder("PID"));

		assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "PARENT", "CHILD"), empty());

		// Do it again to make sure there is no error
		getMigrator().migrate();
		getMigrator().migrate();

	}

}

@@ -1226,24 +1226,125 @@
 y (PID))%22);%0A
+%09%09executeSql(%22create table SIBLING (PID bigint not null, TEXTCOL varchar(255), primary key (PID))%22);%0A
 %09%09executeSql
@@ -1396,16 +1396,35 @@
 RENTREF 
+bigint, SIBLINGREF 
 bigint)%22
@@ -1534,231 +1534,573 @@
 %22);%0A
-%0A%09%09assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), %22PARENT%22, %22CHILD%22), hasSize(1));%0A%0A%09%09assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), %22PARENTREF%22, %22CHILD%22), containsInAnyOrder(%22FK_MOM
+%09%09executeSql(%22alter table CHILD add constraint FK_BROTHER foreign key (SIBLINGREF) references SIBLING(PID)%22);%0A%0A%09%09assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), %22PARENT%22, %22CHILD%22), hasSize(1));%0A%09%09assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), %22SIBLING%22, %22CHILD%22), hasSize(1));%0A%0A%09%09assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), %22PARENTREF%22, %22CHILD%22), containsInAnyOrder(%22FK_MOM%22));%0A%09%09assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), %22SIBLINGREF%22, %22CHILD%22), containsInAnyOrder(%22FK_BROTHER
 %22));
@@ -2376,24 +2376,38 @@
 yOrder(%22PID%22
+, %22SIBLINGREF%22
 ));%0A%0A%09%09asser
@@ -2493,16 +2493,356 @@
 mpty());
+%0A%09%09assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), %22SIBLING%22, %22CHILD%22), hasSize(1));%0A%0A%09%09assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), %22PARENTREF%22, %22CHILD%22), empty());%0A%09%09assertThat(JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), %22SIBLINGREF%22, %22CHILD%22), containsInAnyOrder(%22FK_BROTHER%22));
 %0A%0A%09%09// D

Enhance added JUnit test.