1
= How to make database schema changes =
3
Important: This documentation is mirrored on https://launchpad.canonical.com/DatabaseSchemaChanges
5
So, make sure to copy any changes you make here to the wiki page!
7
== Making schema changes ==
9
1. Make an SQL file in `database/schema/pending/` containing the changes you want, excluding any changes to default values.
10
2. Run `make schema` to get a pristine database of sampledata.
11
3. Run the SQL on the database (`psql launchpad_dev -f your-patch.sql`) to ensure it works.
12
4. In `database/schema/`, `make newsampledata`.
13
5. Replace `current.sql` with `newsampledata.sql`.
14
6. Copy your SQL into `database/schema/` with a name like `patch-xx-99-0.sql` (where ''xx'' matches the existing patches), and ending with the line "INSERT INTO Launchpad``Database``Revision VALUES (xx, 99, 0);". Both the name and this last line should be renamed as directed when the patch is approved.
15
At this point `make schema` should work again.
16
7. Make any necessary changes to `lib/canonical/lp/dbschema.py`, `database/schema/fti.py`, and to the relevant `lib/canonical/launchpad/database/` classes.
17
8. Make any changes to the SQL patch to reflect new default values.
19
== Proposing database schema changes ==
21
For any tables and fields that you change with an SQL script via Stuart
22
(stub on IRC), please make sure you include comments.
24
The process now looks like this:
26
1. If you think the proposed changes may be controversial, or you are just ensure, it is worth discussing the changes on the launchpad mailing list first to avoid wasting your time.
27
2. Work on the patch in a branch as documented above.
28
3. Add the details of your branch to StuartBishop's review queue on PendingReviews.
29
4. Work on it in revision control till StuartBishop is happy. He will give you an official patch number
30
5. Rename your patch to match the official patch number
31
6. Once code is also ready and reviewed, commit as normal.
33
== Resolving schema conflicts ==
35
Resolving conflicts in `current.sql` manually is usually more trouble than it's worth. Instead, first resolve any conflicts in `comments.sql`, then: {{{
38
mv {patch-in-question}-0.sql comments.sql pending/
39
cp {parent branch, e.g. rocketfuel}/database/schema/comments.sql ./
40
cp ../sampledata/current.sql.OTHER ../sampledata/current.sql
42
psql launchpad_dev -f pending/patch-xx-99-0.sql
44
mv ../sampledata/newsampledata.sql ../sampledata/current.sql
45
mv pending/{patch-in-question}-0.sql pending/comments.sql ./
46
make # Just to make sure everything works
48
bzr resolve database/sampledata/current.sql
52
= Production Database Upgrades =
54
First get a copy of the Launchpad source built and ready on emperor, readable
57
Then, before you do anything else, inform #canonical, #launchpad and
58
#ubuntu-devel that Launchpad and the Wiki authentication systems will be
59
offline for 30 minutes (or longer if there is data migration to do).
65
Start PostgreSQL without external connections
67
% pg_ctl start -o '--tcpip-socket=false' -o '--ssl=false' \
68
-l /var/log/postgresql/postgres.log
70
As user postgres, run the upgrade.py, fti.py and security.py scripts.
71
fti.py can be skipped if you are sure no changes need to be made to the
72
full text indexes (ie. fti.py has not been modified and no patches affect
73
the tables being indexed). This process should work without issues, as any
74
issues (such as DB patches not working on production data) will have been
75
picked up from the daily updates to the staging environment. Do not run
76
update.py using the --partial option to ensure that changes will be rolled
79
% cd dists/launchpad/database/schema
80
% env LPCONFIG=production \
81
python upgrade.py -d launchpad_prod -U postgres -H ''
82
% env LPCONFIG=production \
83
python fti.py -d launchpad_prod -U postgres -H ''
84
% env LPCONFIG=production \
85
python security.py -d launchpad_prod -U postgres -H ''
87
Restart PostgreSQL with external connections
90
% pg_ctl start -l /var/log/postgresql/postgres.log
92
At this point, services should be restarted that don't automatically
93
reconnect, such as the Launchpad web application servers and the Librarian.
95
== Create a new development baseline ==
97
After a production update, you should occasionally copy the live schema
98
back into the development tree. This ensures that any differences that have
99
crept in between the development database and reality are fixed.
100
The new baseline dump (launchpad-XX-0-0.sql in this directory) can
101
be generated on production using the following::
103
pg_dump -Fc --schema-only --no-owner --no-acl --schema=public \
104
launchpad_prod > launchpad.dump
105
pg_restore -l launchpad.dump | \
106
grep -v PROCEDURAL | grep -v COMMENT | \
107
grep -v FUNCTION | grep -v VIEW > launchpad.list
108
pg_restore -l launchpad.dump | grep VIEW >> launchpad.list
109
echo "-- Generated `date`" > launchpad.sql
110
echo 'SET client_min_messages=ERROR;' >> launchpad.sql
111
pg_restore --no-owner --no-acl -L launchpad.list launchpad.dump | \
112
grep -v '^--' >> launchpad.sql
114
Move all the existing patches and the old baseline to the archive directory.
115
Add the new baseline using the next revision number (should be in sync
116
with the production release version). Create a patch-XX-0-0.sql patch
117
to populate the LaunchpadDatabaseRevision table with the correct value
123
There is a Makefile in launchpad/database/schema that will
124
create the launchpad_test database (if it doesn't already exist),
125
drop all your tables and create the current schema with all patches
128
If you want to check anything into the launchpad/database/schema
129
directory, please do not give it a .sql extension or you will might
130
confuse the simple Makefile.
1
See https://dev.launchpad.net/PolicyAndProcess/DatabaseSchemaChangesProcess