-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b43e3b
commit 28cc5d7
Showing
5 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from cheeseshop import systemconfig | ||
from cheeseshop import dbapi | ||
from cheeseshop.dbmigrations import add_systemconfig | ||
|
||
# ONLY APPEND TO THIS LIST | ||
# Migrations are run in order and we start off based on the last-migration | ||
# property stored in SysemConfig. | ||
migrations = [ | ||
add_systemconfig | ||
] | ||
|
||
async def run_migrations(conn): | ||
sc = systemconfig.SystemConfig(conn) | ||
create_initial_schema = False | ||
last_migration = None | ||
|
||
try: | ||
last_migration = await sc.last_migration() | ||
except (dbapi.SchemaError, dbapi.NotFoundError): | ||
create_initial_schema = True | ||
|
||
if create_initial_schema: | ||
await dbapi.create_schema(conn) | ||
await dbapi.create_initial_records(conn) | ||
|
||
start_migrating = False | ||
if last_migration is None: | ||
start_migrating = True | ||
for migration in migrations: | ||
cur_name = migration.__name__.rsplit('.', 1)[1] | ||
if start_migrating is False: | ||
# Check if were up to last_migration and if so start migrating | ||
if last_migration == cur_name: | ||
start_migrating = True | ||
continue | ||
else: | ||
await migration.run(conn) | ||
await sc.set_last_migration(cur_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
async def run(conn): | ||
# This is a noop migration which is needed to let the migration system | ||
# know we have created initial schemas (by setting the last-migration key) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import json | ||
|
||
from cheeseshop import dbapi | ||
|
||
class SystemConfig(object): | ||
def __init__(self, conn): | ||
self._conn = conn | ||
|
||
async def last_migration(self): | ||
record = await dbapi.SystemConfig.get(self._conn, 'last-migration') | ||
return json.loads(record.value) | ||
|
||
async def set_last_migration(self, value): | ||
return await dbapi.SystemConfig.set(self._conn, 'last-migration', | ||
json.dumps(value)) |