Savelyev_Vyacheslav

migration Column

Sep 10th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let JSONData = `
  2.  
  3. {
  4.     "sys_db_column": [
  5.         {
  6.             "active": true,
  7.             "application_id": "155931135900000002",
  8.             "choice_field_id": null,
  9.             "choice_table_id": null,
  10.             "choice_type": null,
  11.             "column_name": "name",
  12.             "column_type_id": 12,
  13.             "comments": "",
  14.             "default_value": "",
  15.             "dependent_on_column_id": null,
  16.             "display_by_ref": true,
  17.             "dynamic_default_id": null,
  18.             "dynamic_reference_qualifier_id": null,
  19.             "extra_attributes": "",
  20.             "full_text_search": true,
  21.             "mandatory": false,
  22.             "max_length": null,
  23.             "on_delete": "no_action",
  24.             "policy": "Changed",
  25.             "primary": false,
  26.             "read_only": false,
  27.             "reference_id": null,
  28.             "reference_qualifier_condition": "",
  29.             "reference_qualifier_fixed": false,
  30.             "reference_qualifier_type": null,
  31.             "sys_created_at": "2023-12-05 18:03:37",
  32.             "sys_created_by": "169390306693244400",
  33.             "sys_db_table_id": "155931135900000012",
  34.             "sys_id": "170179941798378472",
  35.             "sys_updated_at": "2023-12-05 18:05:06",
  36.             "sys_updated_by": "169390306693244400",
  37.             "table_id": "172586472001070933",
  38.             "title": "\u041d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435",
  39.             "unique": false,
  40.             "use_dynamic_default": false
  41.         },
  42.         {
  43.             "active": true,
  44.             "application_id": "155931135900000002",
  45.             "choice_field_id": null,
  46.             "choice_table_id": null,
  47.             "choice_type": null,
  48.             "column_name": "manager",
  49.             "column_type_id": 8,
  50.             "comments": "",
  51.             "default_value": "",
  52.             "dependent_on_column_id": null,
  53.             "display_by_ref": false,
  54.             "dynamic_default_id": null,
  55.             "dynamic_reference_qualifier_id": null,
  56.             "extra_attributes": "",
  57.             "full_text_search": false,
  58.             "mandatory": false,
  59.             "max_length": null,
  60.             "on_delete": "no_action",
  61.             "policy": "Open",
  62.             "primary": false,
  63.             "read_only": false,
  64.             "reference_id": "156873090301469473",
  65.             "reference_qualifier_condition": "",
  66.             "reference_qualifier_fixed": false,
  67.             "reference_qualifier_type": null,
  68.             "sys_created_at": "2023-12-05 18:03:01",
  69.             "sys_created_by": "169390306693244400",
  70.             "sys_db_table_id": "155931135900000012",
  71.             "sys_id": "170179938191888852",
  72.             "sys_updated_at": "2023-12-05 18:03:01",
  73.             "sys_updated_by": "169390306693244400",
  74.             "table_id": "172586472001070933",
  75.             "title": "\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440",
  76.             "unique": false,
  77.             "use_dynamic_default": false
  78.         },
  79.         {
  80.             "active": true,
  81.             "application_id": "155931135900000002",
  82.             "choice_field_id": null,
  83.             "choice_table_id": null,
  84.             "choice_type": null,
  85.             "column_name": "location",
  86.             "column_type_id": 8,
  87.             "comments": "",
  88.             "default_value": "",
  89.             "dependent_on_column_id": null,
  90.             "display_by_ref": false,
  91.             "dynamic_default_id": null,
  92.             "dynamic_reference_qualifier_id": null,
  93.             "extra_attributes": "",
  94.             "full_text_search": false,
  95.             "mandatory": false,
  96.             "max_length": null,
  97.             "on_delete": "no_action",
  98.             "policy": "Changed",
  99.             "primary": false,
  100.             "read_only": false,
  101.             "reference_id": "156873092308675549",
  102.             "reference_qualifier_condition": "",
  103.             "reference_qualifier_fixed": false,
  104.             "reference_qualifier_type": null,
  105.             "sys_created_at": "2023-12-05 18:02:28",
  106.             "sys_created_by": "169390306693244400",
  107.             "sys_db_table_id": "155931135900000012",
  108.             "sys_id": "170179934897781752",
  109.             "sys_updated_at": "2023-12-22 12:52:34",
  110.             "sys_updated_by": "169390306693244400",
  111.             "table_id": "172586472001070933",
  112.             "title": "\u0410\u0434\u0440\u0435\u0441 \u0441\u043a\u043b\u0430\u0434\u0430",
  113.             "unique": false,
  114.             "use_dynamic_default": false
  115.         },
  116.         {
  117.             "active": true,
  118.             "application_id": "155931135900000002",
  119.             "choice_field_id": null,
  120.             "choice_table_id": null,
  121.             "choice_type": null,
  122.             "column_name": "assignment_group",
  123.             "column_type_id": 8,
  124.             "comments": "",
  125.             "default_value": "",
  126.             "dependent_on_column_id": null,
  127.             "display_by_ref": false,
  128.             "dynamic_default_id": null,
  129.             "dynamic_reference_qualifier_id": null,
  130.             "extra_attributes": "",
  131.             "full_text_search": false,
  132.             "mandatory": false,
  133.             "max_length": null,
  134.             "on_delete": "no_action",
  135.             "policy": "Open",
  136.             "primary": false,
  137.             "read_only": false,
  138.             "reference_id": "155931135900000025",
  139.             "reference_qualifier_condition": "",
  140.             "reference_qualifier_fixed": false,
  141.             "reference_qualifier_type": null,
  142.             "sys_created_at": "2023-12-05 18:02:01",
  143.             "sys_created_by": "169390306693244400",
  144.             "sys_db_table_id": "155931135900000012",
  145.             "sys_id": "170179932190291068",
  146.             "sys_updated_at": "2023-12-05 18:02:01",
  147.             "sys_updated_by": "169390306693244400",
  148.             "table_id": "172586472001070933",
  149.             "title": "\u0420\u0430\u0431\u043e\u0447\u0430\u044f \u0433\u0440\u0443\u043f\u043f\u0430",
  150.             "unique": false,
  151.             "use_dynamic_default": false
  152.         },
  153.         {
  154.             "active": true,
  155.             "application_id": "155931135900000002",
  156.             "choice_field_id": null,
  157.             "choice_table_id": null,
  158.             "choice_type": null,
  159.             "column_name": "sys_updated_by",
  160.             "column_type_id": 8,
  161.             "comments": null,
  162.             "default_value": null,
  163.             "dependent_on_column_id": null,
  164.             "display_by_ref": false,
  165.             "dynamic_default_id": null,
  166.             "dynamic_reference_qualifier_id": null,
  167.             "extra_attributes": null,
  168.             "full_text_search": false,
  169.             "mandatory": false,
  170.             "max_length": null,
  171.             "on_delete": "no_action",
  172.             "policy": "Protected",
  173.             "primary": false,
  174.             "read_only": false,
  175.             "reference_id": "155931135900000084",
  176.             "reference_qualifier_condition": null,
  177.             "reference_qualifier_fixed": false,
  178.             "reference_qualifier_type": null,
  179.             "sys_created_at": "2023-12-05 18:01:25",
  180.             "sys_created_by": "169390306693244400",
  181.             "sys_db_table_id": "155931135900000012",
  182.             "sys_id": "170179928598071216",
  183.             "sys_updated_at": "2023-12-05 18:01:25",
  184.             "sys_updated_by": "169390306693244400",
  185.             "table_id": "172586472001070933",
  186.             "title": "Updated by",
  187.             "unique": false,
  188.             "use_dynamic_default": false
  189.         },
  190.         {
  191.             "active": true,
  192.             "application_id": "155931135900000002",
  193.             "choice_field_id": null,
  194.             "choice_table_id": null,
  195.             "choice_type": null,
  196.             "column_name": "sys_created_by",
  197.             "column_type_id": 8,
  198.             "comments": null,
  199.             "default_value": null,
  200.             "dependent_on_column_id": null,
  201.             "display_by_ref": false,
  202.             "dynamic_default_id": null,
  203.             "dynamic_reference_qualifier_id": null,
  204.             "extra_attributes": null,
  205.             "full_text_search": false,
  206.             "mandatory": false,
  207.             "max_length": null,
  208.             "on_delete": "no_action",
  209.             "policy": "Protected",
  210.             "primary": false,
  211.             "read_only": false,
  212.             "reference_id": "155931135900000084",
  213.             "reference_qualifier_condition": null,
  214.             "reference_qualifier_fixed": false,
  215.             "reference_qualifier_type": null,
  216.             "sys_created_at": "2023-12-05 18:01:24",
  217.             "sys_created_by": "169390306693244400",
  218.             "sys_db_table_id": "155931135900000012",
  219.             "sys_id": "170179928490595394",
  220.             "sys_updated_at": "2023-12-05 18:01:24",
  221.             "sys_updated_by": "169390306693244400",
  222.             "table_id": "172586472001070933",
  223.             "title": "Created by",
  224.             "unique": false,
  225.             "use_dynamic_default": false
  226.         },
  227.         {
  228.             "active": true,
  229.             "application_id": "155931135900000002",
  230.             "choice_field_id": null,
  231.             "choice_table_id": null,
  232.             "choice_type": null,
  233.             "column_name": "sys_updated_at",
  234.             "column_type_id": 3,
  235.             "comments": null,
  236.             "default_value": null,
  237.             "dependent_on_column_id": null,
  238.             "display_by_ref": false,
  239.             "dynamic_default_id": null,
  240.             "dynamic_reference_qualifier_id": null,
  241.             "extra_attributes": null,
  242.             "full_text_search": false,
  243.             "mandatory": false,
  244.             "max_length": null,
  245.             "on_delete": null,
  246.             "policy": "Protected",
  247.             "primary": false,
  248.             "read_only": false,
  249.             "reference_id": null,
  250.             "reference_qualifier_condition": null,
  251.             "reference_qualifier_fixed": false,
  252.             "reference_qualifier_type": null,
  253.             "sys_created_at": "2023-12-05 18:01:24",
  254.             "sys_created_by": "169390306693244400",
  255.             "sys_db_table_id": "155931135900000012",
  256.             "sys_id": "170179928490594998",
  257.             "sys_updated_at": "2023-12-05 18:01:24",
  258.             "sys_updated_by": "169390306693244400",
  259.             "table_id": "172586472001070933",
  260.             "title": "Updated at",
  261.             "unique": false,
  262.             "use_dynamic_default": false
  263.         },
  264.         {
  265.             "active": true,
  266.             "application_id": "155931135900000002",
  267.             "choice_field_id": null,
  268.             "choice_table_id": null,
  269.             "choice_type": null,
  270.             "column_name": "sys_created_at",
  271.             "column_type_id": 3,
  272.             "comments": null,
  273.             "default_value": null,
  274.             "dependent_on_column_id": null,
  275.             "display_by_ref": false,
  276.             "dynamic_default_id": null,
  277.             "dynamic_reference_qualifier_id": null,
  278.             "extra_attributes": null,
  279.             "full_text_search": false,
  280.             "mandatory": false,
  281.             "max_length": null,
  282.             "on_delete": null,
  283.             "policy": "Protected",
  284.             "primary": false,
  285.             "read_only": false,
  286.             "reference_id": null,
  287.             "reference_qualifier_condition": null,
  288.             "reference_qualifier_fixed": false,
  289.             "reference_qualifier_type": null,
  290.             "sys_created_at": "2023-12-05 18:01:24",
  291.             "sys_created_by": "169390306693244400",
  292.             "sys_db_table_id": "155931135900000012",
  293.             "sys_id": "170179928490594635",
  294.             "sys_updated_at": "2023-12-05 18:01:24",
  295.             "sys_updated_by": "169390306693244400",
  296.             "table_id": "172586472001070933",
  297.             "title": "Created at",
  298.             "unique": false,
  299.             "use_dynamic_default": false
  300.         },
  301.         {
  302.             "active": true,
  303.             "application_id": "155931135900000002",
  304.             "choice_field_id": null,
  305.             "choice_table_id": null,
  306.             "choice_type": null,
  307.             "column_name": "sys_id",
  308.             "column_type_id": 23,
  309.             "comments": null,
  310.             "default_value": null,
  311.             "dependent_on_column_id": null,
  312.             "display_by_ref": false,
  313.             "dynamic_default_id": null,
  314.             "dynamic_reference_qualifier_id": null,
  315.             "extra_attributes": null,
  316.             "full_text_search": false,
  317.             "mandatory": false,
  318.             "max_length": null,
  319.             "on_delete": null,
  320.             "policy": "Protected",
  321.             "primary": true,
  322.             "read_only": false,
  323.             "reference_id": null,
  324.             "reference_qualifier_condition": null,
  325.             "reference_qualifier_fixed": false,
  326.             "reference_qualifier_type": null,
  327.             "sys_created_at": "2023-12-05 18:01:24",
  328.             "sys_created_by": "169390306693244400",
  329.             "sys_db_table_id": "155931135900000012",
  330.             "sys_id": "170179928490594322",
  331.             "sys_updated_at": "2023-12-05 18:05:04",
  332.             "sys_updated_by": "169390306693244400",
  333.             "table_id": "172586472001070933",
  334.             "title": "ID",
  335.             "unique": true,
  336.             "use_dynamic_default": false
  337.         }
  338.     ]
  339. }
  340.  
  341. `
  342. function modifyAndCleanData(dataJSON_IN, keysToRemove = []) {
  343.     // Модифицируем строку JSON, добавляя кавычки вокруг числовых значений
  344.     let modifiedJSON = dataJSON_IN.replace(/"(\w+)": (\d{15,})/g, '"$1": "$2"');
  345.  
  346.     // Парсим модифицированную строку JSON обратно в объект JavaScript
  347.     let data = JSON.parse(modifiedJSON);
  348.  
  349.  
  350.     return data;
  351.   }
  352.  
  353. function insertRecords(data) {
  354.     var records = data.sys_db_column; // Получаем массив записей из JSON
  355.    
  356.     records.forEach(function(record) {
  357.         var gr = new SimpleRecord('sys_db_column'); // Указываем таблицу для вставки
  358.  
  359.         // Явно прописываем поля
  360.         gr.setValue('active', record.active);
  361.         // gr.setValue('application_id', record.application_id);
  362.         gr.setValue('choice_field_id', record.choice_field_id);
  363.         gr.setValue('choice_table_id', record.choice_table_id);
  364.         gr.setValue('choice_type', record.choice_type);
  365.         gr.setValue('column_name', record.column_name);
  366.         gr.setValue('column_type_id', record.column_type_id);
  367.         gr.setValue('comments', record.comments);
  368.         gr.setValue('default_value', record.default_value);
  369.         gr.setValue('dependent_on_column_id', record.dependent_on_column_id);
  370.         gr.setValue('display_by_ref', record.display_by_ref);
  371.         gr.setValue('dynamic_default_id', record.dynamic_default_id);
  372.         gr.setValue('dynamic_reference_qualifier_id', record.dynamic_reference_qualifier_id);
  373.         gr.setValue('extra_attributes', record.extra_attributes);
  374.         gr.setValue('full_text_search', record.full_text_search);
  375.         gr.setValue('mandatory', record.mandatory);
  376.         gr.setValue('max_length', record.max_length);
  377.         gr.setValue('on_delete', record.on_delete);
  378.         gr.setValue('policy', record.policy);
  379.         gr.setValue('primary', record.primary);
  380.         gr.setValue('read_only', record.read_only);
  381.         gr.setValue('reference_id', record.reference_id);
  382.         gr.setValue('reference_qualifier_condition', record.reference_qualifier_condition);
  383.         gr.setValue('reference_qualifier_fixed', record.reference_qualifier_fixed);
  384.         gr.setValue('reference_qualifier_type', record.reference_qualifier_type);
  385.         // gr.setValue('sys_created_at', record.sys_created_at);
  386.         // gr.setValue('sys_created_by', record.sys_created_by);
  387.         gr.setValue('sys_db_table_id', record.sys_db_table_id);
  388.         // gr.setValue('sys_id', record.sys_id);
  389.         // gr.setValue('sys_updated_at', record.sys_updated_at);
  390.         // gr.setValue('sys_updated_by', record.sys_updated_by);
  391.         gr.setValue('table_id', record.table_id);
  392.         gr.setValue('title', record.title);
  393.         gr.setValue('unique', record.unique);
  394.         gr.setValue('use_dynamic_default', record.use_dynamic_default);
  395.  
  396.         gr.insert(); // Вставляем запись в таблицу
  397.     });
  398. }
  399.  
  400. JSONData = modifyAndCleanData(JSONData)
  401.  
  402. insertRecords(JSONData);
  403.  
  404.          
Advertisement
Add Comment
Please, Sign In to add comment