Giter Site home page Giter Site logo

blythedunham / mysql_migration_optimizer Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 3.0 85 KB

http://snowgiraffe.com/rdocs/mysql_migration_optimizer/index.html

Home Page: http://snowgiraffe.com/rdocs/mysql_migration_optimizer/index.html

License: MIT License

Ruby 100.00%

mysql_migration_optimizer's Introduction

MySql Migration Optimizer

Extends the MySQL connector to provide:

    * Support for unsigned integer values through the precision parameter
    * Support for column display width through the scale parameter
    * Support for customization of the primary key value through primary_column hash specified to create_table

UNSIGNED integers

By default, use unsigned are used for integers (and booleans) unless :precision is set to :signed or globally configured by setting the default_sign in environment.rb

  MySqlMigrationOptimizer.default_sign = :signed

Since foreign keys should match, it is a good idea to set the default to :signed if you have already established your app

 add_column :giraffe, :beer_count, :integer, :precision => :signed
 add_column :giraffe, :beer_count, :integer, :precision => :unsigned

 create_table "giraffe" do |t|
   t.integer "neck_length", :scale => 8, :precision => :signed
   t.boolean "has_spots", :default => true
 end

Generated SQL:

 CREATE TABLE `giraffe` (
   `id` int(11) UNSIGNED NOT NULL auto_increment PRIMARY KEY,
   `neck_length` int(8), `has_spots` tinyint(1) UNSIGNED DEFAULT 1
 ) ENGINE=InnoDB

Integer Display width

To set the integer display width, use :scale => XXX Display width is the max number of digits that MySQL will display and is not indicative of the storage size which is determined by the :limit option

 add_column :giraffe, :beer_count, :integer, :scale => 8
 SQL: ALTER TABLE `giraffe` ADD `beer_count` int(8) UNSIGNED

Note that the type of integer tinyint, smallint, mediumint, int, and bigint are determined by the :limit from 1, 2, 3, 4, 5-8 respectively Therefore the following are equivalent

 add_column :giraffe, :beer_count, :smallint
 add_column :giraffe, :beer_count, :integer, :limit => 2

 SQL: ALTER TABLE `giraffe` ADD `beer_count` smallint UNSIGNED

In addition to the normal options of create_table, additional option of :primary_column can specify column attributes for the primary column. By default an unsigned int(11) is used
Create Table :primary_column Hash Map Options

create_table allows for an additional map param :primary_column of options to define the primary key column.

    * :name - the same as specifying the :primary_key in the parent map
    * :scale - the scale of the variable
    * :precision - precision of the column. Set to :signed or :unsigned for integers and booleans. Defaults to the value of MySqlMigrationOptimizer.default_sign which is originaly set to :unsigned
    * :type - the type of column. Defaults to :integer
    * :null - set to false if the column is not nullable
    * :null - default value of the column
    * :auto_increment - Defaulted to true, set to false to turn off auto increment

Specify a string column as the primary key

 create_table "blah", :force => true,
    :primary_column => {:type=>:string, :limit=>25, :auto_increment=>false}  do |t|#
 end
 SQL: CREATE TABLE `blah` (`id` varchar(25) NOT NULL PRIMARY KEY) ENGINE=InnoDB

Specify the primary column as an unsigned (default) integer called special key with display width of 3

 create_table "animal", :primary_key => "special_key", :force => true,
  :primary_column => {:type=>:integer, :limit => 2, :scale=>3}  do |t|
 end

 SQL: CREATE TABLE `animal` (`special_key` smallint(3) UNSIGNED NOT NULL auto_increment PRIMARY KEY) ENGINE=InnoDB

Developers
    * Blythe Dunham http://snowgiraffe.com

Homepage
    * Homepage: http://www.snowgiraffe.com/tech/?tag=mysql_migration_optimizer
    * Rdoc: http://snowgiraffe.com/rdocs/mysql_migration_optimizer/index.html
    * GitHub Project: http://github.com/blythedunham/mysql_migration_optimizer/tree/master
    * Plugin Install: script/plugin git://github.com/blythedunham/mysql_migration_optimizer.git

Copyright (c) 2009 Blythe Dunham, released under the MIT license

mysql_migration_optimizer's People

Contributors

blythedunham avatar massimiliano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mysql_migration_optimizer's Issues

Error in SchemaDumper when Migrating back

I noticed today when I had to rollback some migrations that the SchemaDumper died. Running it with trace on showed the problem was in the primary_column_schema_options method. I didn't really scientifically fix this, but adding checks to make sure the "primary_column" var was not nil seemed to do the trick, and looking over the dumped schema all seemed fine.

So...
Line 201, from:
value = primary_column.send(method)
to:
value = primary_column.send(method) if primary_column

Line 206, from:
options[:auto_increment] = false unless primary_column.extras.to_s.include?('auto_increment')

to:
options[:auto_increment] = false unless primary_column && primary_column.extras.to_s.include?('auto_increment')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.