Skip to content

Instantly share code, notes, and snippets.

@yuchant
Created June 7, 2012 02:44
Show Gist options
  • Save yuchant/2886232 to your computer and use it in GitHub Desktop.
Save yuchant/2886232 to your computer and use it in GitHub Desktop.
def generate_default_sql(app, created_models, verbosity, **kwargs):
"""
Generate default SQL
"""
db_fields = filter(lambda x: hasattr(x, 'db_default'), OrderShipLog._meta.fields)
modelname = OrderShipLog._meta.object_name
sql_dir = os.path.join(os.path.dirname(__file__), '../sql/')
sql_filename = os.path.join(sql_dir, '{modelname}.postgresql_psycopg2.sql'.format(
modelname=modelname.lower()))
if not os.path.exists(sql_dir):
os.makedirs(sql_dir)
with open(sql_filename, 'w+') as f:
for field in db_fields:
attname, db_column = field.get_attname_column()
if not field.db_default:
raise Exception("Must enter value for field {modelname}.{field}.db_defailt".format(
modelname=modelname,
field=attname))
f.write('ALTER TABLE {db_table} ALTER COLUMN {db_column} SET DEFAULT {default};\n'.format(
db_table=OrderShipLog._meta.db_table,
db_column=db_column,
default=field.db_default,
))
post_syncdb.connect(generate_default_sql, sender=fulfillment_models)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment