Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

SqlPackage Publish works, Import does not #64

Closed
lukos opened this issue May 19, 2021 · 3 comments
Closed

SqlPackage Publish works, Import does not #64

lukos opened this issue May 19, 2021 · 3 comments

Comments

@lukos
Copy link

lukos commented May 19, 2021

I have an existing Linux-based dockerfile, which publishes some dacpacs to a local database and this works fine.

However, I have now changed it because I want to import a bacpac including data. I have changed the sqlpackage command only to specify /a:Import and it points to a bacpac instead of a dacpac but it doesn't work, I see this error logged:

Importing to database 'SPC_Test' on server 'localhost'.
Creating deployment plan
Initializing deployment
*** Error importing database:Could not import package.
Unable to connect to target server 'localhost'. Please verify the connection information such as the server name, login credentials, and firewall rules for the target server.
Cannot open database "SPC_Test" requested by the login. The login failed.
Login failed for user 'sa'.

My understanding from msdn is that if the database does not exist, it will be created but the error seems to suggest this is not the case.

If this is the case, the error message should be more specific "Cannot import into new database, create the database first". If this is not the case, then I am stuck as to what I have done wrong. The relevant excerp from the dockerfile is below:

ENV ACCEPT_EULA="Y"
ENV SA_PASSWORD="P@ssW0rd!"

# Install Unzip
# Install SQLPackage for Linux and make it executable
# Get bacpac/dacpac from build
COPY Database/bin/Release/*.bacpac /tmp/

# See https://stackoverflow.com/a/51589787/488695 about waiting for sql to start
RUN ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" \
    && /opt/sqlpackage/sqlpackage /a:Import /tsn:localhost /tdn:SPC_Test /tu:sa /tp:$SA_PASSWORD /sf:/tmp/FuncTest.bacpac \
	&& rm /tmp/*.bacpac && pkill sqlservr
@ErikEJ
Copy link

ErikEJ commented May 19, 2021

Have you tried creating the database in advance?

@lukos
Copy link
Author

lukos commented May 19, 2021

It took me a while but after creating the database first, it seems to work OK. I guess I should raise an issue against the docs in that case!

@lukos lukos closed this as completed May 19, 2021
@dzsquared
Copy link
Contributor

@lukos I see that you closed this - but I noticed that you're using sa to run sqlpackage here so the database would be created by the import action.

My guess is that the engine hasn't finished starting up (despite the stackoverflow method).

For what it's worth, my preferred method of checking that the engine is ready to run queries is using sqlcmd to run something like "select * from sys.databases" in a limited loop until it responds. I've been doing this in the past prior to publishing dacpacs and just tested it with importing a bacpac without an issue.
https://github.com/dzsquared/sql-development-tools/blob/main/.devcontainer/mssql/postCreateCommand.sh

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants