PostgreSQL - Add a Column to All Tables in the Database Unless it Exists

Uncategorized 2 Comments »

To be able to use NHibernate – and as a general purpose and learning exercise –  I wanted to write a script that adds a version column of type integer to all tables in the database, unless such column exists (in Rails the schema_migrations table has a version column for example).

In SQL Server we have the “secret” sproc sp_MSforeachtable for this purpose, but how to bend the stricter syntax of plpgslq to achieve something similar? After a number of hours of trial and error, here’s is a script that actually appears to work, at least with PostgreSQL 9.0 and probably earlier versions:

CREATE OR REPLACE FUNCTION add_version_column_to_all_tables()
RETURNS VOID
AS $$
DECLARE
    my_row    RECORD;
BEGIN      
    FOR my_row IN
        SELECT table_name
        FROM   information_schema.tables
        WHERE  table_schema = ‘public’
       
    LOOP
    IF NOT EXISTS
    (
    SELECT attname FROM pg_attribute WHERE attrelid =
    (SELECT oid FROM pg_class WHERE relname =  my_row.table_name )
     AND attname = ‘version’
     )
     THEN
        EXECUTE(’ALTER TABLE ‘ || my_row.table_name || ‘ ADD COLUMN version int NOT NULL DEFAULT 0;’);
        END IF;
    END LOOP;
END
$$
LANGUAGE plpgsql;

SELECT add_version_column_to_all_tables();

Im sure this script can be optimized, please post any improvements in the columns.

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Installation von LX-Office auf Ubuntu und mit Eigenem Port

ERP No Comments »

Update: Ich sah gerade diese Anleitung hier, die relativ neu und aktuell ist im Gegensatz zur veralteten Debian Anleitung. Der untenstende Eintrag kann vielleicht als Kondensat dienen oder als Hilfe zur Einrichtung eines VirtualHost mit eigenem Port.

Lx-Office ist ein komplettes und recht ausgereiftes Buchhaltungs und ERP-System für Linux, ein Fork von SQL Ledger.

Installation

Mit Firefox runterladen und mit Package-Manger installieren; oder shell:

wget http://sourceforge.net/projects/lx-office/files/Lx-Office%20ERP/2.6.1/lx-office-erp_2.6.1-0-all.deb

sudo dpkg -i lx-office-erp_2.6.1–0–all.deb

Wahrscheinlich git es einige fehlende Dependencies, die im unmittelbaren Anschluss nun mit

sudo apt-get install -f

installiert werden können.

Einstellungen für Apache

Mein Webserver ist mit Phusion Passenger für Ruby on Rails ausgestattet, die Perl Skripten von LX-Office benötigen also ihren eigenen VirtualHost mit eigenem Port.

Wer nicht Passenger/Rails fährt, braucht die Einstellungen betreffs VirtualHost nicht vorzunehmen, die Einrichtung eines eigenen Ports könnte aber mittelfristig ohnehin flexibler sein.

In /etc/apache2/ports.conf:

NameVirtualHost *:8080
Listen 8080

In /etc/apache2/sites-available/default ans Ende

<VirtualHost *:8080> 
 AddHandler cgi-script .pl
 Alias /lx-office-erp/ /usr/lib/lx-office-erp/
 <Directory /usr/lib/lx-office-erp>
  Order Deny,Allow
  Allow from All
  Options +ExecCGI +Includes +FollowSymlinks
 </Directory>
 <Directory /usr/lib/lx-office-erp/users>
  Order Deny,Allow
  Deny from All
 </Directory>
</VirtualHost>

Dann

sudo a2dissite

und

sudo a2ensite

und Apache Neustart.

Einstellungen für Postgresql

Wenn noch nicht passiert, PL/pgSQL in template1 installieren:

psql -U postgres template1

createlang plpgsql template1

In postgresql.conf

tcpip_socket = true

zu setzen funktioniert nicht mehr in Postgresql 8.1 +, es muss jetzt stattdessen

listen_addresses = ‘*’

