|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.InputStream;
|
6 | 6 | import java.lang.reflect.Field;
|
| 7 | +import java.sql.Connection; |
7 | 8 | import java.sql.ResultSet;
|
| 9 | +import java.sql.SQLException; |
| 10 | +import java.sql.Statement; |
8 | 11 | import java.text.SimpleDateFormat;
|
9 | 12 | import java.util.ArrayList;
|
10 | 13 | import java.util.Arrays;
|
|
33 | 36 | import play.db.DB;
|
34 | 37 | import play.db.DBPlugin;
|
35 | 38 | import play.db.Model;
|
| 39 | +import play.exceptions.DatabaseException; |
36 | 40 | import play.exceptions.UnexpectedException;
|
37 | 41 | import play.exceptions.YAMLException;
|
38 | 42 | import play.libs.IO;
|
@@ -430,6 +434,32 @@ private static void disableForeignKeyConstraints() {
|
430 | 434 | return;
|
431 | 435 | }
|
432 | 436 |
|
| 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 | + |
433 | 463 | // Maybe Log a WARN for unsupported DB ?
|
434 | 464 | Logger.warn("Fixtures : unable to disable constraints, unsupported database : " + DBPlugin.url);
|
435 | 465 | }
|
@@ -465,6 +495,34 @@ private static void enableForeignKeyConstraints() {
|
465 | 495 | return;
|
466 | 496 | }
|
467 | 497 |
|
| 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 | + |
468 | 526 | Logger.warn("Fixtures : unable to enable constraints, unsupported database : " + DBPlugin.url);
|
469 | 527 | }
|
470 | 528 |
|
|
0 commit comments