Created — forked from dkubb/data_objects_schema.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View data_objects_schema.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
# DataObjects::Schema::
# Database
# Table
#
# Column::
# String < Column
# Numeric < Column
# Integer < Numeric
# Float < Numeric
# Decimal < Numeric
# ...
#
# Index
# UniqueIndex < Index
# PrimaryKey < UniqueIndex
# ForeignKey
# Reference
#
# OrderedSet
# Columns (has-a OrderedSet)
# Indexes (has-a OrderedSet)
# UniqueIndexes (has-a OrderedSet)
# ForeignKeys (has-a OrderedSet)
 
uri = Addressable::URI.parse('mysql://localhost/dm_core_test')
 
database = DataObjects::Schema.load(uri)
 
database.name # => "dm_core_test"
database.uri # => uri
database.tables # => Tables.new([ Table1, Table2, ... ])
 
database.tables.each do |table|
 
# Table API
table # => Table.new('customers', [ Column1, Column2, ... ], :indexes => indexes)
table.name # => "customers"
table.to_ddl # => CREATE TABLE customers (id INT(10) UNSIGNED NOT NULL, name CHAR(10) NOT NULL DEFAULT 'a', PRIMARY KEY(id))
 
# Column API
table.columns # => Columns.new([ Column1, Column2, ... ])
table.columns.to_ddl # => "id INT(10) UNSIGNED NOT NULL, name CHAR(10) NOT NULL DEFAULT 'a'"
 
table.each do |column|
column # => Column::String.new('name', :length => 0..10)
column.name # => "name"
column.required? # => true
column.default # => "a"
column.to_ddl # => "name CHAR(10) NOT NULL DEFAULT 'a'"
 
# NOTE: Column subclasses will be able to add extra accessors for type
# specific data. For example Column::String may define #length,
# and Column::Decimal may define #precision and #scale
end
 
# Primary Key API
table.primary_key # => PrimaryKey.new(name, [ Column1, Column2, ... ])
table.primary_key.columns # => Columns.new([ Column1, Column2, ... ])
table.primary_key.unique? # => true
table.primary_key.to_ddl # => "PRIMARY KEY(id)"
 
# Index API
table.indexes # => Indexes.new([ Index1, Index2, ... ])
table.indexes.to_ddl # => "KEY index_name1 (column_name1), KEY index_name2 (column_name2)"
 
table.indexes.each do |index|
index # => Index.new('index_name', [ Column1, Column2, ... ])
index.name # => "index_name"
index.unique? # => false
index.columns # => Columns.new([ Column1, Column2, ... ])
index.to_ddl # => "KEY index_name (column_name)"
end
 
# Unique Index API
table.unique_indexes # => UniqueIndexes.new([ UniqueIndex1, UniqueIndex1, ... ])
table.unique_indexes.to_ddl # => "UNIQUE unique_name1 (column_name1), UNIQUE unique_name2 (column_name2)"
 
table.unique_indexes.each do |unique_index|
unique_index # => UniqueIndex.new('unique_name', [ Column1, Column2, ... ])
unique_index.name # => "unique_name"
unique_index.unique? # => true
unique_index.columns # => Columns.new([ Column1, Column2, ... ])
unique_index.to_ddl # => "UNIQUE unique_name (column_name)"
end
 
# Foreign Key API
table.foreign_keys # => ForeignKeys.new([ ForeignKey1, ForeignKey2, ... ])
table.foreign_keys.to_ddl # => "FOREIGN KEY (parent_id) REFERENCES parent (id), FOREIGN KEY (other_id) REFERENCES other (id)"
 
table.foreign_keys.each do |foreign_key|
foreign_key # => ForeignKey.new('foreign_key_name', [ Column1, Column2, ... ], reference, :on_delete => :restrict, :on_update => :restrict)
foreign_key.name # => "foreign_key_name"
foreign_key.columns # => Columns.new([ Column1, Column2, ... ])
foreign_key.reference # => Reference.new(other_table, other_table.primary_key)
foreign_key.on_delete # => :restrict
foreign_key.on_update # => nil
foreign_key.to_ddl # => "FOREIGN KEY foreign_key_name (other_id) REFERENCES others (id)"
end
 
end
 
# NOTES: Table, Index, UniqueIndex, PrimaryKey, ForeignKey and Reference
# are Enumerable, and delegate #each to #columns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.