Skip to content

Commit

Permalink
cmd/atlas: fixed pingURL for sqlserver (#2337)
Browse files Browse the repository at this point in the history
* doc: update SQL server DevURL to docker

* cmd/atlas: fixed pingURL for sqlserver
  • Loading branch information
giautm authored Dec 5, 2023
1 parent f0c7b30 commit a38c429
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
18 changes: 16 additions & 2 deletions cmd/atlas/internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ func (c *Container) Wait(ctx context.Context, timeout time.Duration) error {
if err != nil {
return err
}
pingURL := c.PingURL(*u)
for {
select {
case <-time.After(100 * time.Millisecond):
// Ping against the root connection.
u.Path = "/"
client, err := sqlclient.Open(ctx, u.String())
client, err := sqlclient.Open(ctx, pingURL)
if err != nil {
continue
}
Expand Down Expand Up @@ -396,6 +396,20 @@ func (c *Container) URL() (*url.URL, error) {
}
}

// PingURL returns a URL to ping the Container.
func (c *Container) PingURL(u url.URL) string {
switch c.cfg.driver {
case DriverSQLServer:
q := u.Query()
q.Del("database")
u.RawQuery = q.Encode()
return u.String()
default:
u.Path = "/"
return u.String()
}
}

// validate that no empty values are given.
func (c *Config) validate() error {
if c == nil || c.Image == "" || c.Port == "" || c.Out == nil {
Expand Down
7 changes: 1 addition & 6 deletions doc/md/components/migrate-push-command.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ atlas migrate push app \
</TabItem>
<TabItem value="sqlserver" label="SQL Server">

Run first the following command to start a local MSSQL server using Docker:

```bash
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=P@ssw0rd0995' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
```
```bash
atlas migrate push app \
--dev-url "sqlserver://sa:P@ssw0rd0995@localhost:1433?database=master"
--dev-url "docker://sqlserver/2022-latest"
```

</TabItem>
Expand Down
22 changes: 22 additions & 0 deletions doc/md/concepts/dev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ docker+postgres://org/image:tag
--dev-url "sqlite://dev?mode=memory"
```

</TabItem>
<TabItem value="sqlserver" label="SQLServer">

```shell
# When working on a single database schema.
--dev-url "docker://sqlserver/2022-latest/dev?mode=schema"

# When working on multiple database schemas.
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"
```

To work with an Azure SQL Edge docker image or SQLServer version, use one of the following formats:

```shell
# Run SQLServer 2017-latest in schema mode.
docker://sqlserver/2017-latest?mode=schema
docker://sqlserver/2019-latest?mode=schema
docker://sqlserver/2022-latest?mode=schema

# Run Azure SQL Edge 1.0.7 in schema mode.
docker+sqlserver://mcr.microsoft.com/azure-sql-edge:1.0.7?mode=schema
```
</TabItem>
</Tabs>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,14 @@ CREATE TABLE posts (
);
```

### Spinning up a Dev-Database

Atlas relies on a [_Dev-Database_](/concepts/dev-database), a temporary empty database to normalize schemas and make various
calculations. Because we are providing the desired state of our database in plain SQL, Atlas must somehow transform
our schema to a normalized form which requires such a database. Let's spin up an additional SQL Server instance
on port 1434:

```
docker run --rm -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=P@ssw0rd0995' -p 1434:1433 -d mcr.microsoft.com/mssql/server:latest
```

### Applying our schema

Next, let's apply this schema to our database. To do so, we will use the `atlas schema apply` command.

```shell
atlas schema apply -u "sqlserver://sa:P@ssw0rd0995@localhost:1433?database=master" \
--to file://schema.sql \
--dev-url "sqlserver://sa:P@ssw0rd0995@localhost:1434?database=master"
--dev-url "docker://sqlserver"
```
Atlas will connect to our target database to inspect it's current state. Next, it will use the dev-database to
normalize our schema and finally, it will generate the SQL commands that will bring our database to the desired state:
Expand Down

0 comments on commit a38c429

Please sign in to comment.