I have two custom commands at my setup.py file: create_tables
and drop_tables
:
class create_tables(command):
description = 'create DB tables'
user_options = [
('database=', 'd', 'which database configuration use'),
('reset', 'r', 'reset all data previously'),
]
def initialize_options(self):
command.initialize_options(self)
self.reset = False
def run(self):
if self.reset:
self.run_command('drop_tables')
else:
command.run(self)
from vk_relations import models
models.create_tables()
print 'Tables were created successfully'
class drop_tables(command):
description = 'drop all created DB tables'
user_options = [
('database=', 'd', 'which database configuration use'),
]
def run(self):
command.run(self)
answer = raw_input('Are you sure you want to clear all VK Relations data? (y/n): ')
if 'y' == answer:
from vk_relations import models
models.drop_tables()
print 'Tables were dropped successfully'
elif 'n' == answer:
quit()
else:
sys.exit()
Command $ setup.py create_tables -r -dmain
should run command drop_tables
and create new tables at main
database, but run_command
method doesn't allow to provide options to the command. How to specify option database
for drop_tables
inside create_tables
command?
Right now I've used this hack:
cmd_obj = self.distribution.get_command_obj('drop_tables')
cmd_obj.database = self.database
self.run_command('drop_tables')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With