Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Si 100 db maintenance script #38

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell: Launch Current File",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"args": []
}
]
}
6 changes: 3 additions & 3 deletions AzureSQLMaintenance.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Change Log:
if object_id('AzureSQLMaintenance') is null
exec('create procedure AzureSQLMaintenance as /*dummy procedure body*/ select 1;')
GO
ALTER PROCEDURE [AzureSQLMaintenance]
ALTER PROCEDURE [dbo].[AzureSQLMaintenance]
(
@operation nvarchar(10) = null,
@mode nvarchar(10) = 'smart',
Expand Down Expand Up @@ -77,14 +77,14 @@ begin
raiserror(' "0" (Default) using non resumable index rebuild.',0,0)
raiserror(' "1" using resumable index rebuild when it is supported.',0,0)
raiserror(' ',0,0)
raiserror('@RebuildHeaps(bit) [optional]',0,0)
raiserror('@LogToTable(bit) [optional]',0,0)
raiserror(' Logging option: @LogToTable(bit)',0,0)
raiserror(' 0 - (Default) do not log operation to table',0,0)
raiserror(' 1 - log operation to table',0,0)
raiserror(' for logging option only 3 last execution will be kept by default. this can be changed by easily in the procedure body.',0,0)
raiserror(' Log table will be created automatically if not exists.',0,0)
raiserror(' ',0,0)
raiserror('@LogToTable(bit) [optional]',0,0)
raiserror('@RebuildHeaps(bit) [optional]',0,0)
raiserror(' Rebuild HEAPS to fix forwarded records issue on tables with no clustered index',0,0)
raiserror(' 0 - (Default) do not rebuild heaps',0,0)
raiserror(' 1 - Rebuild heaps based on @mode parameter, @mode=dummy will rebuild all heaps',0,0)
Expand Down
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory to /app
WORKDIR /app

# Install the SQL Server tools (sqlcmd) and ODBC drivers
RUN apt-get update \
&& apt-get install -y curl \
&& apt-get install -y gnupg \
&& curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \
&& ACCEPT_EULA=Y apt-get install -y mssql-tools \
&& apt-get install vim -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/


# Copy the current directory contents into the container at /app
COPY . /app

# Install any Python dependencies specified in requirements.txt
#RUN pip install -r requirements.txt

# Define the default command to run your Python script with sqlcmd
CMD ["python", "sql_maintenance.py"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# AzureSQL
# Uniper Azure DB Maintenance

Anything that is related to Azure SQL DB
The procedure AzureSQLMaintenance.txt is used for automated runbooks created in the https://github.com/uniper/uniper-terraform
repository.

AzureSQLMaintenance.txt - https://techcommunity.microsoft.com/t5/azure-database-support-blog/how-to-maintain-azure-sql-indexes-and-statistics/ba-p/368787

2 changes: 2 additions & 0 deletions query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT 1
SELECT 2
69 changes: 69 additions & 0 deletions sql_maintenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import subprocess

# Define your SQL Server connection details
server = os.environ['DB_CONNECTION_URL']
database = os.environ['my_db_list']
username = os.environ['DB_USERNAME']
password = os.environ['DB_PASSWORD']

sql_script = "/app/query.sql"

#converting in to absolute path
sql_script = os.path.abspath(sql_script)

query_to_run = ["exec [dbo].[AzureSQLMaintenance] @Operation='index',@mode='smart',@LogToTable=1", "exec [dbo].[AzureSQLMaintenance] @Operation='statistics',@mode='dummy' ,@LogToTable=1"]


# Creating procedure in DB
creating_procedure_command = [
"/opt/mssql-tools/bin/sqlcmd",
"-S", server,
"-d", database,
"-U", username,
"-P", password,
"-i", sql_script,
"-t", "65534",
"-l", "60"
]

#creatating procedure in DB

try:
procedure_creation = subprocess.run(creating_procedure_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

if procedure_creation.returncode == 0:
print("Procedure has been created successfully")
else:
print("Error Occured:")
print(procedure_creation.stderr)
except subprocess.CalledProcessError as e:
print("Error running sqlcmd command:")
print(e.stderr)

# Triggering Procedure
for item in query_to_run:
triggering_procedure_command = [
"/opt/mssql-tools/bin/sqlcmd",
"-S", server,
"-d", database,
"-U", username,
"-P", password,
"-Q", item,
"-t", "65534",
"-l", "60"
]

try:
procedure_exec_index = subprocess.run(triggering_procedure_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

if procedure_exec_index.returncode == 0:
print("Query has been executed successfully")
else:
print("Error Occured:")
print(procedure_exec_index.stderr)
except subprocess.CalledProcessError as e:
print("Error running sqlcmd command:")