Part 7: Orderly Populations
This series of posts is dedicated to building tools that support a simulation of simplified D&D gameplay. To implement the simulation I’ll be using git, Python, PostgreSQL, Elastic, and Docker. The idea behind these blog posts is to document my progress through this project and interesting bits I find along the way. If you’d like to start from the beginning, have a look at part 1, where I talked about where this idea came from, and what I’d like to accomplish. The github repository for this project holds the latest version of the files mentioned in these posts. To follow along, part 6 describes how to set up the environment In this part, I’ll be setting up the process of how objects are build for and data loaded into the database.
Part 6 talked about the commands used to set up the directory structure. To follow along while the posts are actively being done, it’s a good idea to get all the updates since the last time.
(rpg) ~$ cd ~/Git/python_rpg_sim (rpg) ~/Git/python_rpg_sim$ git pull
If the database container isn’t running, now would be a good time to start it.
(rpg) ~/Git/python_rpg_sim$ cd docker (rpg) ~/Git/python_rpg_sim/docker$ docker-compose up -d pgs Creating network "docker_default" with the default driver Creating pgs ... done
I am using a database source control product called Liquibase. It allows me to manipulate the objects as well as the data in the database without worrying about the db specific commands and applying those changes is done with a couple of commands. That way the building of the database for this series should be as straightforward as possible.
Liquibase uses jdbc to connect to the database, which doesn’t mean much more than in setting it up, the jdbc driver needs to be placed somewhere it can find it and the definition of the places to look, the classpath has to be done. Create a directory for the Liquibase application to live and extract the distrubutions in the repository there.
(rpg) ~/Git/python_rpg_sim$ cd ~/rpg (rpg) ~/rpg$ mkdir liquibase (rpg) ~/rpg$ cd liquibase (rpg) ~/rpg/liquibase$ unzip ~/Git/python_rpg_sim/liquibase/liquibase-3.4.2-bin.zip ... (rpg) ~/rpg/liquibase$ cp ~/Git/python_rpg_sim/liquibase/postgresql-42.2.2.jar . (rpg) ~/rpg/liquibase$ export PATH=~/rpg/liquibase:$PATH (rpg) ~/rpg/liquibase$ which liquibase ~/rpg/liquibase/liquibase (rpg) ~/rpg/liquibase$ liquibase --version Liquibase Version: 3.4.2
No matter which OS is being used, adding the liquibase executable to the PATH environment variable will allow for it to be called with a minimum of drama.
The setup of where Liquibase finds the classes it uses, like the PostgreSQL jdbc driver, happens in a file called “liquibase.properties”. I’ve provided a mostly complete one: python_rpg_sim/liquibase/changelog/liquibase.properties.example. The only thing missing is the classpath. Unfortunately, it uses an absolute path for it’s definition. Using “~” as a reference to my home directory didn’t seem to work. So, copy the example file over to the correct name and edit the file changing “<putRestOfPathHere>” to the user’s home directory.
(rpg) ~/rpg/liquibase$ cd ~/Git/python_rpg_sim/liquibase/changelog (rpg) ~/Git/python_rpg_sim/liquibase/changelog$ cp liquibase.properties.example liquibase.properties (rpg) ~/Git/python_rpg_sim/liquibase/changelog$ vi liquibase.properties
After setting the classpath correctly, validating and loading the current data should work without issue.
(rpg) ~/Git/python_rpg_sim/liquibase/changelog$ ~/rpg/liquibase/liquibase validate No validation errors found Liquibase 'validate' Successful (rpg) ~/Git/python_rpg_sim/liquibase/changelog$ ~/rpg/liquibase/liquibase migrate Liquibase Update Successful
Liquibase Update Successful means that the data has been added to the database. Take some time to look around in the database now using DBeaver or your favorite DB front end. Next post I get to talk D&D again and look at character race and how it plays into the simulation. Yay! Until then, go play!