Browse files

added textile usage doc

  • Loading branch information...
1 parent 8f28fef commit 6dbbec3823b31aadf666efff02fe0880ab286205 @nickinuse committed Jan 24, 2010
Showing with 107 additions and 1 deletion.
  1. +1 −1 README
  2. +106 −0 usage.textile
@@ -1,5 +1,5 @@
Ruby on Rails-like database migration management for PHP
- (c) 2009
+ (c) 2009/2010
@@ -0,0 +1,106 @@
+config.sample.php contains an implementation for the mysql(i) adapter functions. Rename the file to config.php, provide your database credentials and You should be good to go.
+Some basic usage:
+The name of the migration should be CamelCased and/or under_scored as it'll be converted to a class name. Launch a console/terminal at the folder that contains the migrations.php and config.php file.
+<pre>php -f migrate.php generate SampleTable</pre>
+(this is the same as: php -f migrate.php generate sample_table or php -f migrate.php generate Sample_Table).
+A convenience batch file (either db.bat or [you should chmod it executable]) is provided, so you may type "db" instead of "php -f migrate.php" e.g.:
+<pre>db generate sample_table</pre>
+This should create a folder subfolder called "migrate" and a file named [bunch of numbers]_sample_table.php. The "bunch of numbers" is actually the file's timestamp in yyyymmddhhiiss format.
+The file is a blank template for your migration and initially looks like:
+class SampleTable
+ function up(){//add changes
+ }
+ function down(){//revert changes
+ }
+Let's create a sample migration with a table structure for some articles:
+class SampleTable {
+ function up() {//add changes
+ create_table('sample_table',array( //of columns
+ //mysql doesn't allow defaults for *char type columns, so this should create a trigger
+ t_string('title', array('default'=>'Untitled' ) ),//short column, e.g. varchar(255)
+ t_text('body'),//long column, e.g. TEXT/BLOB
+ t_integer('comments',array('default'=>0)),//to store comment count
+ t_boolean('published'),
+ //magic method that adds created/updated_at columns and triggers to update them
+ t_timestamps()
+ ));
+ }
+ function down() {//revert changes
+ drop_table('sample_table');
+ }
+to apply the changes and create the table run:
+<pre>db migrate up</pre>
+ or simply
+<pre>db migrate</pre>
+and to undo them
+<pre>db migrate down</pre>
+ or
+ <pre>db roll -1</pre>
+ or even (since there are no other migrations yet, roll back all migrations)
+ <pre>db migrate zero</pre>
+h2. Other methods you might often use
+<pre>drop_table($table_name) e.g.
+add_column($table,$column_definition) e.g.
+remove_column($table,*$column_names) t.i. you can pass several columns to remove
+rename_column($table,$column,$column_definition), e.g.
+rename_column('tags', 'tag', t_string('keyword',array('index'=>true)) );
+add_index('table', array('field1','field2'), array('unique'=>true)); //index name will be set to 'field1_field2'
+add_index('table', array('field1','field2'), array('unique'=>true,'name'=>'composite_name'));
+h2. Common column types, shorthand versions
+You can use the following helper functions for column definitions. They are essentially the same as calling t_column($column_name,$type=[string|text|integer...],[$options])
+A special case is
+that creates both the created_at/updated_at columns and according triggers to keep track of db record updates.

0 comments on commit 6dbbec3

Please sign in to comment.