Example #1
0
class TestDatabase(unittest.TestCase):
    def setUp(self):
        self.db = Database('sqlite:///test_database.db')
        
    def create_table(self):
        Table('table', self.db.metadata, Column('col1', String(40)))
        self.db.metadata.create_all()
        
    def tearDown(self):
        if os.path.exists('test_database.db'):
            os.remove('test_database.db')
        
    def test_table_with_name(self):
        """Test table with just a name. Table does not exist"""
        table = self.db.table('test')
        
    def test_table_with_existing_table(self):
        """Test table with table that exists"""
        self.create_table()
        table = self.db.table('table')
        self.assertEqual(['col1'], table.columns.keys())
Example #2
0
class Models(object):
    def __init__(self, dbstring, schema):
        self.database = Database(dbstring)
        self.schema = schema
        self.models = {}

    def build(self):
        """Build models from schema"""
        for modelname, modelschema in self.schema.items():
            table_name = modelschema["id"]
            table = self.database.table(table_name)
            dct = {"schema": modelschema}
            model = type(modelname, (EvolvedModel,), dct)
            mapper(model, table)
            self.models[modelname] = model
Example #3
0
 def __init__(self, dbstring):
     self.database = Database(dbstring)
Example #4
0
class DatabaseRepository(object):
    def __init__(self, dbstring):
        self.database = Database(dbstring)
        
    def initialize(self):
        props = {
            "type": {"type": "string", "maxLength": 40},
            "key": {"type": "string", "maxLength": 40},
            "value": {"type": "string"},
        }
        table = self.database.table('_evolve')
        if table.exists():
            raise RepositoryAlreadyExists()
        else:
            for name, prop in props.items():
                column = self.get_column(name, prop)
                table.append_column(column)
            table.create()
    
    def deploy(self, change):
        if change["change"] == "create":
            self.deploy_create(change["schema"])
        if change["change"] == "drop":
            self.deploy_drop(change["schema"])
        if change["change"] == "alter.add":
            self.deploy_alter_add(change["schema"])
        if change["change"] == "alter.rename":
            self.deploy_alter_rename(change["schema"])
        if change["change"] == "alter.modify":
            self.deploy_alter_modify(change["schema"])
        if change["change"] == "alter.drop":
            self.deploy_alter_drop(change["schema"])
            
    def deploy_create(self, schema):
        table = self.get_table(schema)
        for name, prop in schema["properties"].items():
            column = self.get_column(name, prop)
            table.append_column(column)
        table.create()
        
    def deploy_drop(self, schema):
        table = self.get_table(schema)
        table.drop()
        
    def deploy_alter_add(self, schema):
        table = self.get_table(schema)
        for name, prop in schema["properties"].items():
            column = self.get_column(name, prop)
            column.create(table)
            
    def deploy_alter_drop(self, schema):
        table = self.get_table(schema)
        for name, prop in schema["properties"].items():
            column = table.c[name]
            column.drop()
            
    def deploy_alter_rename(self, schema):
        table = self.get_table(schema)
        for oldname, newname in schema["properties"].items():
            column = table.c[oldname]
            column.alter(name=newname)
            
    def deploy_alter_modify(self, schema):
        table = self.get_table(schema)
        for name, prop in schema["properties"].items():
            newcolumn = get_column(name, prop)
            oldcolumn = table.c[name]
            oldcolumn.alter(newcolumn)
        
    def get_table(self, schema):
        return self.database.table(schema["id"])
        
    def get_column(self, name, prop):
        return self.database.column(name, prop)
Example #5
0
 def setUp(self):
     self.db = Database('sqlite:///test_database.db')
Example #6
0
 def __init__(self, dbstring, schema):
     self.database = Database(dbstring)
     self.schema = schema
     self.models = {}