Skip to content

Commit

Permalink
added --seed in order to enable introducting nashvegas to already exi…
Browse files Browse the repository at this point in the history
…sting projects
  • Loading branch information
paltman committed Aug 27, 2010
1 parent 6116ede commit d064603
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Options
pipe the contents to a migration.
* ``--list`` - Lists all the scripts that will need to be executed.
* ``--execute`` - Executes all the scripts that need to be executed.
* ``--seed`` - Populates Migration model with scripts that have already been
applied to your database and effectively want to skip execution.


Conventions
Expand Down
4 changes: 2 additions & 2 deletions nashvegas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION = (0, 1, 0, "a", 1) # following PEP 386
DEV_N = 2
VERSION = (0, 2, 0, "a", 1) # following PEP 386
DEV_N = 1


def get_version():
Expand Down
61 changes: 56 additions & 5 deletions nashvegas/management/commands/upgradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Command(BaseCommand):
make_option("-c", "--create", action = "store_true",
dest = "do_create", default = False,
help = "Generates sql for models that are installed but not in your database."),
make_option("-s", "--seed", action = "store_true",
dest = "do_seed", default = False,
help = "Seed nashvegas with migrations that have previously been applied in another manner."),
make_option("-p", "--path", dest = "path",
default = os.path.join(
os.path.dirname(
Expand Down Expand Up @@ -125,8 +128,17 @@ def execute_migrations(self):
content = "".join(lines)

if migration_path.endswith(".sql"):
to_execute = "".join([l for l in lines if not l.startswith("### New Model: ")])
p = Popen("python manage.py dbshell".split(), stdin=PIPE, stdout=PIPE, stderr=STDOUT)
to_execute = "".join(
[l for l in lines if not l.startswith("### New Model: ")]
)

p = Popen(
"python manage.py dbshell".split(),
stdin=PIPE,
stdout=PIPE,
stderr=STDOUT
)

p.communicate(input=to_execute)[0]

created_models.extend([
Expand All @@ -140,11 +152,44 @@ def execute_migrations(self):
if hasattr(module, 'migrate') and callable(module.migrate):
module.migrate()

Migration.objects.create(migration_label=migration, content=content, scm_version=self._get_rev(migration_path))
Migration.objects.create(
migration_label=migration,
content=content,
scm_version=self._get_rev(migration_path)
)
fp.close()

emit_post_sync_signal(created_models, self.verbosity, self.interactive, self.db)
call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=self.db)
emit_post_sync_signal(
created_models,
self.verbosity,
self.interactive,
self.db
)

call_command(
'loaddata',
'initial_data',
verbosity=self.verbosity,
database=self.db
)

def seed_migrations(self):
migrations = [os.path.join(self.path, m) for m in self._filter_down()]
if len(self.args) > 0:
migrations = [arg for arg in self.args if not arg.endswith(".pyc")]
for migration in migrations:
m, created = Migration.objects.get_or_create(
migration_label=os.path.basename(migration),
content=open(migration, "rb").read()
)
if created:
# this might have been executed prior to committing
m.scm_version = self._get_rev(migration)
m.save()
print m.migration_label, "has been seeded"
else:
print m.migration_label, "was already applied."


def list_migrations(self):
migrations = self._filter_down()
Expand All @@ -166,6 +211,9 @@ def handle(self, *args, **options):
self.do_list = options.get("do_list")
self.do_execute = options.get("do_execute")
self.do_create = options.get("do_create")
self.do_seed = options.get("do_seed")
self.args = args

self.path = options.get("path")
self.verbosity = int(options.get('verbosity', 1))
self.interactive = options.get('interactive')
Expand All @@ -182,3 +230,6 @@ def handle(self, *args, **options):
if self.do_list:
self.list_migrations()

if self.do_seed:
self.seed_migrations()

1 change: 0 additions & 1 deletion nashvegas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class Migration(models.Model):
date_created = models.DateTimeField(default=datetime.now)
content = models.TextField()
scm_version = models.CharField(max_length=50, null=True, blank=True)

0 comments on commit d064603

Please sign in to comment.