SmartTableSync.Scaffold
0.5.4
dotnet tool install --global SmartTableSync.Scaffold --version 0.5.4
dotnet new tool-manifest
dotnet tool install --local SmartTableSync.Scaffold --version 0.5.4
#tool dotnet:?package=SmartTableSync.Scaffold&version=0.5.4
nuke :add-package SmartTableSync.Scaffold --version 0.5.4
SmartTableSync.Scaffold
A command-line tool that reads a parent table's schema and writes the SQL that
SmartTableSync needs, for a person to review and
apply. It never executes anything against a database: it only reads schema metadata and
writes .sql files.
For the whole setup, including which identity applies which script, see the setup guide.
Install
From NuGet, once published:
dotnet tool install --global SmartTableSync.Scaffold
smarttablesync-scaffold --help
From source, without installing:
dotnet run --project SmartTableSync.Scaffold -- --help
Or install your own build as a global tool:
dotnet pack SmartTableSync.Scaffold --output ./nupkg
dotnet tool install --global SmartTableSync.Scaffold --add-source ./nupkg
Commands
| Command | Writes | Apply to | Needs |
|---|---|---|---|
--infrastructure |
00_infrastructure.sql: the tracking tables CustomSyncState, SyncBatchLog, SyncTableConfig |
child, once | nothing |
<Table> [<Table> ...] |
per table: <Table>.sql when the child table doesn't exist yet, or <Table>_AddColumns.sql when the parent has columns the existing child table lacks; <Table>TableType.sql (TVP tables; drops and recreates an out-of-date type first); sp_Sync<Table>Batch.sql or sp_Sync<Table>.sql |
child | both connection strings |
--change-tracking-setup <Table> [...] [--retention-days <N>] |
<Table>_ChangeTrackingSetup.sql: enables Change Tracking on the database (default 2-day retention) and the table, and grants VIEW CHANGE TRACKING to smarttablesync-runtime |
parent, by a DBA | parent connection string |
--rowversion-setup <Table> [...] [--column <Name>] |
<Table>_RowVersionSetup.sql: adds a rowversion column (default RowVer) and its index |
parent, by a DBA | parent connection string |
Every command takes --output <dir>; the default is sql/generated/ under the current
directory.
Name a table outside dbo as schema.Table (sales.Order). Its scripts create the schema in the
child when it's missing and put the table type and procedure in it; their file names start with
the schema (sales.OrderTableType.sql, sales.sp_SyncOrderBatch.sql).
For each table, the per-table command prints the plan it derived, which is the same plan the sync engine derives at runtime:
=== Product ===
MergeStrategy=Tvp, BatchSize=1000, BulkCopyTimeoutSeconds=120, IncrementalStrategy=SqlChangeTracking
Apply the child files in this order: tables (or added columns), table types, procedures.
For an existing child table, the tool compares it with the parent, the same way Auto mode does.
New columns get the _AddColumns.sql script. Changes that could lose data (a changed type or
length, a changed primary key, a removed NOT NULL column, a column that now allows NULL in
the parent but not in the child) are printed as NEEDS A MANUAL CHANGE and left to you. A
decimal or numeric column whose precision or scale differs from the parent also gets
<Table>_AlterColumns.sql, which fixes the column and says how to copy the table again so the
values that were rounded become exact.
The Change Tracking script grants VIEW CHANGE TRACKING to smarttablesync-runtime. If your
runtime identity has another name, change it in the script before applying.
Connection strings
Set as environment variables:
| Variable | Identity | Rights it needs |
|---|---|---|
ParentIntrospectionConnectionString |
parent introspection | on the parent: VIEW DEFINITION, SELECT on the tables, VIEW DATABASE STATE |
ChildProvisioningConnectionString |
child provisioning | on the child: db_ddladmin. The tool only reads the existing child schema (tables, columns, table types) to decide what to write; it never changes anything |
With your own Microsoft Entra sign-in (through Azure CLI, Visual Studio or environment credentials):
$env:ParentIntrospectionConnectionString = "Server=tcp:<server>.database.windows.net,1433;Database=<parent>;Authentication=Active Directory Default;Encrypt=True"
$env:ChildProvisioningConnectionString = "Server=tcp:<server>.database.windows.net,1433;Database=<child>;Authentication=Active Directory Default;Encrypt=True"
With SQL authentication, use User Id=...;Password=... instead of Authentication=....
Settings that change the plan
The tool reads the same settings the sync engine does, from environment variables, so the scripts it writes match what the engine will expect. Set them the same way as for the Function:
| Variable | Effect |
|---|---|
SmartTableSync__TimestampColumns__<Table> |
the datetime column the Timestamp strategy uses (default LastUpdate) |
SmartTableSync__AllowFullRescanFor |
comma-separated large tables allowed to fall back to a full re-scan |
SmartTableSync__IncrementalStrategyOverrides__<Table> |
force SqlChangeTracking, RowVersion, Timestamp or FullRescan for a table |
If a setting can't work for a table, for example a forced strategy the table doesn't support, the tool reports the same error the engine would and writes nothing for that table.
Example
smarttablesync-scaffold --infrastructure --output out/child
smarttablesync-scaffold Product Customer Document --output out/child
smarttablesync-scaffold --change-tracking-setup Product --output out/parent
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
SmartTableSync now shows up by name in SQL Server, the quick start explains a reused suffix
instead of failing in Terraform, and there are clear ways to give feedback.
### Added
- SmartTableSync's connections show up in SQL Server as `SmartTableSync`, and the scaffold tool's
as `SmartTableSync.Scaffold` (`APP_NAME()`, `sys.dm_exec_sessions.program_name`), so a DBA can
see which sessions are the sync. An `Application Name` in the connection string still wins.
See [connection strings](docs/configuration.md#connection-strings).
- Issue forms for bugs and feature requests, links to Discussions for questions and for sharing a
setup, a [contributing guide](CONTRIBUTING.md), and a feedback section in the README and on the
NuGet package page.
### Changed
- The quick start stops before Terraform, with a clear message, when the suffix's resource group
already exists but another folder or machine created it. Before, Terraform failed at apply with
"a resource with the ID ... already exists". The docs say that `acme1` is only an example.
- A connection string that can't be read now stops the sync at startup with a message naming the
setting (for example *"ChildDbConnectionString isn't a valid connection string"*), instead of at
the first connection.
### Upgrading
Nothing to change. If you filter SQL Server sessions or audit logs by program name, they now show
`SmartTableSync` instead of SqlClient's default; set `Application Name` in the connection strings
to keep a name of your own.
### Tested
- Unit and integration tests (303 and 74) pass, including new ones that check `APP_NAME()` and
`sys.dm_exec_sessions.program_name` on a real SQL Server, with and without an `Application
Name` in the connection string.
- The quick start's suffix check on Azure: it stops for a resource group that exists but isn't in
the folder's Terraform state, and continues for a new suffix, a deleted one, and the folder's own
deployment.
Full changelog: https://github.com/Sidorus/SmartTableSync/blob/main/CHANGELOG.md