[Grok-dev] megrok.rdb, Oracle, and views.

Jeffrey D Peterson bgpete at gmail.com
Thu Jan 14 17:18:49 EST 2010



> -----Original Message-----
> From: Jeffrey D Peterson [mailto:bgpete at gmail.com]
> Sent: Thursday, January 14, 2010 10:53 AM
> To: 'Martijn Faassen'; 'grok-dev at zope.org'
> Subject: RE: [Grok-dev] megrok.rdb, Oracle, and views.
> 
> > A bit I'm confused about is that you say SA can't reflect views but
> > you're still using rdb.reflected().
> >
> > If we had a spelling for rdb.dontcreate() (rdb.evolve is way too cute
> > in
> > my mind - and confusing, as people will think about schema
> evolution),
> > would that be sufficient for you to stop using rdb.reflected() or
> would
> > you still need it?
> >
> > Concerning spelling, I see you use 'useexisting', which is a
> SQLAlchemy
> > feature, correct? Perhaps the spelling should be 'rdb.useexisting'.
> In
> > this case it'll always try to use an existing table and never try to
> > create it.
> 
> Ok, more insight...
> 
> We actually have a few things going on depending on implementation:
> 
> With Tables (w/PK):
> 
> 	rdb.reflected() is as you expect.
> 
> 	When overriding:
> 
> 		rdb.reflected() and useexisting are required when not
> overriding all columns
> 		neither is required if you do
> 
> 	create_all is able to check the DB and find the table and not try
> and create it.
> 
> 
> With Tables (w/o/PK):
> 
> 	rdb.reflected() fails complaining about PK, requiring you to
> override.
> 
> 	When overriding:
> 
> 		rdb.reflected() and useexisting are required when not
> overriding all columns
> 		neither is required if you do
> 
> 	create_all is able to check the DB and find the table and not try
> and create it.
> 
> 
> The patterns are similar if you are overriding just because you want to
> (regardless of PK)
> 
> 
> With Views:
> 
> 	rdb.reflected() fails complaining about PK, requiring you to
> override.
> 
> 	When overriding:
> 
> 		rdb.reflected() and useexisting are required when not
> overriding all columns
> 		neither is required if you do
> 
> 
> 	apparently when create_all checks for existing it cannot identify
> the view, therefore it tries to create it requiring it to be pulled
> from the metadata.
> 
> 
> So my statement about the PK issue is true but only part of the story.
> 
> That being said use_existing may not be what we want either, maybe we
> want rdb.override() and possibly rdb.override_view() and maybe those
> take a Boolean argument full which when True skips rdb.reflected() and
> use_existing, but when False includes them?  Maybe defaulting to False
> as it seems to me you'd want to reflect as much as possible.
> 
> Thoughts?

I was able to garner the same results and eliminate the need to also call
rdb.reflected() or pass useexisting at the class level.

By creating rdb.override()


__init__.py:

5,6c5,6
< from megrok.rdb.directive import (key, metadata, tablename, reflected,
override,
<                                   table, tableargs, polymorphic_on,
inherits,
---
> from megrok.rdb.directive import (key, metadata, tablename, reflected,
table,
>                                   tableargs, polymorphic_on, inherits,


directive.py:

24,31d23
< class override(Directive):
<     scope = CLASS
<     store = ONCE
<     default = None
<
<     def factory(self, **kw):
<         return kw or dict(reflect=True, create=False)
<



meta.py:

16d15
<     martian.directive(rdb.override)
23,26c22,24
<     def execute(self, class_, tablename, metadata, reflected, override,
<                 table, tableargs, inherits, polymorphic_identity,
<                 polymorphic_on, **kw):
<
---
>     def execute(self, class_, tablename, metadata, reflected, table,
>                 tableargs, inherits, polymorphic_identity, polymorphic_on,
>                 **kw):
31,51d28
<
<
<         #overridden table or view
<         if override is not None:
<
<             if override.get('reflect', True):
<                 #reflect some of the columns if True
<                 reflected = True
<                 ue = dict(useexisting=True)
<                 if tableargs is not None:
<                     tableargs.update(ue)
<                 else:
<                     tableargs = ue
<
<             if not override.get('create', False):
<                 #do not create the table
<                 if not hasattr(metadata, '_use_existing_registry'):
<                     metadata._use_existing_registry = []
<
<                 metadata._use_existing_registry.append(class_)
<


setup.py:


91,94d90
<
<     Skip creation of any tables or views that have been overridden with
the
<     'create' flag set to False.  These are listed in
_use_existing_registry
<     following previous convention.
97,99d92
<     if hasattr(metadata, '_use_existing_registry'):
<         for table in metadata._use_existing_registry:
<             metadata.remove(table.__table__)


Regards,
 
Jeff Peterson
Webmaster
Crary Industries



More information about the Grok-dev mailing list