Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions architect/databases/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, model, **meta):
self.column_value = meta['column_value']
self.column_name = meta['column']
self.pks = meta['pk'] if isinstance(meta['pk'], list) else [meta['pk']]
self.return_null = meta.get('return_null', False)

def prepare(self):
"""
Expand Down
15 changes: 10 additions & 5 deletions architect/databases/postgresql/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def prepare(self):

definitions[definition] = '\n'.join(definitions[definition]).format(**formatters)

return self.database.execute("""
execute_sql = """
-- We need to create a before insert function
CREATE OR REPLACE FUNCTION {{parent_table}}_insert_child()
RETURNS TRIGGER AS $$
Expand All @@ -55,7 +55,7 @@ def prepare(self):
END;

EXECUTE 'INSERT INTO ' || tablename || ' VALUES (($1).*);' USING NEW;
RETURN NEW;
RETURN {{return_val}};
END;
$$ LANGUAGE plpgsql;

Expand All @@ -73,7 +73,9 @@ def prepare(self):
FOR EACH ROW EXECUTE PROCEDURE {{parent_table}}_insert_child();
END IF;
END $$;

"""
if not self.return_null:
execute_sql += """
-- Then we create a function to delete duplicate row from the master table after insert
CREATE OR REPLACE FUNCTION {{parent_table}}_delete_master()
RETURNS TRIGGER AS $$
Expand All @@ -97,10 +99,13 @@ def prepare(self):
FOR EACH ROW EXECUTE PROCEDURE {{parent_table}}_delete_master();
END IF;
END $$;
""".format(**definitions).format(
"""

return self.database.execute(execute_sql.format(**definitions).format(
pk=' AND '.join('{pk} = NEW.{pk}'.format(pk=pk) for pk in self.pks),
parent_table=self.table,
column='"{0}"'.format(self.column_name)
column='"{0}"'.format(self.column_name),
return_val='NULL' if self.return_null else 'NEW'
))

def exists(self):
Expand Down
61 changes: 32 additions & 29 deletions tests/models/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,58 @@

# Generation of entities for date range partitioning
for item in ('day', 'week', 'month', 'year'):
class Meta(object):
app_label = 'test'
db_table = 'test_rangedate{0}'.format(item)

name = '{0}RangeDate{1}'.format(dbname, item.capitalize())
partition = install('partition', type='range', subtype='date', constraint=item, column='created')

locals()[name] = partition(type(name, (models.Model,), {
'__module__': 'test.models',
'name': models.CharField(max_length=255),
'created': models.DateTimeField(null=True),
'Meta': Meta,
'db': database,
}))

if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
for return_null in (True, False):
class Meta(object):
app_label = 'test'
db_table = 'test_rangeinteger{0}'.format(item)
db_table = 'test_rangedate{0}{1}'.format(item, '_return_null' if return_null else '')

name = '{0}RangeInteger{1}'.format(dbname, item)
partition = install('partition', type='range', subtype='integer', constraint=item, column='num')
name = '{0}RangeDate{1}{2}'.format(dbname, item.capitalize(), 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='date', constraint=item, column='created', return_null=return_null)

locals()[name] = partition(type(name, (models.Model,), {
'__module__': 'test.models',
'name': models.CharField(max_length=255),
'num': models.IntegerField(null=True),
'created': models.DateTimeField(null=True),
'Meta': Meta,
'db': database,
}))

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
for return_null in (True, False):
class Meta(object):
app_label = 'test'
db_table = 'test_range{0}{1}'.format(subtype, item)
db_table = 'test_rangeinteger{0}{1}'.format(item, '_return_null' if return_null else '')

name = '{0}Range{1}{2}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item)
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title')
name = '{0}RangeInteger{1}{2}'.format(dbname, item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='integer', constraint=item, column='num', return_null=return_null)

locals()[name] = partition(type(name, (models.Model,), {
'__module__': 'test.models',
'name': models.CharField(max_length=255),
'title': models.CharField(max_length=255, null=True),
'num': models.IntegerField(null=True),
'Meta': Meta,
'db': database,
}))

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
for return_null in (True, False):
class Meta(object):
app_label = 'test'
db_table = 'test_range{0}{1}{2}'.format(subtype, item, '_return_null' if return_null else '')

name = '{0}Range{1}{2}{3}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title', return_null=return_null)

locals()[name] = partition(type(name, (models.Model,), {
'__module__': 'test.models',
'name': models.CharField(max_length=255),
'title': models.CharField(max_length=255, null=True),
'Meta': Meta,
'db': database,
}))

management.call_command(command, database=database, run_syncdb=True, verbosity=0, interactive=False)
47 changes: 25 additions & 22 deletions tests/models/pony.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,40 @@

# Generation of entities for date range partitioning
for item in ('day', 'week', 'month', 'year'):
name = '{0}RangeDate{1}'.format(dbname, item.capitalize())
partition = install('partition', type='range', subtype='date', constraint=item, column='created')
for return_null in (True, False):
name = '{0}RangeDate{1}{2}'.format(dbname, item.capitalize(), 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='date', constraint=item, column='created', return_null=return_null)

locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_rangedate{0}'.format(item),
'name': Required(unicode),
'created': Optional(datetime.datetime, nullable=True),
}))
locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_rangedate{0}{1}'.format(item, '_return_null' if return_null else ''),
'name': Required(unicode),
'created': Optional(datetime.datetime, nullable=True),
}))

if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
name = '{0}RangeInteger{1}'.format(dbname, item)
partition = install('partition', type='range', subtype='integer', constraint=item, column='num')
for return_null in (True, False):
name = '{0}RangeInteger{1}{2}'.format(dbname, item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='integer', constraint=item, column='num', return_null=return_null)

locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_rangeinteger{0}'.format(item),
'name': Required(unicode),
'num': Optional(int, nullable=True)
}))
locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_rangeinteger{0}{1}'.format(item, '_return_null' if return_null else ''),
'name': Required(unicode),
'num': Optional(int, nullable=True)
}))

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
name = '{0}Range{1}{2}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item)
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title')

locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_range{0}{1}'.format(subtype, item),
'name': Required(unicode),
'title': Optional(unicode, nullable=True),
}))
for return_null in (True, False):
name = '{0}Range{1}{2}{3}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title', return_null=return_null)

locals()[name] = partition(type(name, (db.Entity,), {
'_table_': 'test_range{0}{1}{2}'.format(subtype, item, '_return_null' if return_null else ''),
'name': Required(unicode),
'title': Optional(unicode, nullable=True),
}))

db.generate_mapping(create_tables=True)
52 changes: 27 additions & 25 deletions tests/models/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,45 @@

# Generation of entities for date range partitioning
for item in ('day', 'week', 'month', 'year'):
name = '{0}RangeDate{1}'.format(dbname, item.capitalize())
partition = install(
'partition', type='range', subtype='date', constraint=item, column='created', db=engine.url)

locals()[name] = partition(type(name, (Base,), {
'__tablename__': 'test_rangedate{0}'.format(item),
'id': Column(Integer, primary_key=True),
'name': Column(String(length=255)),
'created': Column(DateTime, nullable=True)
}))

if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
name = '{0}RangeInteger{1}'.format(dbname, item)
for return_null in (True, False):
name = '{0}RangeDate{1}{2}'.format(dbname, item.capitalize(), 'ReturnNULL' if return_null else '')
partition = install(
'partition', type='range', subtype='integer', constraint=item, column='num', db=engine.url)
'partition', type='range', subtype='date', constraint=item, column='created', db=engine.url, return_null=return_null)

locals()[name] = partition(type(name, (Base,), {
'__tablename__': 'test_rangeinteger{0}'.format(item),
'__tablename__': 'test_rangedate{0}{1}'.format(item, '_return_null' if return_null else ''),
'id': Column(Integer, primary_key=True),
'name': Column(String(length=255)),
'num': Column(Integer, nullable=True)
'created': Column(DateTime, nullable=True)
}))

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
name = '{0}Range{1}{2}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item)
if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
for return_null in (True, False):
name = '{0}RangeInteger{1}{2}'.format(dbname, item, 'ReturnNULL' if return_null else '')
partition = install(
'partition', type='range', subtype=subtype, constraint=item, column='title', db=engine.url)
'partition', type='range', subtype='integer', constraint=item, column='num', db=engine.url, return_null=return_null)

locals()[name] = partition(type(name, (Base,), {
'__tablename__': 'test_range{0}{1}'.format(subtype, item),
'__tablename__': 'test_rangeinteger{0}{1}'.format(item, '_return_null' if return_null else ''),
'id': Column(Integer, primary_key=True),
'name': Column(String(length=255)),
'title': Column(String(length=255), nullable=True)
'num': Column(Integer, nullable=True)
}))

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
for return_null in (True, False):
name = '{0}Range{1}{2}{3}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title', db=engine.url, return_null=return_null)

locals()[name] = partition(type(name, (Base,), {
'__tablename__': 'test_range{0}{1}{2}'.format(subtype, item, '_return_null' if return_null else ''),
'id': Column(Integer, primary_key=True),
'name': Column(String(length=255)),
'title': Column(String(length=255), nullable=True)
}))

Base.metadata.create_all(engine)
62 changes: 33 additions & 29 deletions tests/models/sqlobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,57 @@

# Generation of entities for date range partitioning
for item in ('day', 'week', 'month', 'year'):
class sqlmeta(object):
table = 'test_rangedate{0}'.format(item)

name = '{0}RangeDate{1}'.format(dbname, item.capitalize())
partition = install('partition', type='range', subtype='date', constraint=item, column='created')

locals()[name] = partition(type(name, (SQLObject,), {
'name': StringCol(),
'created': DateTimeCol(default=None),
'sqlmeta': sqlmeta,
'_connection': connection
}))

locals()[name].createTable(True)

if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
for return_null in (True, False):
class sqlmeta(object):
table = 'test_rangeinteger{0}'.format(item)
table = 'test_rangedate{0}{1}'.format(item, '_return_null' if return_null else '')


name = '{0}RangeInteger{1}'.format(dbname, item)
partition = install('partition', type='range', subtype='integer', constraint=item, column='num')
name = '{0}RangeDate{1}{2}'.format(dbname, item.capitalize(), 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='date', constraint=item, column='created', return_null=return_null)

locals()[name] = partition(type(name, (SQLObject,), {
'name': StringCol(),
'num': IntCol(default=None),
'created': DateTimeCol(default=None),
'sqlmeta': sqlmeta,
'_connection': connection
}))

locals()[name].createTable(True)

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
if database == 'pgsql':
# Generation of entities for integer range partitioning
for item in ('2', '5'):
for return_null in (True, False):
class sqlmeta(object):
table = 'test_range{0}{1}'.format(subtype, item)
table = 'test_rangeinteger{0}{1}'.format(item, '_return_null' if return_null else '')

name = '{0}Range{1}{2}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item)
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title')
name = '{0}RangeInteger{1}{2}'.format(dbname, item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype='integer', constraint=item, column='num', return_null=return_null)

locals()[name] = partition(type(name, (SQLObject,), {
'name': StringCol(),
'title': StringCol(default=None),
'num': IntCol(default=None),
'sqlmeta': sqlmeta,
'_connection': connection
}))

locals()[name].createTable(True)

# Generation of entities for string range partitioning
for subtype in ('string_firstchars', 'string_lastchars'):
for item in ('2', '5'):
for return_null in (True, False):
class sqlmeta(object):
table = 'test_range{0}{1}{2}'.format(subtype, item, '_return_null' if return_null else '')

name = '{0}Range{1}{2}{3}'.format(dbname, ''.join(s.capitalize() for s in subtype.split('_')), item, 'ReturnNULL' if return_null else '')
partition = install('partition', type='range', subtype=subtype, constraint=item, column='title', return_null=return_null)

locals()[name] = partition(type(name, (SQLObject,), {
'name': StringCol(),
'title': StringCol(default=None),
'sqlmeta': sqlmeta,
'_connection': connection
}))

locals()[name].createTable(True)
Loading