Monday, September 26, 2016

Gluster on Debian

Remote all installation and config of gluster
apt-get purge --auto-remove glusterfs-server

Install gluster on servers, (server1, server2)
apt-get install glusterfs-server

On server1
gluster peer probe server2

On server2
gluster peer probe server1

On server1
gluster volume create gv0 replica 2 server1:/path/to/folderserver1 server2:/path/to/folderserver2

gluster volume start gv0 

On server2
mount -t glusterfs server2:/gv0 /path/to/sharedir 

sharedir is represent for broadcast to 2 folderserver1 and folderserver2, data store in sharedir is replicated to folderserver1 and folderserver2

Add column to all tables same schema

CREATE OR REPLACE FUNCTION add_version_column_to_all_tables()
RETURNS VOID
AS $$
DECLARE
 my_row RECORD;
BEGIN
 FOR my_row IN
 SELECT *
 FROM information_schema.tables
 WHERE table_schema = 'public' AND table_type='BASE TABLE'
 LOOP
 IF NOT EXISTS
 (
 SELECT attname FROM pg_attribute WHERE attrelid =
 (SELECT oid FROM pg_class WHERE relname = my_row.table_name )
 AND attname = 'subtenant'
 )
 THEN
     IF EXISTS
     (
         SELECT attname FROM pg_attribute WHERE attrelid =
         (SELECT oid FROM pg_class WHERE relname = my_row.table_name )
         AND attname = 'tenantid'
     )
     THEN
         EXECUTE('ALTER TABLE ' || my_row.table_name || ' ADD COLUMN subtenant text;');
         EXECUTE('UPDATE ' || my_row.table_name || ' SET subtenant = tenantid;');
     END IF;
 END IF;
 END LOOP;
END
$$
LANGUAGE plpgsql;

SELECT add_version_column_to_all_tables();