gesetzt werden.

In pg_hba.conf

Ich hatte guten Erfolg mit

local all all trust
host all all 127.0.0.1 255.0.0.0 trust
host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff trust

am Ende der Datei wie in der Debian Anleitung empfohlen. Ansonsten ist diese Anleitung weitgehend veraltet.

Dann geht’s weiter mit

http://localhost:8080/lx-office-erp/admin.pl

und die Datenbank(en) werden initialisiert. Das ist eigentlich alles selbsterklärend, nur sollte man nicht vergessen, den angelegten Benutzer einer Gruppe mit Vollzugriff zuzuordnen.

YMMV

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Enabling gzip Compression with SOAP4R

Ruby on Rails, SOAP4R No Comments »

It’s a one-liner, but info isn’t easy to find (as usual with soap4r).

When you generate your local driver with for example

wsdl2ruby.rb --wsdl MyService.wsdl --classdef MyService --type client --force

and you instantiate your proxy driver like so

ms = MyService.new
ms.wiredump_dev = STDERR

you simply add

ms.streamhandler.accept_encoding_gzip = true

That’s all. No need for custom headers etc.

And: zlib (compression library used by soap4r) is built into Ruby since version 1.8. No need for any gems or other shenenigans.

Link List

As an added year-end bonus, here’s a list of links that helped me with SOAP and ruby over the years:

Excellent SAP article:

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/505ac48e-0578-2a10-60be-c5ffe14a0134

Correct usage of wsdl2ruby (must be wsdl2ruby.rb)

http://www.hostingrails.com/236/Using-Web-Services-Description-Language-wsdl2ruby-and-soap4r-as-a-clientI

ActionWebService:

http://www.datanoise.com/articles/2008/7/2/actionwebservice-is-back

Links:

http://www.brendonwilson.com/blog/2006/04/02/ruby-soap4r-wsdl-hell/

WSDL/Ruby:

http://codeidol.com/other/rubyckbk/Web-Services-and-Distributed-Programming/Using-a-WSDL-File-to-Make-SOAP-Calls-Easier/

Very Helpful Detailed Article:

http://www.ibm.com/developerworks/opensource/library/os-ws-rubyrails/index.html

xsltproc examples:

http://linux.byexamples.com/archives/463/xslt-processor-command-line/

xsd2wsdl:

http://jira.springframework.org/secure/attachment/12306/XsdToWsdl.xsl

linked from here:

http://jira.springframework.org/browse/SWS-79

Don’t forget to enable wiredump:

fe.wiredump_dev = STDERR

Get rid of NS1:

http://www.pluitsolutions.com/2007/08/10/remove-n1-namespace-for-soap4r/

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

script/runner with Debugger in Rails

Ruby on Rails No Comments »
script/runner myscript.rb --debugger

won’t work, so you have to use

rdebug script/runner myscript.rb

That’s all, folks! Merry Christmas!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Prepend to javascript_include_tag :defaults

JavaScript, Ruby on Rails No Comments »

Yes, there is register_javascript_include_default but it will only append to your sources.

To prepend (I’ll use this to have the excellent JS.Class libraries loaded before anything else), proceed like so:

While in Rails 3, you can set

config.action_view.javascript_expansions[:defaults].unshift(’my_prepend_1′, my_prepend_2′)

in your application.rb file,

in Rails 2.3.x you create an initializer (e.g. config/initializers/my_js_defaults.rb) and reset the JAVASCRIPT_DEFAULT_SOURCES constant as so:

module ActionView::Helpers::AssetTagHelper
  JAVASCRIPT_DEFAULT_SOURCES = (remove_const :JAVASCRIPT_DEFAULT_SOURCES).unshift(’my_prepend_1′, ‘my_prepend_2′)
   reset_javascript_include_default
end

This post gave me the idea. It concerns Rails 3 (before the above config.action_view.javascript_expansions which was introduced in Rails 3 RC)  but the code also works with Rails 2.3.x.

 

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in