Skip to content

Conversation

@skywalkerisnull
Copy link

Allows for a more flexible restore where an interactive SQL session can pass in a DB name. This is useful when you want to restore to a different DB to the one that was originally named.

This does not change the current behaviour of the SQL script.

Allows for a more flexible restore where an interactive SQL session can pass in a DB name. This is useful when you want to restore to a different DB to the one that was originally named.

This does not change the current behaviour of the SQL script.
Fixed the spacing of 
-- Server version	{{ .ServerVersion }}
@deitch
Copy link
Collaborator

deitch commented Dec 9, 2024

This does not change the current behaviour of the SQL script

Do you mind explaining how that is?

@skywalkerisnull
Copy link
Author

skywalkerisnull commented Dec 9, 2024

Not a problem.

The update that I have put forward will mean that functionally, the SQL script that is generated will behave the same as the existing one. i.e. if you do not provide the script with the parameter, it will use the original DB name (because of the null check). But if you provide the parameter input, it will give you the ability to specify a different DB name.

Looks like there are some unit tests that I need to update with this PR!

@deitch
Copy link
Collaborator

deitch commented Dec 9, 2024

the SQL script that is generated will behave the same as the existing one. i.e. if you do not provide the script with the parameter, it will use the original DB name (because of the null check). But if you provide the parameter input, it will give you the ability to specify a different DB name

That is what I didn't get. The code is as follows:

-- Define the database name variable
SET @DB_NAME := '{{.Database}}';
-- Replace '{{.Database}}' with the actual database name if provided as an argument
-- You can pass the database name as an argument in the command line while running the script:
-- mysql -u username -p < script.sql --database=new_db_name
-- The database name passed will override '{{.Database}}'
-- Check if the DB_NAME variable is set in the MySQL interactive mode
-- If not set, it will use the default value defined above
USE IFNULL(@DB_NAME, '{{.Database}}');

It sets the @DB_NAME to the value of .Database, but then it sets USE IFNULL(@DB_NAME,'{{.Database}}');.

  1. Sets @DB_NAME, so assumes .Database is set
  2. checks if @DB_NAME is null, which only would be true if .Database is null, and if it is, use .Database, which we just checked was null?

Looks like there are some unit tests that I need to update with this PR!

Yeah, the tests check the output.

@skywalkerisnull
Copy link
Author

Ahh dam, I had a brain glitch here. MySQL does not support the passing of a variable (that is a MS SQL thing that I was trying to do here instead). And my test on my local I was using the same DB name.

@deitch
Copy link
Collaborator

deitch commented Dec 10, 2024

I had a brain glitch here

Smile and get a good laugh out of it.

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