# adding_new_table.md

- create sql (field-names from this file)

  - decide table name and prefix, for ex. error_log, errl
  - create new file error_log.sql into todo/MA

- batabase lua:ssa pitää löytyä kohdasta local function createPostgreTable:
  if not tableExists(c, "db_message") then
  local sqlText = [[
  DROP TABLE IF EXISTS db_message;
  CREATE TABLE db_message(
  type varchar(40) NOT NULL DEFAULT '',
  state varchar(40) NOT NULL DEFAULT '',
  description text NOT NULL DEFAULT '',
  json_data jsonb NOT NULL DEFAULT '{}',

            record_id uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000' UNIQUE,
            create_user varchar(40) NOT NULL DEFAULT '',
            create_time timestamp with time zone NOT NULL DEFAULT '1970-01-01 00:00:00+00',

            modify_id uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
            modify_user varchar(40) NOT NULL DEFAULT '',
            modify_time timestamp with time zone NOT NULL DEFAULT '1970-01-01 00:00:00+00',
            PRIMARY KEY(record_id)
          )
        ]]
        local _, info = sqlExecuteUnsafe(c, sqlText)
        if info.error then
          print(tostring(info.error))
          return nil
        end
      end
      f = fieldTableAdd(c, "db_message", "dbm", nil) -- "db_message" = pitkä nimi "dbm" = lyhenne

  - save sql file to docs/todo/ma folder
  - must be created programmatically to get correct user rights in postgre
  - must ask pasi or aga to add to some worker_xxx.lua code
  - create short name for table e.g. db_message -> dbm

- source/prefrence/output/'tablename'/fi-list.json (colum headers for list-view)

  - first duplicate folder from old and then rename it with new table name
  - then change in list.json file colum names as you want

- output table-popup

  - (fix this)
  - add "f = db.fieldTableAdd(..." -function call to manager-worker.lua / init() -function

- source/prefrence/input/'tablename'

  - first duplicate folder from old for new table and then rename folder with new table name
  - then you can add tabs in input.json -file

- ma-git/other/old/web/ma/plugin/input

  - create input-file for table and name it 'tablename'\_input1.pug (duplicate old)
  - fix field names to match sql-file e.g.
    .col-sm-2
    ma-label(ng-model="hdr.task_description") Name
    .col-sm-2
    ma-popup(type="popup", ng-model="rec.task_description",)
  - for another input tab, create 'tablename'\_input2.pug

- begin.pug ma-git/other/old/web/ma/plugin/begin

  - begin_customer1.pug
  - begin_customer2
  - begin_customer3
  - add button for new table to begin_customerX page by duplicating old coderows
    - you need to add button style also in begin.styl
    - button logo in /ma-git/other/old/web/ma/plugin/begin/img/
  - new tabs for begin-screen you can add to 'begin_fi.json' in 'prefrence/auth' -folder

- translations

  - source-prefrence-language-lang_fi.json
    - add new table fields and do translation to finnish
  - source-prefrence-language-lang_en.json
    - add new table fields and do translation to english

- adding a new input popup/dropdown -fields

  - define.coffee
  - put popup-stuff here manually for testing
  - Old popup
    - /source/preference/input/fi/popup.json
    - add table and values you want to be shown in to the file

- checkbox-example
  ma-checkbox(ng-model='rec.show_only_open')
  ma-translate(ng-model='hdr.work_center.show_only_open') Show only open - if translation isn't found in lang_fi.json, then do translation to lang_fi_customer

- creating table to input

- include.json tells which tables are used and where gridarea links. Create this include.json to tables 'input'-folder
- for linking you need to create file to input folder which includes fields e.g. in 'task'-folder 'task_work.json'
- last add gridarea to input e.g. ma-grid#task_input1_1(ng-model='grid.task_input1_1')

- relation.json fixing

- copy sql foreign e.g.
  ALTER TABLE task_work ADD CONSTRAINT task_id_link FOREIGN KEY (task_id) REFERENCES task(record_id) ON UPDATE CASCADE ON DELETE RESTRICT;
- add this to relation.json
  {
  "editable": true,
  "calculate_field": "",
  "link_type": "prevent delete",
  "linked_table": "task",
  "linking_table": "taskw",
  "check_type": "o",
  "linked_field": "task_record_id",
  "linking_field": "taskw_task_id"
  },
- set dbf.relationTable(), local fixDone = true, restart worker, go to so input -> will generate relation_result.son, local fixDone = false
