sqlserver-schema-reflector 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
+ require './lib/sqlserver_schema_reflector.rb'
2
3
  task :run do
3
- require './lib/sqlserver_schema_reflector.rb'
4
4
  SqlServerSchemaReflector::Reflector.run(
5
5
  File.open('./schema.sql', 'w'),
6
6
  :username => 'user',
@@ -12,3 +12,11 @@ end
12
12
  task :test do
13
13
  system 'bundle exec rspec spec/sqlserver_schema_reflector_test.rb'
14
14
  end
15
+
16
+ task :build do
17
+ system 'gem build sqlserver_schema_reflector.gemspec'
18
+ end
19
+
20
+ task :publish => :build do
21
+ system "gem push sqlserver-schema-reflector-#{SqlServerSchemaReflector::VERSION}.gem"
22
+ end
@@ -1,17 +1,21 @@
1
1
  class SqlServerSchemaReflector::Reflector
2
2
  def self.run(out, options)
3
3
  client = TinyTds::Client.new(options)
4
- names = client.execute("SELECT ([TABLE_SCHEMA] + '.' + [TABLE_NAME]) AS name FROM information_schema.tables").map {|r| r['name']}
5
- names.each do |name|
4
+ tables = client.execute(
5
+ "SELECT [TABLE_SCHEMA] as [schema],
6
+ [TABLE_NAME] as [name]
7
+ FROM information_schema.tables"
8
+ ).map {|r| r.symbolize_keys }
9
+ tables.each do |t|
6
10
  begin
7
- table_hash = client.execute("sp_help '#{name}'").map { |result| result }
8
- table = SqlServerSchemaReflector::TableReflection.new(table_hash)
11
+ cmd = "sp_help " + "'#{t[:schema]}.#{t[:name]}'"
12
+ table_hash = client.execute(cmd).map { |result| result }
13
+ table = SqlServerSchemaReflector::TableReflection.new(table_hash, t)
9
14
  out.write table.create_sql + "\n"
10
- puts "Successfully generated create statement for #{name}"
11
15
  rescue
12
16
  e = "/*\n"
13
- e << "Unable to generate create statement for table: #{name}\n"
14
- e << $!
17
+ e << "Unable to generate create statement for table: #{t}\n"
18
+ e << $!.message
15
19
  e << "*/\n"
16
20
  out.write e
17
21
  $stderr.print e
@@ -1,6 +1,11 @@
1
1
  class SqlServerSchemaReflector::TableReflection
2
- def initialize(properties)
2
+ def initialize(properties, options = {})
3
3
  @properties = properties
4
+ defaults = {
5
+ :schema => meta[:owner]
6
+ }
7
+ defaults.merge! options
8
+ meta.merge! defaults
4
9
  end
5
10
  def meta
6
11
  @meta ||= @properties.find_hashes [:name, :owner, :type]
@@ -39,7 +44,7 @@ class SqlServerSchemaReflector::TableReflection
39
44
  @constraints
40
45
  end
41
46
  def create_sql
42
- sql = "CREATE TABLE [#{meta[:owner]}].[#{meta[:name]}]("
47
+ sql = "CREATE TABLE [#{meta[:schema]}].[#{meta[:name]}]("
43
48
  columns.each do |c|
44
49
  sql << "\n\t#{c.sql},"
45
50
  end
@@ -1,3 +1,3 @@
1
- module SqlserverSchemaReflector
2
- VERSION = "0.0.1"
1
+ module SqlServerSchemaReflector
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,55 +2,108 @@
2
2
  [
3
3
  [{"Name":"employees","Owner":"dbo","Type":"user table","Created_datetime":"2011-07-19 14:08:32 -0600"}],
4
4
  [
5
- {
6
- "Column_name":"id",
7
- "Type":"int",
8
- "Computed":"no",
9
- "Length":4,
10
- "Prec":"10",
11
- "Scale":"0",
12
- "Nullable":"no",
13
- "TrimTrailingBlanks":"(n/a)",
14
- "FixedLenNullInSource":"(n/a)",
15
- "Collation":null
5
+ {
6
+ "Column_name":"id",
7
+ "Type":"int",
8
+ "Computed":"no",
9
+ "Length":4,
10
+ "Prec":"10",
11
+ "Scale":"0",
12
+ "Nullable":"no",
13
+ "TrimTrailingBlanks":"(n/a)",
14
+ "FixedLenNullInSource":"(n/a)",
15
+ "Collation":null
16
16
  },
17
- {
18
- "Column_name":"first_name",
19
- "Type":"varchar",
20
- "Computed":"no",
21
- "Length":255,
22
- "Prec":"",
23
- "Scale":"",
24
- "Nullable":"yes",
25
- "TrimTrailingBlanks":"no",
26
- "FixedLenNullInSource":"yes",
27
- "Collation":"SQL_Latin1_General_CP1_CI_AS"
28
- },
29
- {
30
- "Column_name":"last_name",
31
- "Type":"varchar",
32
- "Computed":"no",
33
- "Length":255,
34
- "Prec":"",
35
- "Scale":"",
36
- "Nullable":"yes",
37
- "TrimTrailingBlanks":"no",
38
- "FixedLenNullInSource":"yes",
39
- "Collation":"SQL_Latin1_General_CP1_CI_AS"
40
- },
41
- {
42
- "Column_name":"suffix",
43
- "Type":"varchar",
44
- "Computed":"no",
45
- "Length":255,
46
- "Prec":" ",
47
- "Scale":" ",
48
- "Nullable":"yes",
49
- "TrimTrailingBlanks":"no",
50
- "FixedLenNullInSource":"yes",
51
- "Collation":"SQL_Latin1_General_CP1_CI_AS"
52
- }
53
- ],
17
+ {
18
+ "Column_name":"first_name",
19
+ "Type":"varchar",
20
+ "Computed":"no",
21
+ "Length":255,
22
+ "Prec":"",
23
+ "Scale":"",
24
+ "Nullable":"yes",
25
+ "TrimTrailingBlanks":"no",
26
+ "FixedLenNullInSource":"yes",
27
+ "Collation":"SQL_Latin1_General_CP1_CI_AS"
28
+ },
29
+ {
30
+ "Column_name":"last_name",
31
+ "Type":"varchar",
32
+ "Computed":"no",
33
+ "Length":255,
34
+ "Prec":"",
35
+ "Scale":"",
36
+ "Nullable":"yes",
37
+ "TrimTrailingBlanks":"no",
38
+ "FixedLenNullInSource":"yes",
39
+ "Collation":"SQL_Latin1_General_CP1_CI_AS"
40
+ },
41
+ {
42
+ "Column_name":"suffix",
43
+ "Type":"varchar",
44
+ "Computed":"no",
45
+ "Length":255,
46
+ "Prec":" ",
47
+ "Scale":" ",
48
+ "Nullable":"yes",
49
+ "TrimTrailingBlanks":"no",
50
+ "FixedLenNullInSource":"yes",
51
+ "Collation":"SQL_Latin1_General_CP1_CI_AS"
52
+ }
53
+ ],
54
+ [
55
+ {
56
+ "Identity":"id",
57
+ "Seed":"0.1E1",
58
+ "Increment":"0.1E1",
59
+ "Not For Replication":0
60
+ }
61
+ ],
62
+ [
63
+ {
64
+ "RowGuidCol":"No rowguidcol column defined."
65
+ }
66
+ ],
67
+ [
68
+ {
69
+ "Data_located_on_filegroup":"PRIMARY"
70
+ }
71
+ ],
72
+ [
73
+ {
74
+ "index_name":"PK__employee__3213E83F023D5A04",
75
+ "index_description":"clustered, unique, primary key located on PRIMARY",
76
+ "index_keys":"id"
77
+ }
78
+ ],
79
+ [
80
+ {
81
+ "constraint_type":"PRIMARY KEY (clustered)",
82
+ "constraint_name":"PK__employee__3213E83F023D5A04",
83
+ "delete_action":"(n/a)",
84
+ "update_action":"(n/a)",
85
+ "status_enabled":"(n/a)",
86
+ "status_for_replication":"(n/a)",
87
+ "constraint_keys":"id"
88
+ }
89
+ ]
90
+ ],
91
+ [
92
+ [{"Name":"employees","Owner":"austin.smith","Type":"user table","Created_datetime":"2011-07-19 14:08:32 -0600"}],
93
+ [
94
+ {
95
+ "Column_name":"id",
96
+ "Type":"int",
97
+ "Computed":"no",
98
+ "Length":4,
99
+ "Prec":"10",
100
+ "Scale":"0",
101
+ "Nullable":"no",
102
+ "TrimTrailingBlanks":"(n/a)",
103
+ "FixedLenNullInSource":"(n/a)",
104
+ "Collation":null
105
+ }
106
+ ],
54
107
  [
55
108
  {
56
109
  "Identity":"id",
@@ -1,17 +1,20 @@
1
1
  require 'sqlserver_schema_reflector'
2
2
 
3
- def employees_table_def
4
- @employees_table ||= JSON.parse(File.read('./spec/fixtures/object_definitions.json'))[0]
3
+ def employees_table_defs
4
+ @employees_table ||= JSON.parse(File.read('./spec/fixtures/object_definitions.json'))
5
5
  end
6
6
 
7
- def table_reflection
8
- SqlServerSchemaReflector::TableReflection.new(employees_table_def)
7
+ def reflections
8
+ {
9
+ :good => SqlServerSchemaReflector::TableReflection.new(employees_table_defs[0]),
10
+ :bad_owner => SqlServerSchemaReflector::TableReflection.new(employees_table_defs[1], :name => 'employees', :schema => 'dbo')
11
+ }
9
12
  end
10
13
 
11
14
  def column_reflection
12
15
  SqlServerSchemaReflector::ColumnReflection.new(
13
- employees_table_def.find_hashes([:column_name])[0],
14
- table_reflection.identity
16
+ employees_table_defs[0].find_hashes([:column_name])[0],
17
+ reflections[:good].identity
15
18
  )
16
19
  end
17
20
 
@@ -20,37 +23,44 @@ describe SqlServerSchemaReflector do
20
23
 
21
24
  describe "#new" do
22
25
  it "creates an object with correct meta data" do
23
- table_reflection.meta.should be_a(Hash)
24
- table_reflection.meta[:name].should == "employees"
25
- table_reflection.meta[:owner].should == "dbo"
26
- table_reflection.meta[:type].should == "user table"
26
+ reflections[:good]
27
+ reflections[:good].meta.should be_a(Hash)
28
+ reflections[:good].meta[:name].should == "employees"
29
+ reflections[:good].meta[:owner].should == "dbo"
30
+ reflections[:good].meta[:type].should == "user table"
31
+ reflections[:good].meta[:schema].should == 'dbo'
32
+
33
+ reflections[:bad_owner].meta[:owner].should === 'austin.smith'
34
+ reflections[:bad_owner].meta[:schema].should === 'dbo'
35
+
36
+
27
37
  end
28
38
  it "creates an array of column reflection objects" do
29
- table_reflection.columns.should be_an(Array)
30
- table_reflection.columns.length.should == 4
31
- table_reflection.columns[0].should be_a(SqlServerSchemaReflector::ColumnReflection)
32
- table_reflection.columns[0].name.should == "id"
33
- table_reflection.columns[0].type.should == 'int'
39
+ reflections[:good].columns.should be_an(Array)
40
+ reflections[:good].columns.length.should == 4
41
+ reflections[:good].columns[0].should be_a(SqlServerSchemaReflector::ColumnReflection)
42
+ reflections[:good].columns[0].name.should == "id"
43
+ reflections[:good].columns[0].type.should == 'int'
34
44
  end
35
45
  it "creates an object with correct identity data" do
36
- table_reflection.identity[:identity].should === 'id'
37
- table_reflection.identity[:seed].should === 1
38
- table_reflection.identity[:increment].should === 1
39
- table_reflection.identity[:not_for_replication].should == 0
46
+ reflections[:good].identity[:identity].should === 'id'
47
+ reflections[:good].identity[:seed].should === 1
48
+ reflections[:good].identity[:increment].should === 1
49
+ reflections[:good].identity[:not_for_replication].should == 0
40
50
  end
41
51
  it "creates an object with correct index data" do
42
- table_reflection.indexes.length.should === 1
43
- table_reflection.indexes[0][:index_name].should === 'PK__employee__3213E83F023D5A04'
44
- table_reflection.indexes[0][:index_description].should === 'clustered, unique, primary key located on PRIMARY'
45
- table_reflection.indexes[0][:index_keys].should === 'id'
52
+ reflections[:good].indexes.length.should === 1
53
+ reflections[:good].indexes[0][:index_name].should === 'PK__employee__3213E83F023D5A04'
54
+ reflections[:good].indexes[0][:index_description].should === 'clustered, unique, primary key located on PRIMARY'
55
+ reflections[:good].indexes[0][:index_keys].should === 'id'
46
56
  end
47
57
  it "creates an object with correct constraints" do
48
- table_reflection.constraints.length.should === 1
49
- table_reflection.constraints[0][:constraint_name].should === "PK__employee__3213E83F023D5A04"
50
- table_reflection.constraints[0][:constraint_type].should === "PRIMARY KEY (clustered)"
58
+ reflections[:good].constraints.length.should === 1
59
+ reflections[:good].constraints[0][:constraint_name].should === "PK__employee__3213E83F023D5A04"
60
+ reflections[:good].constraints[0][:constraint_type].should === "PRIMARY KEY (clustered)"
51
61
  end
52
62
  it "generates proper sql create table statement" do
53
- table_reflection.create_sql.should === "" <<
63
+ reflections[:good].create_sql.should === "" <<
54
64
  "CREATE TABLE [dbo].[employees](\n" <<
55
65
  "\t[id] [int] IDENTITY(1,1) NOT NULL,\n" <<
56
66
  "\t[first_name] [varchar](255) NULL,\n" <<
@@ -71,10 +81,10 @@ describe SqlServerSchemaReflector do
71
81
  column_reflection.identity[:increment].should === 1
72
82
  end
73
83
  it "creates the proper sql column declaration" do
74
- table_reflection.columns[0].sql.should === "[id] [int] IDENTITY(1,1) NOT NULL"
75
- table_reflection.columns[1].sql.should === "[first_name] [varchar](255) NULL"
76
- table_reflection.columns[2].sql.should === "[last_name] [varchar](255) NULL"
77
- table_reflection.columns[3].sql.should === '[suffix] [varchar](255) NULL'
84
+ reflections[:good].columns[0].sql.should === "[id] [int] IDENTITY(1,1) NOT NULL"
85
+ reflections[:good].columns[1].sql.should === "[first_name] [varchar](255) NULL"
86
+ reflections[:good].columns[2].sql.should === "[last_name] [varchar](255) NULL"
87
+ reflections[:good].columns[3].sql.should === '[suffix] [varchar](255) NULL'
78
88
  end
79
89
  end
80
90
  end
@@ -4,7 +4,7 @@ require "sqlserver_schema_reflector/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "sqlserver-schema-reflector"
7
- s.version = SqlserverSchemaReflector::VERSION
7
+ s.version = SqlServerSchemaReflector::VERSION
8
8
  s.authors = ["Jacob Morris"]
9
9
  s.email = ["jacob.s.morris@gmail.com"]
10
10
  s.homepage = "http://www.thewhitespace.co"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlserver-schema-reflector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-21 00:00:00.000000000 -06:00
13
- default_executable:
12
+ date: 2011-07-21 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: json
17
- requirement: &29136084 !ruby/object:Gem::Requirement
16
+ requirement: &27856644 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *29136084
24
+ version_requirements: *27856644
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: json_pure
28
- requirement: &29135832 !ruby/object:Gem::Requirement
27
+ requirement: &27856392 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *29135832
35
+ version_requirements: *27856392
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: tiny_tds
39
- requirement: &29135580 !ruby/object:Gem::Requirement
38
+ requirement: &27856140 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: '0'
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *29135580
46
+ version_requirements: *27856140
48
47
  description: Analyzing any SqlServer database and produce a sql script for replicating
49
48
  the database
50
49
  email:
@@ -67,7 +66,6 @@ files:
67
66
  - spec/fixtures/object_definitions.json
68
67
  - spec/sqlserver_schema_reflector_test.rb
69
68
  - sqlserver_schema_reflector.gemspec
70
- has_rdoc: true
71
69
  homepage: http://www.thewhitespace.co
72
70
  licenses: []
73
71
  post_install_message:
@@ -88,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  version: '0'
89
87
  requirements: []
90
88
  rubyforge_project: sqlserver_schema_reflector
91
- rubygems_version: 1.5.2
89
+ rubygems_version: 1.8.5
92
90
  signing_key:
93
91
  specification_version: 3
94
92
  summary: Analyze any SqlServer database and produce a sql script for replicating the