Skip to content

Commit

Permalink
Merge pull request #25 from Azure-Samples/1.9.5
Browse files Browse the repository at this point in the history
1.9.5
  • Loading branch information
yorek authored Jan 12, 2021
2 parents d66f006 + 1ee01bc commit bb468ba
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Run Tests
on:
push:
branches: [ master, 1.9.4 ]
branches: [ master, 1.9.5 ]
jobs:
RunTests:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ products:
- dotnet
- dotnet-core
description: "Smart, High-Speed, Bulk Copy tool to move data from one Azure SQL or SQL Server database to another"
urlFragment: "smart-bulk-copy"
urlFragment: smart-bulk-copy
---

# Smart Bulk Copy
Expand All @@ -28,7 +28,7 @@ Taxonomies for products and languages: https://review.docs.microsoft.com/new-hop

![License](https://img.shields.io/badge/license-MIT-green.svg) ![Run Tests](https://github.com/yorek/smartbulkcopy/workflows/Run%20Tests/badge.svg)

*Latest Stable Version: 1.9.4*
*Latest Stable Version: 1.9.5*

Smart, High-Speed, Bulk Copy tool to move data from one Azure SQL / SQL Server database to another. Smartly uses logical or physical partitions to maximize transfer speed using parallel copy tasks.

Expand Down
67 changes: 66 additions & 1 deletion client/SmartBulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,23 @@ public async Task<int> Copy(List<String> tablesToCopy)
else
{
_logger.Info("All tables copied correctly.");

if (_config.SyncIdentity)
{
_logger.Info("Synchronizing identity values...");
bool identitySynced = await SyncIdentity();
if (!identitySynced)
{
_logger.Warn("WARNING! Identity synchronization encountered some errors!");
result = 2;
} else
{
_logger.Info("Identity synchronization done.");
}
}
}

_logger.Info("Done in {0:#.00} secs.", (double)_stopwatch.ElapsedMilliseconds / 1000.0);
_logger.Info("Smart Bulk Copy completed in {0:#.00} secs.", (double)_stopwatch.ElapsedMilliseconds / 1000.0);
}
else
{
Expand Down Expand Up @@ -279,6 +293,7 @@ private async Task<bool> CheckResults()
var connSource = new SqlConnection(_config.SourceConnectionString);
var connDest = new SqlConnection(_config.DestinationConnectionString);
bool result = true;

string sql = @"
select
sum(row_count) as row_count
Expand Down Expand Up @@ -318,6 +333,56 @@ group by
return result;
}

private async Task<bool> SyncIdentity()
{
var connSource = new SqlConnection(_config.SourceConnectionString);
var connDest = new SqlConnection(_config.DestinationConnectionString);
bool result = true;

string sqlCheck = @"
with cte as
(
select
objectproperty([object_id], 'TableHasIdentity') as TableHasIdentity,
ident_current(schema_name([schema_id]) + '.' + object_name([object_id])) as IdentityCurrent
from
sys.[tables]
where
object_id = object_id(@tableName)
)
select
IdentityCurrent
from
cte
where
TableHasIdentity = 1
";

foreach (var t in _tablesToCopy)
{
_logger.Debug($"Executing: {sqlCheck}, @tableName = {t}");
var ic = await connSource.ExecuteScalarAsync<int?>(sqlCheck, new { @tableName = t });
if (ic.HasValue)
{
string sqlSet = $"dbcc checkident('{t}', reseed, {ic.Value})";
_logger.Debug($"Executing: {sqlSet}, @tableName = {t}");
connDest.Execute(sqlSet);
var ic2 = await connDest.ExecuteScalarAsync<int?>(sqlCheck, new { @tableName = t });
if (ic.Value == ic2.Value)
{
_logger.Info($"Identity for table {t} set to {ic.Value}");
}
else
{
_logger.Error($"Unable to sync identity value for {t} to {ic.Value}. Identity value found is {ic2.Value}.");
result = false;
}
}
}

return result;
}

