Re: Best way to change column/property type?

Brian Kelley <[email protected]>
Newsgroups gmane.comp.db.metakit
Message-ID <[email protected]>
You are on the right track, you will need to have a dummy column but
you need to commit the database close the database and then reload at
every major step.  here is a small script that you can test with.

I don't think that there is an efficient way of doing this, but here
is A way of doing it :)

import metakit, os

# changes table.b from str to int

# make some data to test
if os.path.exists("foo.mk"): os.remove("foo.mk")
st = metakit.storage("foo.mk", 1)
vw = st.getas("test[a,b,c]")
vw.append(('a','1','c'))
vw.append(('d','2','f'))
st.commit()
del st

print "converting b to newb"
st = metakit.storage("foo.mk", 1)
vw = st.getas("test[a,b,c,newb:I]")
for row in vw:
    row.newb = int(row.b)

# drop the 'b' column
vw = st.getas("test[a,c,newb:I]")
st.commit()

del st

st = metakit.storage("foo.mk", 1)
# add the b column back as an integer
vw = st.getas("test[a,b:I,c,newb:I]")
for row in vw:
    row.b = row.newb
# drop the newb column!
vw = st.getas("test[a,b:I,c]")
st.commit()

del st

# all done, now test
st = metakit.storage("foo.mk", 1)
vw = st.view("test")
metakit.dump(vw)

# you should
st.save(open("foo2.mk", "wb"))
# as this optimizes the on-disk representation
_____________________________________________
Metakit mailing list  -  [email protected]
http://www.equi4.com/mailman/listinfo/metakit
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.