2023年5月9日火曜日

Postgres への移行

tinyint →  int
longint → bigint
decimal → numeric( p, s )
engine → カット
comment → カット
char(32) → uuid
decimal(13.0) →
longtext → text
datetime  →   timestamp



【自動時刻更新】
create table  xxxxx {
date_update timestamp not null default current_timestamp,
xxxx
 }
create or replace function trigger_set_timestamp() returns trigger as $$
 begin
     new.updated_at = noW();
     return new; end;
 $$    language plpgsql;                      

【insert or update】
INSERT INTO {tabale} ( {colmuns} ) VALUES ({values}) 
ON DUPLICATE KEY UPDATE {updates}
INSERT INTO {table} VALUES ({values})
ON CONFLICT ON CONSTRAINT {table}_primary
DO UPDATE SET {updates};

【大文字テーブル名】
 大文字の入ったテーブル名は、“”で囲む必要がある。(囲まない小文字としてみなすよう)

【distinct 】
()が必要。

【カレンダ出力】

        #DEBUG
        if DB_SYSTEM == 'postgres':
            WITH_RECURSIVE_DATE_TABALE = f"""
              with recursive date_table (date_value) AS (
              select
                 (select date_trunc( 'month', current_date + interval '-{str(before)} month' + '1 Day'))
                  union all select  date_value + interval '1 Day'
                  from date_table
                 where date_value < (select date_trunc('month', current_date) + '{str(before + 1)} month' +'-1 Day')
              )"""
            TO_CHAR_DATE_VALUE = """to_char(date_value, 'YYYYmmdd')"""


        else:
          WITH_RECURSIVE_DATE_TABALE = f"""
            with recursive date_table(date_value) as (
              select
                (select date_format(adddate(curdate(), interval -{str(before)} month), '%Y-%m-01'))
              union all
              select
                date_add(date_value, interval 1 day)
              from  date_table
              where
                date_value < (select last_day(adddate(curdate(), interval -{str(before)} month)))
            )"""
          TO_CHAR_DATE_VALUE = """date_format(date_value, '%Y%m%d')"""

【date_format】

 TO_CHAR_DATE_VALUE = """to_char(date_value, 'YYYYmmdd')"""
  TO_CHAR_DATE_VALUE = """date_format(date_value, '%Y%m%d')"""

【ifnull】
ifnull(gross_amt,0)  
COALESCE(gross_amt,0) 


【uuid】 
                           program    sql                      DBフィールド型
mysql               UUID           UUID.hex        char(32)
postgres          UUID           str( UUID)       uuid

uuidをhexに変換してSQLに埋め込む。
uuid1をstrに新刊してSQLに埋め込む。























シャットダウン時の後処理 (shutdown)

# vi /etc/systemd/system/drop.service [Unit] Description= stop httpgwd DefaultDependencies=no Before=shutdown.target RefuseManualStart=true ...