"""
This script generates a user creation script and a list of ALTER GROUP scripts to add the user to the corresponding groups.
Sample Execution:
python Generate_user_creation_script.py
Enter the username for the new user: test_user
Enter the schema names (comma-separated, e.g., 'etl,hr,public'): "etl","hr","public"
Output:
CREATE USER "test_user" PASSWORD 'Hidden1!';
ALTER GROUP etl_ro ADD USER "test_user";
ALTER GROUP hr_ro ADD USER "test_user";
ALTER GROUP public_ro ADD USER "test_user";
"""
def strip_and_parse_schemas(schemas_string):
"""
Strips the double quotes and commas from a given string of schema names and returns a list of schema names.
:param schemas_string: String of schema names, e.g. '"etl","hr","public"'
:return: List of schema names ['etl', 'hr', 'public']
"""
schemas_list = schemas_string.replace('"', '').split(',')
return schemas_list
def generate_create_user_script(username, password):
"""
Generates SQL script to create a new user with a given username and password.
:param username: The username of the new user
:param password: The password for the new user
:return: SQL statement for user creation
"""
create_user_script = f'CREATE USER "{username}" PASSWORD \'{password}\';'
return create_user_script
def generate_alter_group_scripts(schemas, username):
"""
Generates SQL statements to add the user to the pre-existing groups for each schema.
:param schemas: List of schema names
:param username: The username to be added to the groups
:return: List of ALTER GROUP SQL statements
"""
alter_group_scripts = []
for schema in schemas:
group_name = f"{schema}_ro" # Dynamically generate the group name (e.g., "etl_ro")
alter_group_script = f'ALTER GROUP {group_name} ADD USER "{username}";'
alter_group_scripts.append(alter_group_script)
return alter_group_scripts
def print_scripts(schemas, username, password):
"""
Prints the user creation script and the ALTER GROUP scripts to add the user to the corresponding groups.
:param schemas: List of schema names
:param username: The username of the new user
:param password: The password for the new user
"""
print(generate_create_user_script(username, password))
alter_group_scripts = generate_alter_group_scripts(schemas, username)
for script in alter_group_scripts:
print(script)
def main():
# Prompt for username input
username = input("Enter the username for the new user: ")
# Prompt for schema array input (comma-separated string)
schemas_string = input("Enter the schema names (comma-separated, e.g., 'etl,hr,public'): ")
# Strip and parse the schema names
schemas = strip_and_parse_schemas(schemas_string)
# Set the password (or prompt for it if you'd like)
password = "Hidden1!" # You can customize this if needed
# Generate and print the scripts
print_scripts(schemas, username, password)
if __name__ == "__main__":
main()
Month: March 2025
Unstage the last local commit on git
This can be done using the git reset command to move the HEAD pointer back one commit while keeping the changes in your working directory.
git reset HEAD~1
The command has successfully unstaged your last commit. The changes from that commit are now back in your working directory, but they are not staged or committed. You can now:
- Review the changes
- Make additional modifications if needed
- Stage and commit the changes again when you’re ready
Revert the last local changes
This will discard all uncommitted changes in your working directory and staging area. Since this is a destructive operation, I’ll use git reset –hard to revert to the last commit.
git reset --hard HEAD
What Happens
- All uncommitted changes are discarded
- Working directory returns to the state of the last commit
- Staging area is cleared
- ⚠️ Warning: This operation cannot be undone
Export and Import MongoDB
-- Download selected collections from prod cluster
mongoexport --uri="mongodb+srv://user_name@prodcluster.zfzcx.mongodb.net/proddb" --collection=groups --out=groups.json
mongoexport --uri="mongodb+srv://user_name@prodcluster.zfzcx.mongodb.net/proddb" --collection=members --out=members.json
--Import json into another cluster
mongoimport --uri "mongodb+srv://user_name@prodcluster_2.3i2z0.mongodb.net/proddb" --collection groups --file groups.json
mongoimport --uri "mongodb+srv://user_name@prodcluster_2.3i2z0.mongodb.net/proddb" --collection members --file members.json
-- Mongodump to download the entire DB
mongodump --uri="mongodb+srv://user_name@prodcluster.zfzcx.mongodb.net/proddb" --out=c:/path/mongodump
-- Mongorestore to restore the entire DB
mongorestore --uri="mongodb+srv://user_name@prodcluster.zfzcx.mongodb.net/proddb" c:/path/mongodump
Query SQL server database restore history
This query shows the restore history, useful for transaction log restores and tracking progress.
SELECT distinct getdate() as Current_Server__Date, @@SERVERNAME as Destination_Server_Name, [rs].[destination_database_name],
[bs]. type as Backup_Type,
[rs].user_name,
[rs].[restore_date],
[bs].[backup_start_date],
[bs].[backup_finish_date],
[bs].[database_name] as [source_database_name],
[bs].[server_name] as [source_server_name]
--[bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM msdb..restorehistory rs
INNER JOIN msdb..backupset bs
ON [rs].[backup_set_id] = [bs].[backup_set_id]
INNER JOIN msdb..backupmediafamily bmf
ON [bs].[media_set_id] = [bmf].[media_set_id]
--where [rs].destination_database_name = 'SM1' and [bs].type='L' --Change as per requirement
ORDER BY [rs].[restore_date] DESC
--group by [rs].[destination_database_name]