Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-milestones
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-milestones
Commits
52785bfb
Commit
52785bfb
authored
Nov 09, 2015
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert data migration to Django migration
parent
d6808475
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
35 deletions
+36
-35
milestones/migrations/0001_initial.py
+0
-35
milestones/migrations/0002_data__seed_relationship_types.py
+36
-0
No files found.
milestones/migrations/0001_initial.py
View file @
52785bfb
...
...
@@ -4,40 +4,6 @@ from __future__ import unicode_literals
from
django.db
import
models
,
migrations
import
django.utils.timezone
import
model_utils.fields
from
milestones.data
import
fetch_milestone_relationship_types
# See this link for forwards_func and reverse_func
# https://docs.djangoproject.com/en/1.8/ref/migration-operations/#runpython
def
forwards_func
(
apps
,
schema_editor
):
"""
Adds database entries for milestone relationship types defined in data.py
Performs existence checks before adding in order to avoid integrity errors
"""
milestone_relationship_type_model
=
apps
.
get_model
(
"milestones"
,
"MilestoneRelationshipType"
)
db_alias
=
schema_editor
.
connection
.
alias
for
name
in
fetch_milestone_relationship_types
()
.
values
():
try
:
milestone_relationship_type_model
.
objects
.
using
(
db_alias
)
.
get
(
name
=
name
)
except
milestone_relationship_type_model
.
DoesNotExist
:
milestone_relationship_type_model
.
objects
.
using
(
db_alias
)
.
create
(
name
=
name
,
description
=
'Autogenerated milestone relationship type "{}"'
.
format
(
name
),
)
def
reverse_func
(
apps
,
schema_editor
):
"""
forwards_func() creates MilestoneRelationshipType instances,
so reverse_func() should delete them.
"""
milestone_relationship_type_model
=
apps
.
get_model
(
"milestones"
,
"MilestoneRelationshipType"
)
db_alias
=
schema_editor
.
connection
.
alias
for
name
in
fetch_milestone_relationship_types
()
.
values
():
milestone_relationship_type_model
.
objects
.
using
(
db_alias
)
.
filter
(
name
=
name
)
.
delete
()
class
Migration
(
migrations
.
Migration
):
...
...
@@ -143,5 +109,4 @@ class Migration(migrations.Migration):
name
=
'coursecontentmilestone'
,
unique_together
=
set
([(
'course_id'
,
'content_id'
,
'milestone'
)]),
),
migrations
.
RunPython
(
forwards_func
,
reverse_func
,
False
),
]
milestones/migrations/0002_data__seed_relationship_types.py
0 → 100644
View file @
52785bfb
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
from
milestones.data
import
fetch_milestone_relationship_types
def
seed_relationship_types
(
apps
,
schema_editor
):
"""Seed the relationship types."""
MilestoneRelationshipType
=
apps
.
get_model
(
"milestones"
,
"MilestoneRelationshipType"
)
db_alias
=
schema_editor
.
connection
.
alias
for
name
in
fetch_milestone_relationship_types
()
.
values
():
MilestoneRelationshipType
.
objects
.
using
(
db_alias
)
.
get_or_create
(
name
=
name
,
description
=
'Autogenerated milestone relationship type "{}"'
.
format
(
name
),
)
def
delete_relationship_types
(
apps
,
schema_editor
):
"""Clean up any relationships we made."""
MilestoneRelationshipType
=
apps
.
get_model
(
"milestones"
,
"MilestoneRelationshipType"
)
db_alias
=
schema_editor
.
connection
.
alias
for
name
in
fetch_milestone_relationship_types
()
.
values
():
MilestoneRelationshipType
.
objects
.
using
(
db_alias
)
.
filter
(
name
=
name
)
.
delete
()
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'milestones'
,
'0001_initial'
),
]
operations
=
[
migrations
.
RunPython
(
seed_relationship_types
,
delete_relationship_types
),
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment