Code to Clone a DB [was OpenBase to MySQL]
Code to Clone a DB [was OpenBase to MySQL]
- Subject: Code to Clone a DB [was OpenBase to MySQL]
- From: Owen McKerrow <email@hidden>
- Date: Wed, 5 Oct 2005 13:11:16 +1000
I figured I should post this to the list after all. I can't remember
who sent me this in the first place (THANKYOU however you are ) and I
hope they don't mind me sending it to the list. We have a couple of
apps which need to clone data from a central Oracle database to our
Openbase ones. This way we can play with the data and make it all
dirty, which is something we wouldn't be allowed to do if we just
used the main source all the time. So we have this run on a 24 hr
cron job, to kick of the app.
public Application() {
super();
System.out.println("Welcome to " + this.name() + "!");
/* ** Put your application initialization code here ** */
ec = new EOEditingContext();
//We keep a log of what happens.
theLog = (NightlyUpdateLog)
EOUtilities.createAndInsertInstance(ec,"NightlyUpdateLog");
theLog.setTimeStart(new NSTimestamp());
cloneDatabase();
//This is were we play with the data as well as we do other
things in here but I've deleted them for this example
theLog.setTimeEnd(new NSTimestamp());
ec.saveChanges();
System.out.println("Finished Syncing");
System.exit(1);
}
private void cloneDatabase()
{
System.out.println("cloneDatabase : ");
try {
// Name of any entity in source database so we can
identify the source model
//Same entity in 2 different models, one points to the
source DB and the other the one we want to clone to.
String sourceEntityName = "StaffTable_ITS";
String sourceModelSufix = "_ITS";
String sql;
//Create and editing context and get info on the model
and through the entities name
EOEntity sourceEntity = EOUtilities.entityNamed(ec,
sourceEntityName);
EOModel sourceModel = sourceEntity.model();
NSArray entityNames = sourceModel.entityNames();
ec.lock();
//Delete the old data
//This may be a bad way of doing it, but this little
statement dumps all the data from the last time the clone ran.
sql = "DELETE EDU_STAFF_VW";
NSArray temp = EOUtilities.rawRowsForSQL
(ec,"staffDB",sql,null);
//Enumerate through all the entities in the model.
Delete there contents then copy in the new data.
Enumeration entityEnumerator =
entityNames.objectEnumerator();
while (entityEnumerator.hasMoreElements())
{
String entityName = (String)
entityEnumerator.nextElement();
String aNewEntityName = entityName.substring
(0,sourceEntityName.length() - sourceModelSufix.length());
System.out.println("\ncopying " + entityName + "
into " + aNewEntityName);
NSArray recordList =
EOUtilities.objectsForEntityNamed(ec, entityName);
//Copy each row one at a time from the Central DB to
our clone DB
Enumeration anEnumerator =
recordList.objectEnumerator();
while (anEnumerator.hasMoreElements())
{
EOEnterpriseObject oldObject =
(EOEnterpriseObject)anEnumerator.nextElement();
//System.out.println("oldObject="+ oldObject);
NSDictionary aDictionary =
oldObject.valuesForKeys(oldObject.attributeKeys());
//System.out.println("aDictionary="+ aDictionary);
EOEnterpriseObject newObject =
EOUtilities.createAndInsertInstance(ec, aNewEntityName);
newObject.takeValuesFromDictionary(aDictionary);
}
}
ec.unlock();
ec.saveChanges();
} catch(Exception e) {
emailException(e);
}
}
On 05/10/2005, at 12:00 PM, Jake MacMullin wrote:
Owen,
I'm interested in this for migrating from Frontbase to MySQL. I'd
love to see your code sample.
Thanks,
Jake MacMullin
Owen McKerrow wrote:
One possible solution is to duplicate your model thats pointing
to openbase and then change it so its pointing to your MySQL
database, update the model and then clone the data across using
WebObjects.
If your intersted in this, contact me off list and I can give you
some code to do the clone in one easy step. Someone here on the
list gave me the code to begin with, but I can't remeber who that
was.
owen
On 05/10/2005, at 11:18 AM, Philip Thompson wrote:
Hi all.
Is there an easy way to convert OpenBase to MySQL? If so, may
you enlighten me? I have searched the archives, but have not
found a lot of useful information.
Thanks in advance,
~Philip
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40uow.edu.au
This email sent to email@hidden
Owen McKerrow
WebMaster, emlab
http://emlab.uow.edu.au
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
'The test of a first-rate intelligence is the ability to hold
two opposed ideas in the mind at the same time and still be able
to function.'
-F.Scott Fitzgerald,
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
Owen McKerrow
WebMaster, emlab
http://emlab.uow.edu.au
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -
"I like the way this project has somehow, against all common sense,
got itself made."
- Peter Jackson, "The Lord of The Rings"
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden