Commits

Tuukka Norri committed e4181f9

Upgrading the schema should now work
- Schema versions are also compared on connect.

  • Participants
  • Parent commits 062c759
  • Branches PGTS-NG

Comments (0)

Files changed (3)

File BaseTenAssistant/BaseTenAssistant.xcodeproj/project.pbxproj

 			children = (
 				531517560AFA55FB003FB616 /* View Classes */,
 				080E96DDFE201D6D7F000001 /* Controllers */,
-				531517570AFA5612003FB616 /* Interfaces */,
 				29B97315FDCFA39411CA2CEA /* Other Sources */,
 				29B97317FDCFA39411CA2CEA /* Resources */,
 				29B97323FDCFA39411CA2CEA /* Frameworks */,
 			name = "View Classes";
 			sourceTree = "<group>";
 		};
-		531517570AFA5612003FB616 /* Interfaces */ = {
-			isa = PBXGroup;
-			children = (
-			);
-			name = Interfaces;
-			sourceTree = "<group>";
-		};
 		5388DCD30E23AE22006CDBD8 /* Products */ = {
 			isa = PBXGroup;
 			children = (

File BaseTenAssistant/Source/BXAController.h

 	BXAImportController* mImportController;
 	BXDataModelCompiler* mCompiler;
 	BXPGSQLScriptReader* mReader;
+	NSNumber* mBundledSchemaVersionNumber;
 	
 	RKRegex* mCompilationErrorRegex;
 	RKRegex* mCompilationFailedRegex;

File BaseTenAssistant/Source/BXAController.m

 {
 	gController = self;
 	mLastSelectedEntityWasView = YES;
+	mBundledSchemaVersionNumber = BXACopyBundledVersionNumber ();
 
 	mReader = [[BXPGSQLScriptReader alloc] init];
 	[mReader setDelegate: self];
 	}
 }
 
+- (void) finishUpgrading: (BOOL) status
+{
+	PGTSConnection* connection = [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] connection];
+
+	if (status)
+	{
+		PGTSResultSet* res = nil;
+		res = [connection executeQuery: @"INSERT INTO baseten.viewprimarykey SELECT * FROM baseten_viewprimarykey"];
+		if (! [res querySucceeded])
+			[NSApp presentError: [res error]];
+		else
+		{
+			res = [connection executeQuery: @"SELECT baseten.prepareformodificationobserving (oid) FROM baseten_enabledoids"];
+			if (! [res querySucceeded])
+				[NSApp presentError: [res error]];
+		}
+	}
+	
+	//Clean up.
+	[connection executeQuery: @"DROP TABLE baseten_viewprimarykey"];
+	[connection executeQuery: @"DROP TABLE baseten_enabledoids"];
+}
+
+- (void) upgradeBaseTenSchema
+{
+	PGTSConnection* connection = [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] connection];
+	[connection executeQuery: @"CREATE TEMPORARY TABLE baseten_viewprimarykey AS SELECT * FROM baseten.viewprimarykey"];
+	NSString* query = 
+	@"CREATE TEMPORARY TABLE baseten_enabledoids AS "
+	@" SELECT oid FROM pg_class WHERE baseten.isobservingcompatible (oid) = true";
+	[connection executeQuery: query];
+	
+	[self installBaseTenSchema: MakeInvocation (self, @selector (finishUpgrading:))];
+}
+
 - (void) continueImport
 {
 	NSOpenPanel* openPanel = [NSOpenPanel openPanel];
 
 - (BOOL) canUpgradeSchema
 {
-	//FIXME: make me work.
-	return NO;
+	BOOL retval = NO;
+	BXPGDatabaseDescription* db = [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] databaseDescription];
+	NSNumber* currentVersion = [db schemaVersion];
+	if (currentVersion && NSOrderedDescending == [mBundledSchemaVersionNumber compare: currentVersion])
+		retval = YES;
+	return retval;
 }
 
 - (BOOL) canRemoveSchema
 
 
 @implementation BXAController (Delegation)
+- (void) alertDidEnd: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) ctx
+{
+	[NSApp stopModalWithCode: returnCode];
+}
+
 - (void) dataModelCompiler: (BXDataModelCompiler *) compiler finished: (int) exitStatus errorOutput: (NSFileHandle *) handle
 {
 	if (0 == exitStatus)
 	
 	BXPGInterface* interface = (id) [mContext databaseInterface];
 	[mReader setConnection: [[interface transactionHandler] connection]];
-	[self checkBaseTenSchema: NULL];
+	
+	if ([self checkBaseTenSchema: NULL] && [self canUpgradeSchema])
+	{
+		NSString* message = @"The installed schema has older version than the one bundlend with this application. Would you like to upgrade the database?";
+		NSAlert* alert = [NSAlert alertWithMessageText: @"Upgrade BaseTen schema?" 
+										 defaultButton: @"Upgrade"
+									   alternateButton: @"Don't upgrade"
+										   otherButton: nil 
+							 informativeTextWithFormat: message];
+		[alert beginSheetModalForWindow: mMainWindow modalDelegate: self 
+						 didEndSelector: @selector (alertDidEnd:returnCode:contextInfo:) contextInfo: NULL];
+		NSInteger returnCode = [NSApp runModalForWindow: mMainWindow];
+		
+		if (NSAlertDefaultReturn == returnCode)
+			[self upgradeBaseTenSchema];
+	}
 }
 
 
 static void
 InvokeRecoveryInvocation (NSInvocation* recoveryInvocation, BOOL status)
 {
-	[recoveryInvocation setArgument: &status atIndex: 2];
-	[recoveryInvocation invoke];
+	if (recoveryInvocation)
+	{
+		[recoveryInvocation setArgument: &status atIndex: 2];
+		[recoveryInvocation invoke];
+	}
 }
 
 - (void) SQLScriptReaderSucceeded: (BXPGSQLScriptReader *) reader userInfo: (id) userInfo
 
 - (IBAction) upgradeSchema: (id) sender
 {
-	//FIXME: make me work.
+	[self upgradeBaseTenSchema];
 }
 
 - (IBAction) removeSchema: (id) sender