Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 40 additions & 47 deletions docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,74 @@ Changes that involve database migrations
========================================

Any changes to the database (model fields, model field data, adding
permissions, ...) need a migration.
permissions, ...) require a migration.

We use `schematic <https://github.com/jbalogh/schematic>`_ for
migrations.
We use `South <http://south.readthedocs.org/en/latest/index.html>`_
for migrations.


Running migrations
------------------

To run migrations, you do::

./vendor/src/schematic/schematic migrations/
$ ./manage.py migrate

It'll perform any migrations that haven't been performed for all apps.


Setting up an app for migrations
--------------------------------

It'll perform any migrations that haven't been performed, yet.
New apps need to have the migration structure initialized. To do that,
do::

$ ./manage.py schemamigration <appname> --initial


Creating a new migration
------------------------

Each migration increases the schema version number by 1. You can
figure out which schema your database is running by doing::
There are two kinds of migrations: schema migrations and data
migrations.

To create a new schema migration, do::

./vendor/src/schematic/schematic -v migrations
$ ./manage.py schemamigration <appname> --auto

Migrations are stored in files in ``migrations/``.

To create a new migration, you'll create a new file in the
``migrations/`` directory. The first part of the filename is the
schema version number. Then a dash. Then some name that indicates what
the migration is for. See the directory for examples.
South can figure out a lot of it for you. You can see the list of
things it'll probably get right `in the South autodetector docs
<http://south.readthedocs.org/en/latest/autodetector.html#autodetector-supported-actions>`_.

There are a bunch of ways to create the substance of the file. It
depends on what it is you're trying to do. One way is to base it on
the output of::
For everything else, you can run the auto and then tweak the migration.

./manage.py sqlall <appname>
To create a new data migration, do::

for the app that you made changes to, then editing that down to the
bits needed.
$ ./manage.py datamigration <appname>

1. Remove the ``BEGIN`` and ``COMMIT`` lines.

2. If you have CREATE TABLE statements, make sure they end with setting
the engine to InnoDB. For example::
For obvious reasons, there is no "auto" mode for data migrations.

CREATE TABLE `topics_topic` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`title` varchar(255) NOT NULL,
`slug` varchar(50) NOT NULL,
`description` longtext NOT NULL,
`image` varchar(250),
`parent_id` integer,
`display_order` integer NOT NULL,
`visible` bool NOT NULL
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

3. If you created new models, make sure to insert the content type and
default permissions. For example, something like this::
More about migrations
---------------------

INSERT INTO django_content_type (name, app_label, model) VALUES
('record', 'search', 'record');
SET @ct = (SELECT id from django_content_type WHERE app_label='search'
and model='record');
INSERT INTO auth_permission (name, content_type_id, codename) VALUES
('Can add record', @ct, 'add_record'),
('Can change record', @ct, 'change_record'),
('Can delete record', @ct, 'delete_record');
Definitely read the chapter of the South tutorial on `teams and
workflow
<http://south.readthedocs.org/en/latest/tutorial/part5.html>`_.
That'll answer a lot of questions about how to write and test
migrations.

For many of your questions, `the South tutorial
<http://south.readthedocs.org/en/latest/tutorial/index.html>`_
contains the answers.

Testing a migration
-------------------
For other questions, definitely check out the `South documentation
<http://south.readthedocs.org/en/latest/index.html>`_.

You can dump your db to a file to save its state, then run the
migrations. That way if the migration has bugs, you can restore your
database.
For questions that aren't answered there, ask someone and/or try
Googling the answer.


Changes that involve reindexing
Expand Down
34 changes: 14 additions & 20 deletions docs/hacking_howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,26 +252,29 @@ database settings. For example, using the settings above::
mysql> GRANT ALL ON kitsune.* TO kitsune@localhost IDENTIFIED BY '<YOUR_PASSWORD>';


To load the latest database schema, use ``scripts/schema.sql`` and
``schematic``::
To initialize the database, do::

$ mysql -u kitsune -p kitsune < scripts/schema.sql
$ ./vendor/src/schematic/schematic migrations/
$ ./manage.py syncdb --migrate


This will ask you to create a superuser account. Just follow the prompts.

You'll now have an empty but up-to-date database!

Finally, you'll probably want to create a superuser. Just use Django's
``createsuperuser`` management command::
After logging in, you can create a profile for the user by going to
``/users/edit`` in your browser.

$ ./manage.py createsuperuser
See also the :ref:`important wiki documents <wiki-chapter>`
documentation.


and follow the prompts. After logging in, you can create a profile for
the user by going to ``/users/edit`` in your browser.
Install Sample Data
-------------------

