def _populateSchema(pathObj=None): """ Generate the global L{SchemaSyntax}. """ if pathObj is None: pathObj = _schemaFiles()[0] return SchemaSyntax(schemaFromPath(pathObj))
def test_translateSingleUnique(self): """ L{_translateSchema} translates single-column 'unique' statements inline. """ self.assertSortaEquals( self.translated( SchemaSyntax( self.schemaFromString( "create table alpha (beta integer unique)"))), 'create table alpha ( "beta" integer unique );')
def test_translateSingleTableUnique(self): """ L{_translateSchema} translates single-column 'unique' statements inline, even if they were originally at the table level. """ stx = SchemaSyntax( self.schemaFromString( "create table alpha (beta integer, unique (beta))")) self.assertSortaEquals( self.translated(stx), 'create table alpha ( "beta" integer unique );')
def test_translateSinglePrimaryKey(self): """ L{_translateSchema} translates single-column 'primary key' statements inline. """ self.assertSortaEquals( self.translated( SchemaSyntax( self.schemaFromString( "create table alpha (beta integer primary key)"))), 'create table alpha ( "beta" integer primary key );')
def test_anonymousCheckConstraint(self): """ Named 'check' constraints are propagated through translation without modification. """ self.assertSortaEquals( self.translated( SchemaSyntax( self.schemaFromString("create table alpha ( " 'beta integer, check (beta > 3)' " );"))), "create table alpha ( " '"beta" integer, check ("beta" > 3)' " );")
def test_multiTablePrimaryKey(self): """ L{_translateSchema} translates multi-column 'primary key' statements. """ stx = SchemaSyntax( self.schemaFromString( "create table alpha (" "beta integer, gamma text, primary key (beta, gamma))")) self.assertSortaEquals( self.translated(stx), 'create table alpha ( "beta" integer, "gamma" nclob, ' 'primary key ("beta", "gamma") );')
def test_primaryKeyUniqueOrdering(self): """ If a table specifies both a PRIMARY KEY and a UNIQUE constraint, the PRIMARY KEY will always be emitted first. """ stx = SchemaSyntax( self.schemaFromString( "create table alpha (" "beta integer, gamma text, delta integer, " "unique (beta, delta), primary key (beta, gamma))")) self.assertSortaEquals( self.translated(stx), 'create table alpha ( ' '"beta" integer, "gamma" nclob, "delta" integer, ' 'primary key ("beta", "gamma"), unique ("beta", "delta") );')
def test_youBrokeTheSchema(self): """ Oracle table names have a 30-character limit. Our schema translator simply truncates the names if necessary, but that means that you can't have tables whose first 30 characters differ. L{_translateSchema} raises a SchemaBroken() exception if this happens. (This test is to make sure L{test_schemaTranslation}, above, will actually fail if this happens.) """ # TODO: same thing for sequences. schema = self.schemaFromString(""" create table same_012345678012345678990123456789_1 (foo integer); create table same_012345678012345678990123456789_2 (bar text); """) self.assertRaises(SchemaBroken, self.translated, SchemaSyntax(schema))
sth.id = lambda: __name__ schemaString = """ create table ALPHA (BETA integer primary key, GAMMA text); create table DELTA (PHI integer primary key default (nextval('myseq')), EPSILON text not null, ZETA timestamp not null default '2012-12-12 12:12:12' ); """ # sqlite can be made to support nextval() as a function, but 'create sequence' # is syntax and can't. parseableSchemaString = """ create sequence myseq; """ + schemaString try: testSchema = SchemaSyntax(sth.schemaFromString(parseableSchemaString)) except SkipTest as e: Alpha = Delta = object skip = e else: Alpha = fromTable(testSchema.ALPHA) Delta = fromTable(testSchema.DELTA) skip = False class TestRecord(Record, Alpha): """ A sample test record. """
JobTable = Table(inSchema, "JOB") JobTable.addColumn("JOB_ID", SQLType("integer", None), default=Sequence(inSchema, "JOB_SEQ"), notNull=True, primaryKey=True) JobTable.addColumn("WORK_TYPE", SQLType("varchar", 255), notNull=True) JobTable.addColumn("PRIORITY", SQLType("integer", 0), default=0, notNull=True) JobTable.addColumn("WEIGHT", SQLType("integer", 0), default=0, notNull=True) JobTable.addColumn("NOT_BEFORE", SQLType("timestamp", None), notNull=True) JobTable.addColumn("IS_ASSIGNED", SQLType("integer", 0), default=0, notNull=True) JobTable.addColumn("ASSIGNED", SQLType("timestamp", None), default=None) JobTable.addColumn("OVERDUE", SQLType("timestamp", None), default=None) JobTable.addColumn("FAILED", SQLType("integer", 0), default=0, notNull=True) JobTable.addColumn("PAUSE", SQLType("integer", 0), default=0, notNull=True) return inSchema JobInfoSchema = SchemaSyntax(makeJobSchema(Schema(__file__))) class JobFailedError(Exception): """ A job failed to run - we need to be smart about clean up. """ def __init__(self, ex): self._ex = ex class JobTemporaryError(Exception): """ A job failed to run due to a temporary failure. We will get the job to run again after the specified interval (with a built-in back-off based on the number of failures also applied).
@param inSchema: a L{Schema} to add the locks table to. @type inSchema: L{Schema} @return: inSchema """ LockTable = Table(inSchema, "NAMED_LOCK") LockTable.addColumn("LOCK_NAME", SQLType("varchar", 255)) LockTable.tableConstraint(Constraint.NOT_NULL, ["LOCK_NAME"]) LockTable.tableConstraint(Constraint.UNIQUE, ["LOCK_NAME"]) LockTable.primaryKey = [LockTable.columnNamed("LOCK_NAME")] return inSchema LockSchema = SchemaSyntax(makeLockSchema(Schema(__file__))) class NamedLock(Record, fromTable(LockSchema.NAMED_LOCK)): """ An L{AcquiredLock} lock against a shared data store that the current process holds via the referenced transaction. """ @classmethod def acquire(cls, txn, name): """ Acquire a lock with the given name. @param name: The name of the lock to acquire. Against the same store, no two locks may be acquired. @type name: L{unicode}
A integer, B integer, DELETE_ON_LOAD integer default 0); create table DUMMY_WORK_DONE (WORK_ID integer primary key, A_PLUS_B integer); """) nodeSchema = SQL(""" create table NODE_INFO (HOSTNAME varchar(255) not null, PID integer not null, PORT integer not null, TIME timestamp default current_timestamp not null, primary key (HOSTNAME, PORT)); """) try: schema = SchemaSyntax(SimpleSchemaHelper().schemaFromString(schemaText)) dropSQL = [ "drop table {name}".format(name=table.model.name) for table in schema ] except SkipTest as e: DummyWorkDone = DummyWorkItem = object skip = e else: DummyWorkDone = fromTable(schema.DUMMY_WORK_DONE) DummyWorkItem = fromTable(schema.DUMMY_WORK_ITEM) skip = False class DummyWorkDone(Record, DummyWorkDone): """