Skip to content

Instantly share code, notes, and snippets.

View MarkPryceMaherMSFT's full-sized avatar

Mark Pryce-Maher MarkPryceMaherMSFT

View GitHub Profile
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / sp_help_change_feed.sql
Created October 9, 2025 19:34
Code to materialise the stored proc sp_help_change_feed
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[help_change_feed]') AND type in (N'U')) DROP TABLE [dbo].[help_change_feed]
create table help_change_feed
(
table_group_id uniqueidentifier ,
table_group_name nvarchar(140) ,
destination_location nvarchar(512) ,
destination_credential nvarchar(247) ,
destination_type nvarchar(247) ,
workspace_id nvarchar(247) ,
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / ConvertViews.py
Last active October 4, 2025 03:12
This code is for converting spark views to tsql views
%%pyspark
import sempy.fabric as fabric
import struct
import sqlalchemy
import pyodbc
import pandas as pd
from notebookutils import mssparkutils
#Function to Return sqlalchemt ODBC Engine, given a connection string and using Integrated AAD Auth to Fabric
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / mapping.json
Created September 29, 2025 11:19
json mapping
@json('
"translator": {
"type": "TabularTranslator",
"mappings": [
{
"source": {
"name": "t",
"type": "DateTime",
"physicalType": "datetime"
},
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / sync_everything.py
Created September 4, 2025 16:51
MD Sync all lakehouses
import requests
from notebookutils import mssparkutils
import sempy.fabric as fabric
from sempy.fabric.exceptions import FabricHTTPException, WorkspaceNotFoundException
import json
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType
from datetime import datetime
# Get token
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / delta_publishing_test.py
Last active September 5, 2025 08:10
Test to see how long it takes delta log publishing to work
import pandas as pd
import struct
import sqlalchemy
import pyodbc
import notebookutils
import sempy.fabric as fabric
from sqlalchemy import text
import time
# -----------------------------------------------------------------------------
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / migrate_views.py
Created August 11, 2025 13:11
View Migration Script: SQL Server ➜ Fabric Warehouse/Lakehouse -------------------------------------------------------------- This script copies one or more view definitions from a source SQL Server database into a Fabric Warehouse or Lakehouse SQL endpoint. Key Features: - Connects to SQL Server using SQLAlchemy/pyodbc. - Connects to Fabric usi…
"""
View Migration Script: SQL Server ➜ Fabric Warehouse/Lakehouse
--------------------------------------------------------------
This script copies one or more view definitions from a source SQL Server database
into a Fabric Warehouse or Lakehouse SQL endpoint.
Key Features:
- Connects to SQL Server using SQLAlchemy/pyodbc.
- Connects to Fabric using MSI authentication (access token).
- Reads view definitions from sys.views/sys.sql_modules in the source.
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / updatecolumns.sql
Created August 1, 2025 18:41
this generates the script to alter the columns and remove the user defined data types
/*
This is very rough, it doesn't work for ints/bits/etc only varchars
This does not work if there are schema bound views
*/
select 'alter table ' + s.name + '.' + object_name(c.object_id) + ' ALTER COLUMN ' + c.name + ' ' + ttt.name + '(' + convert(varchar,c.max_length) + ');',
c.max_length , c.*, tt.*, s.* from sys.columns c
inner join sys.tables tt on tt.object_id = c.object_id
inner join sys.schemas s on s.schema_id = tt.schema_id
Value Description Meaning
Failure Indicates a failure. Something when wrong. Check the errors.
NotRun Indicates that the operation did not run. We did run - but found no changes to the table.
Success Indicates a success. We found a update i.e. a new delta log and updated the SQL Endpoint.
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / _metadata.json
Created June 18, 2025 23:44
Example metadata file for a DelimitedText text file
{
"KeyColumns": [ "_id_" ],
"SchemaDefinition": {
"Columns": [
{
"Name": "id",
"DataType": "Int32"
},
{
"Name": "Timestamp",
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / mdsync-rest-api.py
Last active June 9, 2025 20:55
Example code using the new fabric rest api
import json
import notebookutils
import sempy.fabric as fabric
from sempy.fabric.exceptions import FabricHTTPException, WorkspaceNotFoundException
def pad_or_truncate_string(input_string, length, pad_char=' '):
# Truncate if the string is longer than the specified length
if len(input_string) > length:
return input_string[:length]
# Pad if the string is shorter than the specified length