""" 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. - Allows the user to specify which views to migrate. - Replaces 'CREATE VIEW' with 'CREATE OR ALTER VIEW' for idempotent creation. - Executes the migration with error handling, logging successes/failures. - Writes any failed migrations to a CSV for retry. """ import pandas as pd import re import struct import sqlalchemy from sqlalchemy import create_engine, text import pyodbc import notebookutils import sempy.fabric as fabric # ------------------------------------------------- # Helper: Create a SQLAlchemy engine for Fabric SQL endpoint with MSI token # ------------------------------------------------- def create_engine_alt(connection_string: str): """ Creates a SQLAlchemy engine for Fabric SQL endpoint authentication using Managed Service Identity (MSI) access token. Args: connection_string (str): ODBC connection string for Fabric SQL endpoint. Returns: sqlalchemy.Engine: Configured SQLAlchemy engine. """ token = notebookutils.credentials.getToken( 'https://analysis.windows.net/powerbi/api' ).encode("UTF-16-LE") token_struct = struct.pack(f'