This project has moved and is read-only. For the latest updates, please go here.

Saving Schema to disk

Mar 8, 2011 at 5:02 AM

Is there a way to save a database schema to disk?

Mar 8, 2011 at 7:41 AM
Edited Mar 8, 2011 at 10:05 AM

Seems I missed a few [Serializable] attributes on some of the schema classes (function, package, trigger, sequence and resultset).

But when those are in (I'll commit later today), a binary serialization works fine...

            var f = new BinaryFormatter();
            using (var stm = new FileStream("schema.bin", FileMode.Create))
            {
                f.Serialize(stm, schema);
            }

            DatabaseSchema clone;
            using (var stm = new FileStream("schema.bin", FileMode.Open))
            {
                clone = (DatabaseSchema)f.Deserialize(stm);
            }
An XmlSerializer won't cope because of the List<T>s and parameterized constructors, but it wouldn't be much effort to change to support it...

[Edit] No, you can't use XmlSerializer because there are circular references. I got DataContractSerializer to work (as long as you use  preserveObjectReferences). An example unit test will be in the next commit

Mar 8, 2011 at 2:07 PM

Martin,

Yeah, I tried saving as a binary file and got errors. I downloaded your code and saw where you were missing a few [serialize] attributes, but I didn’t spend the time looking at all of the classes to see which ones needed to be fixed. That’s when I decided to write you.

I might ask that when you’re in there fixing the serialize attributes, can you make the schema classes ‘partial’ classes so someone can extend the classes as needed?

Saving as binary is great, xml would be good to, but I personally don’t need it (not now anyway).

-Ron Anderson

From: martinjw [email removed]
Sent: Monday, March 07, 2011 11:42 PM
To: randerson1001@hotmail.com
Subject: Re: Saving Schema to disk [dbschemareader:248807]

From: martinjw

Seems I missed a few [Serializable] attributes on some of the schema classes (function, package, trigger, sequence and resultset).

But when those are in (I'll commit later today), a binary serialization works fine...

var f = new BinaryFormatter();
using (var stm = new FileStream("schema.bin", FileMode.Create))
{
f.Serialize(stm, schema);
}

DatabaseSchema clone;
using (var stm = new FileStream("schema.bin", FileMode.Open))
{
clone = (DatabaseSchema)f.Deserialize(stm);
}
An XmlSerializer won't cope because of the List<T>s and parameterized constructors, but it wouldn't be much effort to change to support it...

Mar 8, 2011 at 6:50 PM

The current source is now Binary serializable, as well as DataContract serializable.

There's some API changes (mainly to stop exposing settable List<T>). Ideally these should be interfaces (IList<IDatabaseColumn>) but that really will break serialization.

I'll probably add partial in the next commit.

Mar 10, 2011 at 5:22 PM
Thanks for the quick turnaround.



Sent from my Windows Phone

From: martinjw
Sent: Tuesday, March 08, 2011 10:50 AM
To: randerson1001@hotmail.com
Subject: Re: Saving Schema to disk [dbschemareader:248807]



> From: martinjw
>
> The current source is now Binary serializable, as well as DataContract serializable.There's some API changes (mainly to stop exposing settable List<T>). Ideally these should be interfaces (IList<IDatabaseColumn>) but that really will break serialization.I'll probably add partial in the next commit.
>
>