I have a CheckDBVersion method that runs every time my app starts. I store a DBVersion setting in the database. When my app starts it checks the DBVersion against the version the app expects. If the DBVerion is newer than expected a warning is displayed and the app closes. If the DB is older then schema changes are executed to update the database.
Here is my entire method. (PostgreSQL)
[code]Sub CheckMainDBVersion()
Dim ProgramDBVersion as Integer = 7
Dim DBVersion as Integer = SettingGlobal(“DBVersion”).IntegerValue
if ProgramDBVersion < DBVersion Then
MsgBoxT “This version of QBMS is not compatible with this version of the database.”
quit
end if
if DBVersion < 1 Then
MainDB.SQLExecute(“ALTER TABLE QBItems ADD COLUMN SubItem TEXT”)
end if
if DBVersion < 2 Then
'create trigger to notify server about orders to Import
MainDB.SQLExecute(" " _
+ "CREATE OR REPLACE FUNCTION notify_orderchanged() " _
+ "RETURNS trigger " _
+ "LANGUAGE plpgsql " _
+ "AS $function$ " _
+ "DECLARE " _
+ "BEGIN " _
+ "PERFORM pg_notify(CAST(‘server’ as text), ‘OrderChanges,’ || NEW.id::text || ‘,’ || NEW.savestatus::text ) " _
+ "WHERE NEW.savestatus = ‘1’ or NEW.savestatus = ‘2’ or NEW.savestatus = ‘3’ or NEW.savestatus = ‘9’; " _
+ "RETURN NEW; " _
+ “END;” _
+ "$function$; " _
+ “” _
+ "DROP TRIGGER IF EXISTS notify_orderchanged on orders ; " _
+ “CREATE TRIGGER notify_orderchanged AFTER INSERT OR UPDATE ON Orders FOR EACH ROW EXECUTE PROCEDURE notify_orderchanged();”)
MainDB.SQLExecute(“ALTER TABLE Panels ADD COLUMN ManualComplete BOOLEAN”)
MainDB.SQLExecute(“ALTER TABLE QBInventory ADD COLUMN AvgCost NUMERIC”)
end if
if DBVersion < 3 Then
MainDB.SQLExecute(“ALTER TABLE Customers ADD COLUMN Phone TEXT”)
MainDB.SQLExecute(“ALTER TABLE Orders ADD COLUMN Phone TEXT”)
MainDB.SQLExecute(“ALTER TABLE OrderLines ALTER COLUMN Quantity TYPE NUMERIC”)
end if
if DBVersion < 4 Then
MainDB.SQLExecute(“ALTER TABLE Orders ADD COLUMN ShipIdx INTEGER DEFAULT 0”)
end if
if DBVersion < 5 Then
'create table to store inventory assembly builds. Also create trigger to notify server about new build to import to QB.
MainDB.SQLExecute("" _
+ " CREATE TABLE inventorybuilds(ID SERIAL, PID INTEGER,ITEM TEXT, QTY NUMERIC, status TEXT DEFAUlT ‘new’, TS Timestamp default now()); " _
+ " CREATE OR REPLACE FUNCTION notify_newinventorybuild() " _
+ " RETURNS trigger " _
+ " LANGUAGE plpgsql AS $function$ " _
+ " DECLARE BEGIN PERFORM pg_notify(CAST(‘server’ as text), ‘NewBuild,’ || NEW.id::text) WHERE NEW.status = ‘new’ ; " _
+ " RETURN NEW; END;$function$; " _
+ " CREATE TRIGGER notify_newinventorybuild AFTER INSERT OR UPDATE ON inventorybuilds FOR EACH ROW EXECUTE PROCEDURE notify_newinventorybuild();")
end if
if DBVersion < 6 Then
MainDB.SQLExecute(“ALTER TABLE ProductionLog ALTER COLUMN TimeStamp TYPE TIMESTAMP;” _
+ “ALTER TABLE inventorybuilds ALTER COLUMN TS SET DEFAULT now();” _
+ “ALTER TABLE Orders ADD COLUMN BundleInfo TEXT;”)
end if
If DBVersion < 7 Then
'create trigger to notify server about new messages
MainDB.SQLExecute(" " _
+ "DROP TABLE IF EXISTS messages; "_
+ "CREATE TABLE messages(id SERIAL, user1 Text, user2 Text, status1 Integer, status2 Integer, msg Text, ts Timestamp Without Time Zone DEFAULT now(),PRIMARY KEY (id)); " _
+ "CREATE OR REPLACE FUNCTION notify_newmsg() " _
+ "RETURNS trigger " _
+ "LANGUAGE plpgsql AS $function$ " _
+ "DECLARE BEGIN PERFORM pg_notify(CAST(‘server’ as text), ‘NewMessage,’ || NEW.id::text) WHERE NEW.status1 = 3 OR NEW.status2 = 3; " _
+ "RETURN NEW; END;$function$; " _
+ “CREATE TRIGGER notify_newmsg AFTER INSERT OR UPDATE ON messages FOR EACH ROW EXECUTE PROCEDURE notify_newmsg();” _
+ “ALTER TABLE Log ALTER COLUMN Time TYPE TIMESTAMP;”)
End If
'update the version of the MainDB
SettingGlobal(“DBVersion”) = ProgramDBVersion
End Sub
[/code]