private void TruncateDestinationTable(string tableName)
{
_logger.Info($"Truncating '{tableName}'...");
Expand Down
6 changes: 3 additions & 3 deletions client/SmartBulkCopy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<Authors>Davide Mauri</Authors>
<Product>Smart Bulk Copy</Product>
<PackageId>SmartBulkCopy</PackageId>
<Version>1.9.4</Version>
<AssemblyVersion>1.9.4</AssemblyVersion>
<FileVersion>1.9.4</FileVersion>
<Version>1.9.5</Version>
<AssemblyVersion>1.9.5</AssemblyVersion>
<FileVersion>1.9.5</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
<Description>Efficently copy database tables from one SQL Server/Azure SQL database to another</Description>
<Copyright>Davide Mauri</Copyright>
Expand Down
3 changes: 3 additions & 0 deletions client/SmartBulkCopyConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public int LogicalPartitions {

public int CommandTimeOut = 90 * 60; // 90 minutes

public bool SyncIdentity = false;

private LogicalPartitioningStrategy _logicalPartitioningStrategy = LogicalPartitioningStrategy.Auto;

public LogicalPartitioningStrategy LogicalPartitioningStrategy {
Expand Down Expand Up @@ -139,6 +141,7 @@ public static SmartBulkCopyConfiguration LoadFromConfigFile(string configFile, I
sbcc.BatchSize = int.Parse(config?["options:batch-size"] ?? sbcc.BatchSize.ToString());
sbcc.MaxParallelTasks = int.Parse(config?["options:tasks"] ?? sbcc.MaxParallelTasks.ToString());
sbcc.TruncateTables = bool.Parse(config?["options:truncate-tables"] ?? sbcc.TruncateTables.ToString());
sbcc.SyncIdentity = bool.Parse(config?["options:sync-identity"] ?? sbcc.SyncIdentity.ToString());
sbcc.RetryMaxAttempt = int.Parse(config?["options:retry-connection:max-attempt"] ?? sbcc.RetryMaxAttempt.ToString());
sbcc.RetryDelayIncrement = int.Parse(config?["options:retry-connection:delay-increment"] ?? sbcc.RetryDelayIncrement.ToString());

Expand Down
3 changes: 2 additions & 1 deletion client/e2e-test.config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"tables": {
"include": ["schema1.*"],
"include": ["schema1.*", "dbo.Issue17", "dbo.Issue23"],
"exclude": ["schema1.table_with_fk", "schema1.mix", "schema1.clustered_rowstore_pk"]
},
"options": {
"tasks": 8,
"logical-partitions": "auto",
"batch-size": 100000,
"truncate-tables": true,
"sync-identity": true,
"safe-check": "none",
"stop-if": {
"secondary-indexes": true,
Expand Down
1 change: 1 addition & 0 deletions client/smartbulkcopy.config.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"logical-partitions": "auto",
"batch-size": 100000,
"truncate-tables": true,
"sync-identity": false,
"safe-check": "readonly",
"stop-if": {
"secondary-indexes": true,
Expand Down
7 changes: 7 additions & 0 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ You can fine tune Smart Bulk Copy behavior with the configuration settings avail
- [Partitions](#Partitions)
- [Batch Size](#Batch%20Size)
- [Table Truncation](#Table%20Truncation)
- [Identity Sync](#Identity%20Sync)
- [Safety Checks](#Safety%20Checks)
- [Connection Resiliency](#Connection%20Resiliency)
- [Command Timeout](#Command%20Timeout)
Expand Down Expand Up @@ -43,6 +44,12 @@ If you're unsure of what value you should use, leave the suggested 100000.

Instruct Smart Bulk Copy to truncate tables on the destination before loading them. This requires that destination table doesn't have any Foreign Key constraint: [TRUNCATE TABLE - Restrictions](https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-2017#restrictions)

## Identity Synchronization

`"sync-identity": false`

Introduced in **version 1.9.5**: If set to `true`, tells Smart Bulk Copy to make sure that IDENTITY values are synchronized from the source to the target database. This should already happen normally, so by default is set to `false`. If you have manually reseeded the IDENTITY values on the source table this option will make sure the same reseed will happen also on the target tables.

## Safety Checks

`"safe-check": "readonly"`
Expand Down
4 changes: 4 additions & 0 deletions docs/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### Version History

## 1.9.5

Added support for synchronizing Identity values

## 1.9.4

Added support for setting the command timeout.
Expand Down
6 changes: 3 additions & 3 deletions tests/SmartBulkCopy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<PackageProjectUrl>https://github.com/yorek/smartbulkcopy</PackageProjectUrl>
<RepositoryUrl>https://github.com/yorek/smartbulkcopy</RepositoryUrl>
<RepositoryType>GIT</RepositoryType>
<Version>1.9.4</Version>
<AssemblyVersion>1.9.4</AssemblyVersion>
<FileVersion>1.9.4</FileVersion>
<Version>1.9.5</Version>
<AssemblyVersion>1.9.5</AssemblyVersion>
<FileVersion>1.9.5</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit bb468ba

Please sign in to comment.