Skip to content

Commit dc4786a

Browse files
authored
feat: Better summary of schema changes (#21169)
#### Summary Similar to #21165. Updated the contributing docs with a docker compose stack to make it easier to test the plugin.
1 parent 16054d8 commit dc4786a

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

plugins/destination/mssql/CONTRIBUTING.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,7 @@ Tests require an SQL Server instance available.
1515
You can run the following script to start the local server & create the required database:
1616

1717
```bash
18-
docker run \
19-
--platform=linux/amd64 \
20-
-e ACCEPT_EULA=Y \
21-
-e MSSQL_PID=Express \
22-
-e MSSQL_SA_PASSWORD='yourStrongP@ssword' \
23-
-e DB_USER=SA \
24-
-e DB_NAME=cloudquery \
25-
-p 1433:1433 \
26-
-d mcr.microsoft.com/mssql/server:2017-latest
27-
```
28-
29-
After that, ensure that the database is created:
30-
31-
```bash
32-
docker exec $(docker ps -alq) \
33-
/opt/mssql-tools/bin/sqlcmd \
34-
-U "SA" \
35-
-P 'yourStrongP@ssword' \
36-
-Q "CREATE DATABASE cloudquery;"
18+
docker compose up --wait
3719
```
3820

3921
Then you can run tests:
@@ -46,4 +28,10 @@ make test
4628

4729
```bash
4830
make lint
31+
```
32+
33+
To clean up:
34+
35+
```bash
36+
docker compose down --volumes
4937
```

plugins/destination/mssql/client/changes.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,11 @@ package client
22

33
import (
44
"slices"
5-
"strings"
65

76
"github.com/apache/arrow-go/v18/arrow"
87
"github.com/cloudquery/plugin-sdk/v4/schema"
98
)
109

11-
func prettifyChanges(name string, changes []schema.TableColumnChange) string {
12-
builder := new(strings.Builder)
13-
builder.WriteString(name + ":")
14-
for _, change := range changes {
15-
builder.WriteString("\n")
16-
builder.WriteString(change.String())
17-
}
18-
return builder.String()
19-
}
20-
2110
func unsafeChanges(changes []schema.TableColumnChange) []schema.TableColumnChange {
2211
unsafe := make([]schema.TableColumnChange, 0, len(changes))
2312
for _, c := range changes {

plugins/destination/mssql/client/migrate.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package client
33
import (
44
"context"
55
"database/sql"
6-
"errors"
6+
"fmt"
77
"strings"
88

99
"github.com/cloudquery/cloudquery/plugins/destination/mssql/v5/queries"
@@ -20,7 +20,7 @@ func (c *Client) MigrateTables(ctx context.Context, messages message.WriteMigrat
2020

2121
want := normalizedTables(messages)
2222

23-
if err := c.checkForced(have, want, messages); err != nil {
23+
if err := checkForced(have, want, messages); err != nil {
2424
return err
2525
}
2626

@@ -49,8 +49,8 @@ func (c *Client) MigrateTables(ctx context.Context, messages message.WriteMigrat
4949
return nil
5050
}
5151

52-
func (c *Client) checkForced(have, want schema.Tables, messages message.WriteMigrateTables) error {
53-
forcedErr := false
52+
func checkForced(have, want schema.Tables, messages message.WriteMigrateTables) error {
53+
nonAutoMigratableTables := make(map[string][]schema.TableColumnChange)
5454
for _, m := range messages {
5555
if m.MigrateForce {
5656
continue
@@ -63,16 +63,12 @@ func (c *Client) checkForced(have, want schema.Tables, messages message.WriteMig
6363
}
6464
want := want.Get(m.Table.Name) // and it should never be nil
6565
if unsafe := unsafeChanges(want.GetChanges(have)); len(unsafe) > 0 {
66-
c.logger.Error().
67-
Str("table", m.Table.Name).
68-
Str("changes", prettifyChanges(m.Table.Name, unsafe)).
69-
Msg("migrate manually or consider using 'migrate_mode: forced'")
70-
forcedErr = true
66+
nonAutoMigratableTables[m.Table.Name] = unsafe
7167
}
7268
}
7369

74-
if forcedErr {
75-
return errors.New("migrate manually or consider using 'migrate_mode: forced'")
70+
if len(nonAutoMigratableTables) > 0 {
71+
return fmt.Errorf("\nCan't migrate tables automatically, migrate manually or consider using 'migrate_mode: forced'. Non auto migratable tables changes:\n\n%s", schema.GetChangesSummary(nonAutoMigratableTables))
7672
}
7773
return nil
7874
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
services:
2+
mssql:
3+
image: mcr.microsoft.com/mssql/server:2019-latest
4+
platform: linux/amd64
5+
environment:
6+
ACCEPT_EULA: Y
7+
MSSQL_PID: Express
8+
MSSQL_SA_PASSWORD: yourStrongP@ssword
9+
DB_USER: SA
10+
DB_NAME: cloudquery
11+
ports:
12+
- 1433:1433
13+
healthcheck:
14+
test: ["CMD", "/opt/mssql-tools18/bin/sqlcmd", "-C", "-U", "SA", "-P", "yourStrongP@ssword", "-Q", "SELECT 1"]
15+
interval: 15s
16+
timeout: 30s
17+
retries: 10
18+
init:
19+
image: mcr.microsoft.com/mssql/server:2019-latest
20+
platform: linux/amd64
21+
command: /opt/mssql-tools18/bin/sqlcmd -C -S tcp:mssql,1433 -U SA -P yourStrongP@ssword -Q "CREATE DATABASE cloudquery;"
22+
depends_on:
23+
mssql:
24+
condition: service_healthy

0 commit comments

Comments
 (0)