SPRING JPA - массовая вставка в 2 базы данных (azure & h2) - PullRequest
0 голосов
/ 12 февраля 2019

У меня проблема с Spring Data JPA / Hibernate.Я использую 2 базы данных, SQL Azure для Dev / Prod и H2 для тестов и параметры:

  • spring.jpa.hibernate.ddl-auto = create
  • spring.datasource.initialization-mode = всегда

У меня большой data.sql с множеством ограничений.Поэтому мне нужно отключить внешние ключи.

На данный момент я использую 2 data.sql, но это создает проблемы с обслуживанием:

(пример) azure-data.sql

DECLARE @enable_constraints bit = 0

--Don't change anything below this line.
DECLARE @schema_name SYSNAME
DECLARE @table_name  SYSNAME

DECLARE table_cursor CURSOR FOR
  SELECT
    schemas.name,
    tables.name
  FROM
    sys.tables
      INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id

OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @schema_name, @table_name

DECLARE @cmd varchar(200)
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @cmd = 'ALTER TABLE ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ' '
  SET @cmd = @cmd + (CASE WHEN @enable_constraints = 1 THEN 'CHECK' ELSE 'NOCHECK' END) + ' CONSTRAINT ALL'

  PRINT @cmd
  EXEC( @cmd )

  FETCH NEXT FROM table_cursor INTO @schema_name, @table_name
END

CLOSE table_cursor
DEALLOCATE table_cursor


SET IDENTITY_INSERT Rec.dev.address ON  ;
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city, label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', 'Addresse', null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg','Addresse de facturaction',  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO  Rec.dev.address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (3, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', null,  null, null, '1 Place Brant', null, null, '67000', null, 1);

SET IDENTITY_INSERT Rec.dev.address OFF  ;
SET IDENTITY_INSERT Rec.dev.country ON  ;

INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'FR', 'France');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'GE', 'Allemagne');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (4, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'ABW', 'Aruba');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (5, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AFG', 'Afghanistan');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (6, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AGO', 'Angola');
INSERT INTO Rec.dev.country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (7, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AIA', 'Anguilla');

SET IDENTITY_INSERT Rec.dev.country OFF  ;

DECLARE @enable_constraints bit = 0

h2-data.sql

SET REFERENTIAL_INTEGRITY false;


INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city, label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', 'Addresse', null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg','Addresse de facturaction',  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO address (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, city,  label, comment, end_date, line1, line2, line3, postal_code, start_date, id_country) VALUES (3, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, 'Strasbourg', null,  null, null, '1 Place Brant', null, null, '67000', null, 1);
INSERT INTO application (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, label, owner_id, company_id) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, null, 1, 1);
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (1, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'FR', 'France');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (2, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 1, 1, 'GE', 'Allemagne');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (4, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'ABW', 'Aruba');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (5, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AFG', 'Afghanistan');
INSERT INTO country (id, creation_by, creation_date, deletion_by, deletion_date, modification_by, modification_date, comment, is_eu, is_allowed, iso_code, name) VALUES (6, 'System', '2019-02-04 14:36:20.0300000', null, null, null, null, null, 0, 0, 'AGO', 'Angola');
SET REFERENTIAL_INTEGRITY true;

Как использовать один файл для Azure SQL и H2?С SQL или другим форматом?Можно ли весной / спящему изменить таблицу ПОСЛЕ вставки?

...