Skip to content

Commit ebae026

Browse files
unknownerwan
unknown
authored andcommitted
[#945] Adding support for MS Sql Server database in Fixtures.deleteDatabase()
1 parent 96c55df commit ebae026

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

framework/src/play/test/Fixtures.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.lang.reflect.Field;
7+
import java.sql.Connection;
78
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.sql.Statement;
811
import java.text.SimpleDateFormat;
912
import java.util.ArrayList;
1013
import java.util.Arrays;
@@ -33,6 +36,7 @@
3336
import play.db.DB;
3437
import play.db.DBPlugin;
3538
import play.db.Model;
39+
import play.exceptions.DatabaseException;
3640
import play.exceptions.UnexpectedException;
3741
import play.exceptions.YAMLException;
3842
import play.libs.IO;
@@ -430,6 +434,32 @@ private static void disableForeignKeyConstraints() {
430434
return;
431435
}
432436

437+
if (DBPlugin.url.startsWith("jdbc:sqlserver:")) {
438+
Statement exec=null;
439+
440+
try {
441+
List<String> names = new ArrayList<String>();
442+
Connection connection = DB.getConnection();
443+
444+
ResultSet rs = connection.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
445+
while (rs.next()) {
446+
String name = rs.getString("TABLE_NAME");
447+
names.add(name);
448+
}
449+
450+
// Then we disable all foreign keys
451+
exec = connection.createStatement();
452+
for (String tableName:names)
453+
exec.addBatch("ALTER TABLE " + tableName+" NOCHECK CONSTRAINT ALL");
454+
exec.executeBatch();
455+
exec.close();
456+
457+
return;
458+
} catch (SQLException ex) {
459+
throw new DatabaseException("Error while disabling foreign keys", ex);
460+
}
461+
}
462+
433463
// Maybe Log a WARN for unsupported DB ?
434464
Logger.warn("Fixtures : unable to disable constraints, unsupported database : " + DBPlugin.url);
435465
}
@@ -465,6 +495,34 @@ private static void enableForeignKeyConstraints() {
465495
return;
466496
}
467497

498+
if (DBPlugin.url.startsWith("jdbc:sqlserver:")) {
499+
Connection connect = null;
500+
Statement exec=null;
501+
try {
502+
connect = DB.getConnection();
503+
// We must first drop all foreign keys
504+
ArrayList<String> checkFKCommands=new ArrayList<String>();
505+
exec=connect.createStatement();
506+
ResultSet rs=exec.executeQuery("SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME +'] WITH CHECK CHECK CONSTRAINT [' + CONSTRAINT_NAME + ']' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'");
507+
while (rs.next())
508+
{
509+
checkFKCommands.add(rs.getString(1));
510+
}
511+
exec.close();
512+
exec=null;
513+
514+
// Now we have the drop commands, let's execute them
515+
exec=connect.createStatement();
516+
for (String sql:checkFKCommands)
517+
exec.addBatch(sql);
518+
exec.executeBatch();
519+
exec.close();
520+
} catch (SQLException ex) {
521+
throw new DatabaseException("Cannot enable foreign keys", ex);
522+
}
523+
return;
524+
}
525+
468526
Logger.warn("Fixtures : unable to enable constraints, unsupported database : " + DBPlugin.url);
469527
}
470528

0 commit comments

Comments
 (0)