evolve - evolve your schema to the latest version
Schema evolution is a critical part of any developers work - the process of changing the structure of a database overtime. However, while there are tools to help with this task, the are not many that integrate well with version control systems.
SchemaEvolution is a very basic command line application that helps automate this process, while allowing you to also track changes in version control.
Each change to the database is tracked in a single SQL file, which are ordered
numerically. The database itself has a special column (schema_version.version by
default) that maintains track of the current version of the database, and the
evolve
command simply applies the set of SQL files after this version.
Let's briefly use evolve to track a small project - a little application that
has users and messages, and messages can be sent between users. The first thing
we need to do is create our evolution.ini
file. This configuration file tells
evolve how to connect to our database.
dsn = dbi:Pg:dbname=messaging username = messaging password = hello
The dsn string is the same format as used by DBI, in this case we're using the PostgresSQL driver to connect to the messaging database, with the credentials "messaging" and "hello"
Next, we need to add the version column into our database. By default this is the version column in the schema_version table.
CREATE TABLE schema_version ( version INTEGER NOT NULL DEFAULT 0 );
Great! Now we can start writing some schema evolutions. The first thing we need in our system is support for users. Let's go with something basic. In our application directory, create the folder "evolutions" and start with our first definition, 1_user.sql
CREATE TABLE 'user' ( id SERIAL PRIMARY KEY, name TEXT NOT NULL );
Now, with our first schema evolution, all we have to do is run "evolve"