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

Mssql migration support for table and column comments #1630

Open
RobertoTassistroLeonardo opened this issue Dec 6, 2024 · 0 comments
Open

Comments

@RobertoTassistroLeonardo
Copy link

RobertoTassistroLeonardo commented Dec 6, 2024

hello, i try to use mysql comments flow to provide same functionality to mssql:

(defmethod add-comments-to-postgresql ((catalog catalog) (mssql copy-mssql))
  "Add table and column comments from MSSQL to PostgreSQL."
  (loop
     :for (table-name comment) :in (list-mssql-table-comments mssql)
     :do (when (and table-name comment)
           (let ((pg-table (find-table (catalog->pg catalog) table-name)))
             (when pg-table
               (execute (format nil "COMMENT ON TABLE ~a IS '~a';"
                                (escape-identifier (table-name pg-table))
                                (escape-string comment)))))))
  (loop
     :for (table-name column-name comment) :in (list-mssql-columns-comments mssql)
     :do (when (and table-name column-name comment)
           (let* ((pg-table (find-table (catalog->pg catalog) table-name))
                  (pg-column (find-column pg-table column-name)))
             (when (and pg-table pg-column)
               (execute (format nil "COMMENT ON COLUMN ~a.~a IS '~a';"
                                (escape-identifier (table-name pg-table))
                                (escape-identifier (column-name pg-column))
                                (escape-string comment))))))))

(defun list-mssql-table-comments (mssql)
  "Return comments on MSSQL tables."
  (loop
     :for (table-name comment)
     :in (mssql-query "
        SELECT 
            LOWER(so.name) AS table_name,  -- Convert table names to lowercase
            CAST(ep.value AS NVARCHAR(MAX)) AS comment
        FROM 
            sys.extended_properties ep
        JOIN 
            sys.objects so ON ep.major_id = so.object_id
        LEFT JOIN 
            sys.columns sc ON so.object_id = sc.object_id AND ep.minor_id = sc.column_id
        WHERE 
            sc.name IS NULL
        ")
     :when (and table-name comment (not (string= comment "")))
     :collect (list table-name comment)))

(defun list-mssql-columns-comments (mssql)
  "Return comments on MSSQL columns."
  (loop
     :for (table-name column-name comment)
     :in (mssql-query "
        SELECT 
            LOWER(so.name) AS table name,  -- Convert table names to lowercase
            LOWER(sc.name) AS column name,  -- Convert column names to lowercase
            CAST(ep.value AS NVARCHAR(MAX)) AS comment
        FROM 
            sys.extended_properties ep
        JOIN 
            sys.objects so ON ep.major_id = so.object_id
        LEFT JOIN 
            sys.columns sc ON so.object id = sc.object id AND ep.minor id = sc.column id
        WHERE 
            sc.name IS NOT NULL
        ")
     :when (and table name column name comment (not (string= comment "")))
     :collect (list table name column name comment)))

(defun escape-identifier (identifier)
  "Escape SQL identifiers to handle special characters properly."
  (format nil "\"~a\"" identifier))

(defun escape-string (string)
  "Escape SQL strings to handle special characters properly."
  (replace-regexp-in-string "'" "''" string))

I need to verify if is it right way to follow and to know how handle mssql connection: please let me know, your comments can be very helpful thank you

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

No branches or pull requests

1 participant