Skip to content

Conversation

@carvido1
Copy link
Contributor

@carvido1 carvido1 commented Nov 9, 2022

This PR adds capabilities to import bacpac snapshots using the mssqlimport service

@mpern
Copy link
Member

mpern commented Nov 10, 2022

Hi Carlos,
Thank You for contributing!

- SA_PASSWORD=localSAPassw0rd
depends_on:
- mssql
mssqlimport:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please combine / merge mssqlimport into the exisitng mssqlinit container.

There should be one container and script that either imports the bacpac (if present) or bootstraps a fresh DB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mpern

I can merge both mssqlimport and the mssqlinit into the same script. As a bacpac file is not a backup but a snapshot we need an already created database to import the content of the snapshot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right!
I should have looked at your script more carefully.

In that case:
Always create db
Import bacpac, if file is available (and not imported previously)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mpern . I'm progressing on the requested changes. After testing, I've found that it is possible to import a bacpac without an already created database. Despite to that, I think it would be better to initialise brand new database anyway and then check if a bacpac is present to be imported. What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not vouching the idea of having the init and the import on the same docker-compose service.

I'm keeping both services apart for now. I will wait for your review for the changes I've just added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.
Let's keep using two different services for now.

Comment on lines 25 to 33
#https://learn.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage-download?view=sql-server-ver16
apt-get update && apt-get install -y unzip libunwind8 libicu55

cd ~
curl -s -L -o sqlpackage.zip https://aka.ms/sqlpackage-linux
mkdir ~/sqlpackage
unzip sqlpackage.zip -d ~/sqlpackage
echo "export PATH=\"\$PATH:$HOME/sqlpackage\"" >> ~/.bashrc
chmod a+x ~/sqlpackage/sqlpackage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move install steps to a Dockerfile and use docker compose build
e.g.

...
  mssqlinit:
    build: ./docker-resources/init
    volumes: 
      - ./docker-resources:/tmp/resources
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello. This change has been added. The unique way of getting it done was to name the file Dockerfile and point to the docker-resources folder as the folder to find the build file for docker.


echo "SQL Server is up; Starting bacpac import..."
# Run the setup script to create the DB and the schema in the DB
/root/sqlpackage/sqlpackage /a:Import /sf:/tmp/resources/backup.bacpac /TargetServerName:mssql /TargetDatabaseName:localdev /TargetUser:sa /TargetPassword:$SA_PASSWORD No newline at end of file
Copy link
Member

@mpern mpern Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if file backup.bacpac exists before executing the import

nice to have:
after import, touch a kind of marker file to indicate that the file has already been imported; skip import if marker is present.
if you want to be really fancy, you could calculate a hash of the bacpac and use that to determine if you need to run the import again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello.

I have checked what happens when I try to import twice the snapshot. I get the following error:

Importing to database 'localdev' on server 'mssql'.
Creating deployment plan
Initializing deployment
*** Error importing database:Data cannot be imported into target because it contains one or more user objects. Import should be performed against a new, empty database.
Error SQL0: Data cannot be imported into target because it contains one or more user objects. Import should be performed against a new, empty database.

It seems that once there is data on the database, we can not import any other snapshot.
What are your thoughts on this?

feat: to add condition to import the snapshot

fix: to rename db backup to db snapshot

feat: to add .gitignore
@carvido1 carvido1 requested a review from mpern November 13, 2022 22:50
Copy link
Member

@mpern mpern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more things to adapt, but nothing major

if [ $UBUNTU_VERSION -eq 16 ]; then apt-get install -y libicu55; elif [ $UBUNTU_VERSION -eq 17 ]; then apt-get install -y libicu57; \
elif [ $UBUNTU_VERSION -eq 18 ]; then apt-get install -y libicu60; elif [ $UBUNTU_VERSION -eq 20 ]; then apt-get install -y libicu66; fi && \
curl -s -L -o /opt/sqlpackage.zip https://aka.ms/sqlpackage-linux && mkdir /opt/sqlpackage && unzip /opt/sqlpackage.zip -d /opt/sqlpackage && \
rm -f /opt/sqlpackage.zip && chmod a+x /opt/sqlpackage/sqlpackage && echo "export PATH=\"\$PATH:$/opt/sqlpackage\"" >> ~/.bashrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1 @@
*.bacpac No newline at end of file
Copy link
Member

@mpern mpern Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That reminds me:
Please create a .dockerignore for docker-resources to ignore bacpac files while building the import container
OR
move the dockerfile of mssqlimport to a different subfolder

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#exclude-with-dockerignore

- SA_PASSWORD=localSAPassw0rd
depends_on:
- mssql
mssqlimport:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.
Let's keep using two different services for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants