DO $$
DECLARE
    table_name text;
    tables text[] := ARRAY['lotes', 'lotes_entidades', 'lotes_movimentacoes'];
BEGIN
    FOREACH table_name IN ARRAY tables LOOP
        EXECUTE format('
            ALTER TABLE %I
            ADD COLUMN IF NOT EXISTS data_cadastro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            ADD COLUMN IF NOT EXISTS block_filial_id INTEGER DEFAULT 1,
            ADD COLUMN IF NOT EXISTS block_usuario_id INTEGER,
            ADD COLUMN IF NOT EXISTS usuario_cadastro INTEGER,
            ADD COLUMN IF NOT EXISTS data_atualizacao TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            ADD COLUMN IF NOT EXISTS usuario_atualizacao INTEGER,
            ADD COLUMN IF NOT EXISTS block_data TIMESTAMP,
            ADD COLUMN IF NOT EXISTS block_quem INTEGER,
            ADD COLUMN IF NOT EXISTS block INTEGER DEFAULT 1,
            ADD COLUMN IF NOT EXISTS block_empresa_id INTEGER DEFAULT 1;
        ', table_name);
    END LOOP;
END
$$;