We include some sample data to get you started. You can install it by
running this command::

See also the :ref:`important wiki documents <wiki-chapter>`
documentation.
$ ./manage.py generatedata


Product Details Initialization
Expand All @@ -285,15 +288,6 @@ the initial fetch::
$ ./manage.py update_product_details


Install Sample Data
-------------------

We include some sample data to get you started. You can install it by
running this command::

$ ./manage.py generatedata


Testing it out
==============

Expand Down
88 changes: 88 additions & 0 deletions kitsune/announcements/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

def forwards(self, orm):
# Adding model 'Announcement'
db.create_table('announcements_announcement', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
('creator', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
('show_after', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, db_index=True)),
('show_until', self.gf('django.db.models.fields.DateTimeField')(db_index=True, null=True, blank=True)),
('content', self.gf('django.db.models.fields.TextField')(max_length=10000)),
('group', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.Group'], null=True, blank=True)),
('locale', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['wiki.Locale'], null=True, blank=True)),
))
db.send_create_signal('announcements', ['Announcement'])


def backwards(self, orm):
# Deleting model 'Announcement'
db.delete_table('announcements_announcement')


models = {
'announcements.announcement': {
'Meta': {'object_name': 'Announcement'},
'content': ('django.db.models.fields.TextField', [], {'max_length': '10000'}),
'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'creator': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.Group']", 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'locale': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['wiki.Locale']", 'null': 'True', 'blank': 'True'}),
'show_after': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
'show_until': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'})
},
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'wiki.locale': {
'Meta': {'ordering': "['locale']", 'object_name': 'Locale'},
'editors': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'locales_editor'", 'blank': 'True', 'to': "orm['auth.User']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'leaders': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'locales_leader'", 'blank': 'True', 'to': "orm['auth.User']"}),
'locale': ('django.db.models.fields.CharField', [], {'max_length': '7', 'db_index': 'True'}),
'reviewers': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'locales_reviewer'", 'blank': 'True', 'to': "orm['auth.User']"})
}
}

complete_apps = ['announcements']
Empty file.
103 changes: 103 additions & 0 deletions kitsune/customercare/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

def forwards(self, orm):
# Adding model 'Tweet'
db.create_table('customercare_tweet', (
('tweet_id', self.gf('django.db.models.fields.BigIntegerField')(primary_key=True)),
('raw_json', self.gf('django.db.models.fields.TextField')()),
('locale', self.gf('django.db.models.fields.CharField')(max_length=20, db_index=True)),
('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, db_index=True)),
('reply_to', self.gf('django.db.models.fields.related.ForeignKey')(related_name='replies', null=True, to=orm['customercare.Tweet'])),
('hidden', self.gf('django.db.models.fields.BooleanField')(default=False, db_index=True)),
))
db.send_create_signal('customercare', ['Tweet'])

# Adding model 'Reply'
db.create_table('customercare_reply', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(blank=True, related_name='tweet_replies', null=True, to=orm['auth.User'])),
('twitter_username', self.gf('django.db.models.fields.CharField')(max_length=20)),
('tweet_id', self.gf('django.db.models.fields.BigIntegerField')()),
('raw_json', self.gf('django.db.models.fields.TextField')()),
('locale', self.gf('django.db.models.fields.CharField')(max_length=20)),
('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, db_index=True)),
('reply_to_tweet_id', self.gf('django.db.models.fields.BigIntegerField')()),
))
db.send_create_signal('customercare', ['Reply'])


def backwards(self, orm):
# Deleting model 'Tweet'
db.delete_table('customercare_tweet')

# Deleting model 'Reply'
db.delete_table('customercare_reply')


models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'customercare.reply': {
'Meta': {'object_name': 'Reply'},
'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'locale': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
'raw_json': ('django.db.models.fields.TextField', [], {}),
'reply_to_tweet_id': ('django.db.models.fields.BigIntegerField', [], {}),
'tweet_id': ('django.db.models.fields.BigIntegerField', [], {}),
'twitter_username': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'tweet_replies'", 'null': 'True', 'to': "orm['auth.User']"})
},
'customercare.tweet': {
'Meta': {'ordering': "('-tweet_id',)", 'object_name': 'Tweet'},
'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
'hidden': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
'locale': ('django.db.models.fields.CharField', [], {'max_length': '20', 'db_index': 'True'}),
'raw_json': ('django.db.models.fields.TextField', [], {}),
'reply_to': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'replies'", 'null': 'True', 'to': "orm['customercare.Tweet']"}),
'tweet_id': ('django.db.models.fields.BigIntegerField', [], {'primary_key': 'True'})
}
}

complete_apps = ['customercare']
Empty file.
Loading