Skip to content

Commit

Permalink
Migrations (#124)
Browse files Browse the repository at this point in the history
* Migrations setup

* Add major to array migration

Co-authored-by: Lucent Fong <[email protected]>
Co-authored-by: js324 <[email protected]>
  • Loading branch information
3 people authored May 24, 2022
1 parent edb1115 commit 9ea25e3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config
from mongodb_migrations.cli import MigrationManager

mgr = MigrationManager()
mgr.config.mongo_url = config.DB_URI
mgr.config.metastore = 'migrations'
mgr.run()
13 changes: 13 additions & 0 deletions migrations/20200820_users_major_to_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from mongodb_migrations.base import BaseMigration
from titlecase import titlecase

class Migration(BaseMigration):
def upgrade(self):
for u in self.db.users.find():
u['major'] = list(map(lambda s: titlecase(s.strip()), u['major'].split(';')))
self.db.users.save(u)

def downgrade(self):
for u in self.db.users.find():
u['major'] = u['major'].join(';')
self.db.users.save(u)
10 changes: 10 additions & 0 deletions migrations/_20200806_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from mongodb_migrations.base import BaseMigration

class Migration(BaseMigration):
def upgrade(self):
for i in self.db.some_collection.find():
item['new_column'] = 'new_value'
self.db.some_collection.save(item)

def downgrade(self):
self.db.some_collection.update_many({}, {"$unset": {"new_column": ""}})
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jsonschema==2.6.0
lazy-object-proxy==1.3.1
mccabe==0.6.1
mock==4.0.2
mongodb-migrations==0.7.0
more-itertools==5.0.0
oauth2client==4.1.3
oauthlib==3.1.0
Expand All @@ -46,6 +47,7 @@ rsa==4.7
s3transfer==0.2.1
six==1.12.0
sparkpost==1.3.6
titlecase==1.1.1
typed-ast==1.4.3
uritemplate==3.0.0
urllib3==1.24.2
Expand Down

0 comments on commit 9ea25e3

Please sign in to comment.