# Fri, 3 Jul 2026 11:20:33 GMT
---
version: 23
data:


- type: ModificationMethod
  attributes:
    displayName: Restart sequence
    description: |
          This modification method reads the highest value of the selected column of the table and adds 1 to this value.
                "Then the selected column of the target table restarts with this value. On this way it is avoided that
                "the current sequence number will ever be reached what would cause loss of data.
                "You have to select the primary key-column in the linked modification set for the correct usage.
    scriptLanguage: "GROOVY"
    legacyScript: false
    inputType: "COLUMN"
    mappingTableContainers: []
    mappingTableAccessMode: READ
    code: |
        package de.ubs.xdm3.configuration.examples.modification.misc
        
        /*
        * *****************************************************************************
        * The following variables are available in this script:
        * =====================================================
        * ctx             : A context object that offers a number of methods.
        *                   The method indexOf() can be used to get the index of a
        *                   specific column within the data.
        * data            : The data array. This array contains the contents of all
        *                   columns of the current row of the table that is being
        *                   masked by this method.
        *
        * Additionally for column based modification methods the following two
        * variables are available:
        * columnName      : The name of the column that matched the pattern.
        * columnIndex     : The position of the column that matched the pattern within
        *                   the data array. This value is zero-based.
        * *****************************************************************************
        */
        
        /*
         * This function is called once before the processing starts. The method
         * is called once per table.
         */
        def init() {
            def maxValue = ctx.fetchSingleValueFromConnection("TARGET", "SELECT MAX(${columnName}) FROM \"${ctx.getCurrentTableMappedSchema()}\".\"${ctx.getCurrentTableMappedName()}\"")
            def pool = ctx.getCurrentTargetConnectionPool()
            def connection = pool.get()
            def stmt = connection.createStatement()
            stmt.execute("ALTER TABLE \"${ctx.getCurrentTableMappedSchema()}\".\"${ctx.getCurrentTableMappedName()}\" ALTER COLUMN ${columnName} RESTART WITH ${(maxValue as Integer) + 1}")
            pool.put(connection)
        }
        
        /*
        * This function should modify the row that is provided by the data. The
        * method is called for each single row of the table.
        */
        def apply() {
            return true
        }
        
        /*
        * This function is executed once after the modification of a table is complete. The method
        * is called once per table.
        */
        def close() {
        
        }
