CREATE TABLE pbx_organizacao
(
  id serial NOT NULL PRIMARY KEY,
  nome VARCHAR(128) NOT NULL,
  status INTEGER NOT NULL DEFAULT 1,
  master int not null DEFAULT 0, 
  updated_at timestamp without time zone,
  created_at timestamp without time zone NOT NULL DEFAULT now()
);

CREATE TABLE pbx_organizacao_usuarios
(
	id SERIAL not null primary key,
	id_usuario INTEGER not null,
	id_organizacao INTEGER not null,
	updated_at timestamp without time zone,
	created_at timestamp without time zone NOT NULL DEFAULT now()
);

ALTER TABLE pbx_organizacao_usuarios ADD CONSTRAINT id_usuario FOREIGN KEY (id_usuario)
REFERENCES pbx_usuarios(id) ON DELETE CASCADE;

ALTER TABLE pbx_organizacao_usuarios ADD CONSTRAINT id_organizacao FOREIGN KEY (id_organizacao)
REFERENCES pbx_organizacao(id) ON DELETE CASCADE;


--create temporary table tpbx_facilidades as select * from pbx_facilidades where id = 1;
--alter table tpbx_facilidades drop column id;
--insert into pbx_facilidades select * from tpbx_facilidades;
-- update from pbx_facilidades set org_id = $org_id where id = $id

create temporary table tpbx_features_featuremap as select * from pbx_features_featuremap where id = 1;
alter table tpbx_features_featuremap drop column id;
insert into pbx_features_featuremap select * from tpbx_features_featuremap;
update pbx_features_featuremap set org_id = $org_id where id = $id

create temporary table tpbx_features_general as select * from pbx_features_general where id = 1;
alter table tpbx_features_general drop column id;
insert into pbx_features_general select * from tpbx_features_general;
update pbx_features_general set org_id = $org_id where id = $id

create temporary table tpbx_iax_general as select * from pbx_iax_general where id = 1;
alter table tpbx_iax_general drop column id;
insert into pbx_iax_general select * from tpbx_iax_general;
update pbx_iax_general set org_id = 2 where id = 4


------------------------------------------
-----    ADICAO DE ID NAS TABELAS    -----
------------------------------------------
ALTER TABLE pbx_sip_general ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE pbx_iax_general ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE pbx_facilidades ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE pbx_features_general ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE pbx_features_featuremap ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE pbx_voicemail_general ADD COLUMN id SERIAL PRIMARY KEY;
