| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # ABSTRACT: evolve - evolve your schema to the latest version | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use FindBin '$Bin'; | ||
| 8 | use lib "$Bin/../lib"; | ||
| 9 | |||
| 10 | use SchemaEvolution; | ||
| 11 | |||
| 12 | my $app = SchemaEvolution->new_with_options; | ||
| 13 | $app->run; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Schema evolution is a critical part of any developers work - the process of | ||
| 18 | changing the structure of a database overtime. However, while there are tools to | ||
| 19 | help with this task, the are not many that integrate well with version control | ||
| 20 | systems. | ||
| 21 | |||
| 22 | SchemaEvolution is a very basic command line application that helps automate | ||
| 23 | this process, while allowing you to also track changes in version control. | ||
| 24 | |||
| 25 | Each change to the database is tracked in a single SQL file, which are ordered | ||
| 26 | numerically. The database itself has a special column (schema_version.version by | ||
| 27 | default) that maintains track of the current version of the database, and the | ||
| 28 | C<evolve> command simply applies the set of SQL files after this version. | ||
| 29 | |||
| 30 | =head1 TUTORIAL | ||
| 31 | |||
| 32 | Let's briefly use evolve to track a small project - a little application that | ||
| 33 | has users and messages, and messages can be sent between users. The first thing | ||
| 34 | we need to do is create our C<evolution.ini> file. This configuration file tells | ||
| 35 | evolve how to connect to our database. | ||
| 36 | |||
| 37 | dsn = dbi:Pg:dbname=messaging | ||
| 38 | username = messaging | ||
| 39 | password = hello | ||
| 40 | |||
| 41 | The dsn string is the same format as used by L<DBI>, in this case we're using | ||
| 42 | the PostgresSQL driver to connect to the messaging database, with the | ||
| 43 | credentials "messaging" and "hello" | ||
| 44 | |||
| 45 | Next, we need to add the version column into our database. By default this is | ||
| 46 | the version column in the schema_version table. We can either enter the SQL by | ||
| 47 | hand... | ||
| 48 | |||
| 49 | CREATE TABLE schema_version ( | ||
| 50 | version INTEGER NOT NULL DEFAULT 0 | ||
| 51 | ); | ||
| 52 | INSERT INTO schema_version VALUES (0); | ||
| 53 | |||
| 54 | Or we can just let evolve.pl do it for us: | ||
| 55 | |||
| 56 | evolve.pl --initialize | ||
| 57 | |||
| 58 | Great! Now we can start writing some schema evolutions. The first thing we need | ||
| 59 | in our system is support for users. Let's go with something basic. In our | ||
| 60 | application directory, create the folder "evolutions" and start with our first | ||
| 61 | definition, 1_user.sql | ||
| 62 | |||
| 63 | CREATE TABLE 'user' ( | ||
| 64 | id SERIAL PRIMARY KEY, | ||
| 65 | name TEXT NOT NULL | ||
| 66 | ); | ||
| 67 | |||
| 68 | Now, with our first schema evolution, all we have to do is run "evolve". As | ||
| 69 | we're at version 0 in our database, 1_user.sql will be applied. Now you can go | ||
| 70 | ahead and write your model in Perl code to access this table (DBIx::Class, Fey, | ||
| 71 | or whatever takes your fancy). Now we can check this in to our version control | ||
| 72 | system and Bob Other Developer can grab our code and get his database up to the | ||
| 73 | same as ours, great! | ||
| 74 | |||
| 75 | Likewise, we can continue with our messages table: | ||
| 76 | |||
| 77 | CREATE TABLE 'messages' ( | ||
| 78 | id SERIAL PRIMARY KEY, | ||
| 79 | to INT NOT NULL, | ||
| 80 | from INT NOT NULL, | ||
| 81 | message TEXT NOT NULL | ||
| 82 | ); | ||
| 83 | |||
| 84 | We just evolve again, and voila, we have a messages table. Again, our other | ||
| 85 | developers can just update their version control, run C<evolve> and then be up | ||
| 86 | to date ready to run code. New developers can do the same; for the them the | ||
| 87 | database would first get 1_user.sql applied, and then 2_messages.sql | ||
| 88 | |||
| 89 | =cut |