drop table if exists user_log cascade;
create table user_log(
	user_log_id serial not null,
	ctstamp timestamp without time zone not null default now(),
	user_log_type_id integer not null references user_log_types( user_log_type_id ),
	user_log_subtype_id integer not null,
	user_id integer not null references users( user_id ),
	member_id integer references members( member_id ),
	is_system boolean not null default false,
	is_admin boolean not null default false,
	is_user boolean not null default false,
	user_log_ipaddress varchar(16) not null,
	user_log_foreign_id varchar(128),
	user_log_note text,
	constraint user_log_pkey primary key( user_log_id )
);

comment on table user_log is 'storage of user log';
comment on column user_log.user_log_id is 'unique id of table';
comment on column user_log.ctstamp is 'creational timestamp of user';
comment on column user_log.user_log_type_id is 'reference id to user_log_types table';
comment on column user_log.user_log_subtype_id is 'subtype of log_type (lower 8 bits)';
comment on column user_log.user_id is 'reference id to users table (mandatory)';
comment on column user_log.member_id is 'reference id to members table (optional)';
comment on column user_log.is_system is 'is visible by system';
comment on column user_log.is_admin is 'is visible by admin';
comment on column user_log.is_user is 'is visible by user';
comment on column user_log.user_log_ipaddress is 'ipaddress of user in the moment of logging';
comment on column user_log.user_log_foreign_id is 'foreign id of log entry (bank account number, invoice reference number, etc)';
comment on column user_log.user_log_note is 'the note itself';

comment on constraint user_log_pkey on user_log is 'unique constraint of table';
comment on constraint user_log_user_id_fkey on user_log is 'foreign constraint to users table';
comment on constraint user_log_member_id_fkey on user_log is 'foreign constraint to members table';
comment on constraint user_log_user_log_type_id_fkey on user_log is 'foreign id to user_log_types table';
