We could probably use a single database with multiple schemas for all our development work. That’s
how we have been doing it for years. But sometimes that leads to us making compromises - like
running a slightly different version of the database in dev vs production. And it leads to being
somewhat conservative when trying out different database options. Now that we are using Docker for
deploying our applications, it makes sense to use docker-compose to create all the services our apps
use: relational databases, ElasticSearch, caches. The docker-compose file also manages mounted
volumes (for preserving the data in our development databases) AND a private network for each set of
Creating a database and user
If we want to take full advantage of docker-compose’s automated workflow, we need to be able to
recreate our databases - including loading dev data - automatically. The official MySQL database
container image supports a really easy way to do this. If you set a
handful of environment variables in your docker-compose.yml file, the first time you start the
database container, it will create the database instance and set the root password to the value from
MYSQL_ROOT_PASSWORD. If you include values for MYSQL_DATABASE, MYSQL_USER and
MYSQL_PASSWORD, the first startup of the database will create that database and grant all
privileges on that database to the specified user. Excellent! That gets us most of the way there.
Now if we could only load some initial data….
Loading initial data
The MySQL image provides for this too. In the section “Initializing a fresh instance”:
When a container is started for the first time, a new database with the specified name will be
created and initialized with the provided configuration variables. Furthermore, it will execute
files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files
will be executed in alphabetical order. You can easily populate your mysql services by mounting a
SQL dump into that directory and provide custom images with contributed data. SQL files will be
imported by default to the database specified by the MYSQL_DATABASE variable.
So the database section of our standard docker-compose.yml looks like:
And our project’s sql/docker/ directory has:
And don’t forget to exclude that mysql-data directory from your image by including it in your
Version 2a - custom user creation
The stock setup above works great - until you have some super old code that you don’t want to have
to upgrade to recent MySQL libraries. If you want to connect those projects to MySQL 8, you need to
tell the database that this user will be using an older authentication plugin. So you need to issue
the user create yourself. One option is to only do the user creation and privilege setting in your
script, leaving the database creation and loading as above. Put the following in
And then remove MYSQL_USER and MYSQL_PASSWORD from the docker-compose.yml environment.
Version 2b - fully custom script
The version above works fine, but if you need additional configuration of the database - or just
want to take full control of the user and database creation and data loading - you can use the
First, move the initial_data.sql file out of the project’s sql/docker/ directory:
Then remove MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD variables from your
docker-compose.yml and add another volume to mount the directory where you put initial_data.sql. I
just moved the file up one level and then mounted /sql as /sql_data in the container. The
docker-compose.yml should now look like this:
Then, in sql/docker/init.sql, create your user and database; then load your data from the mounted file:
I have been (was?) a longtime emacs user - ever since I got a Unix account in
graduate school. In the first dot com boom, I worked for a company that was
mainly populated by emacs users and picked up a few tricks - including using
emacs without a mouse. Unlike most emacs users, I never did very much
customization. So in some people’s eyes, I was never really an emacs power user.
But that did have the advantage that I could sit down to any computer, install a
fresh emacs build, and immediately be completely comfortable with the
Well…. except for the fact that on a Mac keyboard, the alt key (on a Mac,
that’s the option key), is in the wrong place. For a decade, I simply swapped
the keyboard bindings for the option and command keys. Worked great for me but
it meant that no one could sit down to my keyboard and do anything. A few years
ago I started working with a new team and thought we might do some pair
programming. I didn’t want to learn Vim and I couldn’t expect them to switch to
Emacs, so we all settled on SublimeText - and I quit remapping my keys at the OS
SublimeText worked pretty well for our team and there are a number of things I
really like about it. I use the multiline editing a lot - and code folding can
be pretty convenient. I know emacs can do both of those things, but I never
learned how to set up either of those features in emacs. On the other hand, I
never learned to rewrap paragraphs in Sublime (alt-q in emacs). And if I have to
do any heavy search and replace, I still prefer using the keyboard shortcuts in
emacs to the equivalent replace function in Sublime.
I recently decided it was time for some professional self-improvement and that
configuring and really learning one text editor is one thing I need to do.
VSCode appears to be where all the cool kids are moving these days, so I decided
it was time for me to try it too.
Plugins and Configuration
Since I used emacs without a mouse for literally decades, I use the emacs
keybindings to move around - even in a terminal. Having more of my familiar
keybindings available would make me a lot happier. I never installed them in
Sublime because the point of moving to Sublime was to have a unified editor
experience with my team. Unfortunately that team is no longer together, so no
reason for me not to set up my text editor to suit me and only me!
I initially found Emacs
but later found a Reddit that pointed out that plugin had not been updated in a
while. I took Reddit’s advice and did a little comparison shopping. Awesome
has excellent ratings and recent updates. So let’s learn to use that. Based on
the instructions, I set editor.find.seedSearchStringFromSelection to false. I
also need the command key to work as Alt, so I set
emacs-mcx.useMetaPrefixMacCmd to true.
Nice key bindings:
Cmd-s and C-x C-s BOTH work to save a file!
C-a (and C-e) behave in an interesting way. The first time you use it, it goes
to the start of the visible line; the second time you use it, it goes to the
start of the logical line. Not what I am used to from emacs but super handy!
Things I need to get used to or fix:
VSCode shares the kill ring with the OS (BIG WIN!), however, once you get into
VSCode, paste is NOT Cmd-v, it’s C-y. I like C-y working, but switching
keyboard mappings in mid-operation may break my brain. We’ll have to see if
I get used to it.
I am not sure what emacs-mcx.cursorMoveOnFindWidget is. Sounds like I might
need to adjust it but not sure yet.
In the mean time, I have printed out the cheat sheet for keybindings.
One thing I miss in Sublime Text is the ability to rewrap paragraphs using M-q.
I use that A LOT when writing prose such as documentation or blog posts - or
when I edit a block comment and want to reflow the text. This VSCode extension,
like what I want - it even says “Similar to wrap/fill paragraph in Sublime
(alt+q) Emacs (M-q) or Vim (gq); but more powerful.”. The only wrinkle is that
it’s default keybinding is Option-q; M-q is already bound to VSCode’s
workspace.action.quit. A little searching told me how to remove that
keybinding (find it, right click, and choose “remove keybinding” from the
dropdown). Then I searched for the Rewrap plugin’s keybindings and swapped in my
usual M-q for rewrapping a paragraph.
I also turned on auto wrap (like emacs auto-file mode) and adjusted the
editor.wordWrapColumn to 100 since I think the default (80) is a bit narrow
these days. We’ll see what I think of that. If I don’t like it, I can either set
per file type widths OR try the ‘wrap to rulers’ option.
According to the docs, I should be
able to rewrap code comments without messing with doc comments within the
comment and without messing with the code itself. It will be very interesting to
see if that works.
I am a very poor speller. In emacs, I use ispell. So what can I replace that
with in VSCode? Searching for spellcheckers in extensions sows “Spell Right” is
popular and will use my Mac’s built-in dictionary. That will be super handy
since I can add words like this:
Now for my last trick, I would like to have the same setup on my work computer
and on my personal laptop. VSCode allows me to save my settings - using a
private GIST attached to my GitHub account. There are a bunch of docs about how
to do partial syncs and how to resolve conflicts. But since this is a new
install on both computers, I just clicked the menu item for turning sync on,
told GitHub to allow VSCode to store information for me, and now I have matching
settings on both my computers.
There is an in-editor Markdown previewer!! So I can see how this post renders as
I compose it!
accessible to anyone who can log into your site at all
accessible if they have a password you enter on that form
That would seem like a fairly comprehensive list, but at work, we often
restrict things to anyone coming from specific IP addresses. So when
we rolled out our new CMS, we had requests for it to support the “on
campus” option - just like the old ones had.
Adding the “On Campus” option comes in two parts: adding it to the
options offered to the editor and then enforcing that restriction when
serving pages or documents.
Adding a New Restriction Choice
The privacy options are defined in the BaseViewRestriction class in
wagtail.core.models. We will be deciding if a browser is coming from
on or off campus with a middleware, so we will not have to add any columns
to the tables defined by classes inheriting from BaseViewRestriction.
But we do need to add “on campus” to the options offered to the site
To override the stock RESTRICTION_CHOICES from wagtail.core.models.BaseViewRestriction
with our own, we need to monkeypatch the class. We have a number of small
customizations in our wagtail site, so we collect them all into their own app,
wagtail_patches, which we include in INSTALLED_APPS.
And now the monkeypatching code:
That will add the ON_CAMPUS choice to our form. Since there are no additional
parameters needed for this restriction, you wouldn’t think we would have do
make any additional changes to the form or form validations. But as of Django 3,
we also need to patch the model level validations. We do that like this:
Enforcing Our New Restriction
In our setup, we have split enforcement into two parts, a middleware that
determines if a request is “on campus” or not and then code that uses that
information to show or not show the private page or file. We took this
approach beause we already have shared library that does the “on campus”
checking. If you do not need to share the code that checks for on vs off
campus, you may want to put that check directly into the code that enforces
On Campus Middleware
Define the following middleware somewhere in your project - customizing it
with your own IP addresses.
Then add this to the MIDDLEWARE list in your Django settings file. Since
this middleware is a silent pass through in both directions (only the side
effect of setting the ON_CAMPUS session variable to True or False matters),
you can put this line anywhere in the list.
Updating the Enforcement Code
The meat of the restriction enforcement is in BaseViewRestriction’s accept_request
method, so we need to add our new on-campus check:
What should happen when accept_request returns False? That depends on which
restriction triggers the failure. For example, if a user fails the LOGIN restriction,
they should be directed to log in - but if they fail the ON_CAMPUS restriction,
they should get an error message. The correct actions for the built-in
restriction types are handled in a before_serve_page hook called
Since we already have monkey patched some other hooks, what we did was
to monkey patch check_view_restrictions:
But looking at the source code for the page serve view, I don’t think
we need to replace the existing check_view_restrictions. I think we
can just add an additional before_serve_page hook that returns our
“sorry you need to be on campus to see this page” message. If I were
doing this from scratch, I would put the following code into one of my
wagtail_hooks.py files (either in the wagtail_patches app or in that
app that contains most of my page models).
At work, we need to build a scheduling system. We want to present the
user with a list of possible dates - and then the possible slots on
that date. I don’t want to have all the possible empty slots in the
database so I thought I would have to build them procedurally using
The code above loops over the days in the range - and then on
available days, loops over the hours in that day and returns a list of
datetimes. There is a lot of ugly adding of Python timedelta objects
and resetting the time to start iterating on a new day. It works - but
the next step, eliminating slots that are already full, is going to be
even uglier - lots of tedious “does this interval overlap with
existing scheduled events”.
When I started looking into how to check the overlap, I started to
looking into checking overlaps in the database - and found that a)
Postgres has a date range data type (tstzrange), b) Django’s Postgres
extensions has a field that wraps the Postgres tstzrange field
(DateTimeRangeField), and c) the Postgres docs even have
of how to create indexes that prevent you from scheduling more than
one person to occupy a specific room at one time. All that ugly
python, turns into:
The only slightly tricky part of that was restricting allowed days to
MWF. I want my constant to use the day names, not the integers
Postgres uses for days of the week. So I needed to import Python’s
calendar module to convert “Monday” to an integer. Python uses 0
for Monday, but Postgres thinks Monday is 1, so add 1. Then it took me
a little while to figure out how to pass a list into the query in a
way that everything is properly interpolated and quoted; the trick:
Now I just need to join to my reservations table to exclude slots
where the schedule is already full.
Django has extensive documentation for it’s ORM but somehow I still
end up surprised by some of the queries it builds. The default logging
configuration doesn’t log queries in the way Rails does (in its
development environment) so when a query appears to give the correct
results, I don’t usually check the SQL. But I recently had a page
start to fail; I finally tracked it down to a specific query but
couldn’t immediately see why I was not getting the row I expected so I
printed the query and Django was not building the query I thought it
had been. The python is:
This produces the following SQL (edited slightly to make it easier to read):
Hmmm that’s not what I want. I don’t want 2 subqueries, one for each
condition. I want one subquery, with two two conditions. If I had
wanted 2 subqueries, I would have written 2 excludes, like this:
But both of those QuerySet definitions produce the same SQL. So how
can I produce the following SQL using the Django ORM:
I tried a couple of things using Q but mostly ended up with syntax
errors. Fortunately I finally found this Stack Overflow thread
with references the bug report for this problem AND the solution. You
can force Django to build the desired subquery by writing the subquery
It’s a little verbose, but it is actually a little clearer in some
respects - it is more like a direct python translation of the desired SQL.