Skip to content

Commit

Permalink
attempt to get cross catalog and schema hackily working on ms-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed May 15, 2009
1 parent 012d73a commit 976f978
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
Expand Up @@ -159,7 +159,7 @@ public void enableSyncTriggers() {
}

public String getSyncTriggersExpression() {
return "dbo.fn_sym_triggers_disabled() = 0";
return getDefaultCatalog() + ".dbo.fn_sym_triggers_disabled() = 0";
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions symmetric/src/main/resources/dialects/mssql.xml
Expand Up @@ -123,13 +123,13 @@
open DataCursor
fetch next from DataCursor into @DataRow, $(newKeyVariables)
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, create_time)
insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, create_time)
values('$(targetTableName)','I', $(triggerHistoryId), @DataRow, current_timestamp)
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id)
insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id)
select c.node_id, @@IDENTITY, '$(channelName)', $(txIdExpression)
from $(defaultSchema)$(prefixName)_node c, inserted
from $(defaultCatalog)$(defaultSchema)$(prefixName)_node c, inserted
where $(varNewPrimaryKeyJoin)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and (dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and ($(defaultCatalog)dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
fetch next from DataCursor into @DataRow, $(newKeyVariables)
end
close DataCursor
Expand Down Expand Up @@ -163,13 +163,13 @@
open DataCursor
fetch next from DataCursor into @DataRow, @OldPk, @OldDataRow, $(oldKeyVariables), $(newKeyVariables)
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, old_data, create_time)
insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, old_data, create_time)
values('$(targetTableName)','U', $(triggerHistoryId), @DataRow, @OldPk, @OldDataRow, current_timestamp)
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id)
select c.node_id, @@IDENTITY, '$(channelName)', $(txIdExpression)
from $(defaultSchema)$(prefixName)_node c, inserted, deleted
from $(defaultCatalog)$(defaultSchema)$(prefixName)_node c, inserted, deleted
where $(varOldPrimaryKeyJoin) and $(varNewPrimaryKeyJoin)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and (dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and ($(defaultCatalog)dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
fetch next from DataCursor into @DataRow, @OldPk, @OldDataRow, $(oldKeyVariables), $(newKeyVariables)
end
close DataCursor
Expand All @@ -196,13 +196,13 @@
open DataCursor
fetch next from DataCursor into @OldPk, $(oldKeyVariables)
while @@FETCH_STATUS = 0 begin
insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, create_time)
insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, create_time)
values('$(targetTableName)','D', $(triggerHistoryId), @OldPk, current_timestamp)
insert into $(defaultSchema)$(prefixName)_data_event (node_id, data_id, channel_id, transaction_id)
select c.node_id, @@IDENTITY, '$(channelName)', $(txIdExpression)
from $(defaultSchema)$(prefixName)_node c, deleted
from $(defaultCatalog)$(defaultSchema)$(prefixName)_node c, deleted
where $(varOldPrimaryKeyJoin)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and (dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
and (c.node_group_id='$(targetGroupId)' and c.sync_enabled=1 and ($(defaultCatalog)dbo.fn_sym_node_disabled() != c.node_id)) $(nodeSelectWhere)
fetch next from DataCursor into @OldPk, $(oldKeyVariables)
end
close DataCursor
Expand Down
Expand Up @@ -88,4 +88,66 @@ protected void testCrossCatalogSyncOnMySQL(boolean schema, boolean catalog) {
Assert.assertEquals("The data event from the other database's other_table was not captured.", jdbcTemplate
.queryForInt("select count(*) from sym_data_event where channel_id='other'"), 1);
}

@Ignore
@Test
@ParameterMatcher("mssql")
public void testCrossCatalogSyncOnMsSql() {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
try {
jdbcTemplate.update("drop database other");
} catch (Exception e) { }
jdbcTemplate.update("create database other");
String db = (String) jdbcTemplate.queryForObject("select db_name()", String.class);
jdbcTemplate.update("use other");
jdbcTemplate.update("create table other_table (id char(5) not null, name varchar(40), primary key(id))");
jdbcTemplate.update("use " + db);
IConfigurationService configService = find(Constants.CONFIG_SERVICE);
Trigger trigger = new Trigger();
trigger.setChannelId("other");
trigger.setSourceGroupId(TestConstants.TEST_CONTINUOUS_NODE_GROUP);
trigger.setTargetGroupId(TestConstants.TEST_CONTINUOUS_NODE_GROUP);
trigger.setSourceCatalogName("other");
trigger.setSourceSchemaName("dbo");
trigger.setSourceTableName("other_table");
trigger.setSyncOnInsert(true);
trigger.setSyncOnUpdate(true);
trigger.setSyncOnDelete(true);
configService.insert(trigger);
getSymmetricEngine().syncTriggers();
jdbcTemplate.update("insert into other.dbo.other_table values('00000','first row')");
Assert.assertEquals("The data event from the other database's other_table was not captured.", 1, jdbcTemplate
.queryForInt("select count(*) from sym_data_event where channel_id='other'"));
}

@Ignore
@Test
@ParameterMatcher("mssql")
public void testCrossSchemaSyncOnMsSql() {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
try {
jdbcTemplate.update("drop table other.other_table2");
} catch (Exception e) { }
try {
jdbcTemplate.update("drop schema other");
} catch (Exception e) { }
jdbcTemplate.update("create schema other");
jdbcTemplate.update("create table other.other_table2 (id char(5) not null, name varchar(40), primary key(id))");
IConfigurationService configService = find(Constants.CONFIG_SERVICE);
Trigger trigger = new Trigger();
trigger.setChannelId("other");
trigger.setSourceGroupId(TestConstants.TEST_CONTINUOUS_NODE_GROUP);
trigger.setTargetGroupId(TestConstants.TEST_CONTINUOUS_NODE_GROUP);
trigger.setSourceSchemaName("other");
trigger.setSourceTableName("other_table2");
trigger.setSyncOnInsert(true);
trigger.setSyncOnUpdate(true);
trigger.setSyncOnDelete(true);
configService.insert(trigger);
getSymmetricEngine().syncTriggers();
jdbcTemplate.update("insert into other.other_table2 values('00000','first row')");
Assert.assertEquals("The data event from the other database's other_table was not captured.", 1, jdbcTemplate
.queryForInt("select count(*) from sym_data where table_name='other_table2'"));
}

}

0 comments on commit 976f978

Please sign in to comment.