dbdiff 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/CHANGELOG +6 -0
  2. data/LICENSE +58 -0
  3. data/README +29 -0
  4. data/lib/dbdiff/column.rb +69 -0
  5. data/lib/dbdiff/database.rb +169 -0
  6. data/lib/dbdiff/delta.rb +266 -0
  7. data/lib/dbdiff/foreign_key.rb +68 -0
  8. data/lib/dbdiff/key.rb +69 -0
  9. data/lib/dbdiff/row.rb +32 -0
  10. data/lib/dbdiff/table.rb +50 -0
  11. data/lib/dbdiff/table_element.rb +16 -0
  12. data/lib/dbdiff.rb +207 -0
  13. data/test/ai_column/source.sql +10 -0
  14. data/test/ai_column/target.sql +7 -0
  15. data/test/change_pk/source.sql +11 -0
  16. data/test/change_pk/target.sql +8 -0
  17. data/test/column/source.sql +10 -0
  18. data/test/column/target.sql +9 -0
  19. data/test/fk/source.sql +19 -0
  20. data/test/fk/target.sql +20 -0
  21. data/test/key/source.sql +11 -0
  22. data/test/key/target.sql +10 -0
  23. data/test/modify_column/source.sql +9 -0
  24. data/test/modify_column/target.sql +10 -0
  25. data/test/modify_fk/source.sql +20 -0
  26. data/test/modify_fk/target.sql +22 -0
  27. data/test/modify_key_fk/source.sql +19 -0
  28. data/test/modify_key_fk/target.sql +20 -0
  29. data/test/modify_key_fk_ref/source.sql +18 -0
  30. data/test/modify_key_fk_ref/target.sql +20 -0
  31. data/test/modify_row/source.sql +8 -0
  32. data/test/modify_row/target.sql +9 -0
  33. data/test/modify_table/source.sql +8 -0
  34. data/test/modify_table/target.sql +9 -0
  35. data/test/multi_fk/source.sql +22 -0
  36. data/test/multi_fk/target.sql +20 -0
  37. data/test/multi_key/source.sql +11 -0
  38. data/test/multi_key/target.sql +10 -0
  39. data/test/multi_unique_key/source.sql +11 -0
  40. data/test/multi_unique_key/target.sql +10 -0
  41. data/test/row/source.sql +8 -0
  42. data/test/row/target.sql +6 -0
  43. data/test/suite.rb +7 -0
  44. data/test/table/source.sql +9 -0
  45. data/test/table/target.sql +0 -0
  46. data/test/table_fk/source.sql +13 -0
  47. data/test/table_fk/target.sql +20 -0
  48. data/test/table_fk2/source.sql +17 -0
  49. data/test/table_fk2/target.sql +5 -0
  50. data/test/test_column.rb +93 -0
  51. data/test/test_dbdiff.rb +92 -0
  52. data/test/test_foreign_key.rb +136 -0
  53. data/test/test_key.rb +116 -0
  54. data/test/test_row.rb +84 -0
  55. data/test/test_table.rb +30 -0
  56. data/test/unique_key/source.sql +11 -0
  57. data/test/unique_key/target.sql +9 -0
  58. metadata +127 -0
@@ -0,0 +1,9 @@
1
+
2
+ CREATE TABLE authors (
3
+ `id` int(11) NOT NULL auto_increment,
4
+ `name` char(60) NOT NULL,
5
+ PRIMARY KEY (`id`)
6
+ ) ENGINE=myisam DEFAULT CHARSET=latin1;
7
+
8
+
9
+
@@ -0,0 +1,22 @@
1
+
2
+ CREATE TABLE authors (
3
+ `id` int(11) NOT NULL,
4
+ id2 int not null,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ unique(id, id2),
8
+ PRIMARY KEY (`id`)
9
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
10
+
11
+ CREATE TABLE books (
12
+ id int(11) NOT NULL auto_increment,
13
+ author_id int not null,
14
+ author_id2 int not null,
15
+ name char(60) NOT NULL,
16
+ pages int not null,
17
+ FOREIGN KEY (author_id, author_id2) references authors(id, id2),
18
+ PRIMARY KEY (`id`)
19
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
20
+
21
+
22
+
@@ -0,0 +1,20 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ PRIMARY KEY (`id`)
8
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
9
+
10
+ CREATE TABLE books (
11
+ id int(11) NOT NULL auto_increment,
12
+ author_id int not null,
13
+ name char(60) NOT NULL,
14
+ pages int not null,
15
+ PRIMARY KEY (`id`)
16
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
17
+
18
+
19
+
20
+
@@ -0,0 +1,11 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ index(name, address1),
8
+ PRIMARY KEY (`id`)
9
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
10
+
11
+
@@ -0,0 +1,10 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ PRIMARY KEY (`id`)
8
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
9
+
10
+
@@ -0,0 +1,11 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ unique(name, address1),
8
+ PRIMARY KEY (`id`)
9
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
10
+
11
+
@@ -0,0 +1,10 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ `address1` char(60) NOT NULL,
7
+ PRIMARY KEY (`id`)
8
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
9
+
10
+
@@ -0,0 +1,8 @@
1
+ CREATE TABLE authors (
2
+ `id` int(11) NOT NULL auto_increment,
3
+ `name` char(60) NOT NULL,
4
+ `address` char(60) NULL,
5
+ PRIMARY KEY (`id`)
6
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
7
+
8
+ INSERT INTO authors (id, name, address) values (1, 'FOO', NULL);
@@ -0,0 +1,6 @@
1
+ CREATE TABLE authors (
2
+ `id` int(11) NOT NULL auto_increment,
3
+ `name` char(60) NOT NULL,
4
+ PRIMARY KEY (`id`)
5
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
6
+
data/test/suite.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+ require 'test_table'
3
+ require 'test_row'
4
+ require 'test_foreign_key'
5
+ require 'test_key'
6
+ require 'test_column'
7
+ require 'test_dbdiff'
@@ -0,0 +1,9 @@
1
+ DROP TABLE IF EXISTS `authors`;
2
+
3
+ CREATE TABLE authors (
4
+ `id` int(11) NOT NULL auto_increment,
5
+ `name` char(60) NOT NULL,
6
+ PRIMARY KEY (`id`)
7
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
8
+
9
+
File without changes
@@ -0,0 +1,13 @@
1
+
2
+ CREATE TABLE books (
3
+ id int(11) NOT NULL auto_increment,
4
+ author_id int not null,
5
+ name char(60) NOT NULL,
6
+ pages int not null,
7
+ PRIMARY KEY (`id`)
8
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
9
+
10
+
11
+
12
+
13
+
@@ -0,0 +1,20 @@
1
+
2
+ CREATE TABLE authors (
3
+ `id` int(11) NOT NULL auto_increment,
4
+ `name` char(60) NOT NULL,
5
+ PRIMARY KEY (`id`)
6
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
7
+
8
+
9
+ CREATE TABLE books (
10
+ id int(11) NOT NULL auto_increment,
11
+ author_id int not null,
12
+ name char(60) NOT NULL,
13
+ pages int not null,
14
+ FOREIGN KEY (author_id) references authors(id),
15
+ PRIMARY KEY (`id`)
16
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
17
+
18
+
19
+
20
+
@@ -0,0 +1,17 @@
1
+
2
+ CREATE TABLE authors (
3
+ `id` int(11) NOT NULL auto_increment,
4
+ `name` char(60) NOT NULL,
5
+ PRIMARY KEY (`id`)
6
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
7
+
8
+
9
+ CREATE TABLE books (
10
+ id int(11) NOT NULL auto_increment,
11
+ author_id int not null,
12
+ name char(60) NOT NULL,
13
+ pages int not null,
14
+ FOREIGN KEY (author_id) references authors(id),
15
+ PRIMARY KEY (`id`)
16
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
17
+
@@ -0,0 +1,5 @@
1
+ CREATE TABLE authors (
2
+ `id` int(11) NOT NULL auto_increment,
3
+ `name` char(60) NOT NULL,
4
+ PRIMARY KEY (`id`)
5
+ ) ENGINE=innodb DEFAULT CHARSET=latin1;
@@ -0,0 +1,93 @@
1
+ require 'dbdiff'
2
+ require 'test/unit'
3
+
4
+ class TestColumn < Test::Unit::TestCase
5
+
6
+ def test_load
7
+ c_ai = DbDiff::Column.new(
8
+ 'TABLE_NAME' => 'mytable',
9
+ 'COLUMN_NAME' => 'thisid',
10
+ 'DATA_TYPE' => 'int',
11
+ 'COLUMN_TYPE' => 'int(11)',
12
+ 'NUMERIC_PRECISION' => '10',
13
+ 'COLUMN_DEFAULT' => '5',
14
+ 'EXTRA' => 'auto_increment',
15
+ 'IS_NULLABLE' => 'NO'
16
+ )
17
+
18
+ assert(c_ai.table_name == 'mytable')
19
+ assert(c_ai.auto_increment )
20
+ assert(c_ai.not_null)
21
+ assert(c_ai.name == 'thisid')
22
+ assert(c_ai.default == '5')
23
+ assert(c_ai.column_type == 'int(11)')
24
+ assert(c_ai.data_type == 'int')
25
+
26
+ c = DbDiff::Column.new(
27
+ 'COLUMN_NAME' => 'thisid',
28
+ 'DATA_TYPE' => 'int',
29
+ 'COLUMN_TYPE' => 'int(11)',
30
+ 'NUMERIC_PRECISION' => '10',
31
+ 'COLUMN_DEFAULT' => '5'
32
+ )
33
+
34
+ assert(!c.auto_increment )
35
+ assert(!c.not_null)
36
+ assert(c.name == 'thisid')
37
+ assert(c.default == '5')
38
+ assert(c.column_type == 'int(11)')
39
+ assert(c.data_type == 'int')
40
+ end
41
+
42
+
43
+
44
+ def test_not_equal
45
+ c2 = DbDiff::Column.new(
46
+ 'COLUMN_NAME' => 'thatid',
47
+ 'DATA_TYPE' => 'int',
48
+ 'COLUMN_TYPE' => 'int(11)',
49
+ 'NUMERIC_PRECISION' => '10',
50
+ 'COLUMN_DEFAULT' => '5',
51
+ 'EXTRA' => 'auto_increment',
52
+ 'IS_NULLABLE' => 'NO'
53
+ )
54
+ c1 = DbDiff::Column.new(
55
+ 'COLUMN_NAME' => 'thisid',
56
+ 'DATA_TYPE' => 'int',
57
+ 'COLUMN_TYPE' => 'int(11)',
58
+ 'NUMERIC_PRECISION' => '10',
59
+ 'COLUMN_DEFAULT' => '5',
60
+ 'EXTRA' => 'auto_increment',
61
+ 'IS_NULLABLE' => 'NO'
62
+ )
63
+
64
+ assert(c1 != c2)
65
+
66
+ end
67
+
68
+ def test_equal
69
+ c2 = DbDiff::Column.new(
70
+ 'COLUMN_NAME' => 'thisid',
71
+ 'DATA_TYPE' => 'int',
72
+ 'COLUMN_TYPE' => 'int(11)',
73
+ 'NUMERIC_PRECISION' => '10',
74
+ 'COLUMN_DEFAULT' => '5',
75
+ 'EXTRA' => 'auto_increment',
76
+ 'IS_NULLABLE' => 'NO'
77
+ )
78
+
79
+ c1 = DbDiff::Column.new(
80
+ 'COLUMN_NAME' => 'thisid',
81
+ 'DATA_TYPE' => 'int',
82
+ 'COLUMN_TYPE' => 'int(11)',
83
+ 'NUMERIC_PRECISION' => '10',
84
+ 'COLUMN_DEFAULT' => '5',
85
+ 'EXTRA' => 'auto_increment',
86
+ 'IS_NULLABLE' => 'NO'
87
+ )
88
+
89
+ assert(c1 == c2)
90
+ end
91
+
92
+ end
93
+
@@ -0,0 +1,92 @@
1
+ require 'test/unit'
2
+ require 'mysql'
3
+ require 'dbdiff'
4
+
5
+ class TestDbDiff < Test::Unit::TestCase
6
+
7
+ SOURCE_DB = '__test_source'
8
+ TARGET_DB = '__test_target'
9
+
10
+ def prep_databases(dbh, source_sql, target_sql)
11
+
12
+ dbh.query("DROP DATABASE IF EXISTS `#{SOURCE_DB}`")
13
+ dbh.query("DROP DATABASE IF EXISTS `#{TARGET_DB}`")
14
+ dbh.query("CREATE DATABASE `#{SOURCE_DB}`")
15
+ dbh.query("CREATE DATABASE `#{TARGET_DB}`")
16
+
17
+ apply_sql(dbh, SOURCE_DB, source_sql)
18
+ apply_sql(dbh, TARGET_DB, target_sql)
19
+
20
+ end
21
+
22
+
23
+ def test_diffs
24
+ dir = Dir.open("./test")
25
+
26
+ dbh = Mysql.real_connect('127.0.0.1', 'root', nil)
27
+
28
+ dir.each do |f|
29
+ if File.stat("./test/" + f).directory? and f !~ /^\./
30
+ run_diff(dbh, f)
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ private
37
+
38
+ def apply_sql(dbh, database, sql)
39
+
40
+ dbh.select_db(database)
41
+ sql.split(";").each do |q|
42
+ q.strip!
43
+ next unless q.length > 0
44
+ dbh.query(q)
45
+ end
46
+ end
47
+
48
+ def process_diff
49
+
50
+ dbdiff = DbDiff.new( {:host => '127.0.0.1', :password => nil, :user => 'root', :name => SOURCE_DB},
51
+ {:host => '127.0.0.1', :password => nil, :user => 'root', :name => TARGET_DB}, :tables => ['authors'])
52
+
53
+ dbdiff.dry_run = false
54
+ dbdiff.diff
55
+
56
+ assert(dbdiff.target.deltas.size > 0)
57
+
58
+ dbdiff.apply_diffs(:drop_column, :drop_table, :drop_row)
59
+
60
+ dbdiff.diff
61
+
62
+ assert(dbdiff.target.deltas.size == 0)
63
+
64
+ dbdiff = DbDiff.new( {:host => '127.0.0.1', :password => nil, :user => 'root', :name => SOURCE_DB},
65
+ {:host => '127.0.0.1', :password => nil, :user => 'root', :name => TARGET_DB}, :tables => %w(authors))
66
+
67
+ dbdiff.diff
68
+
69
+ assert(dbdiff.target.deltas.size == 0)
70
+
71
+ end
72
+
73
+ def run_diff(dbh, dir)
74
+ source = File.open("./test/" + dir + "/source.sql").read
75
+ target = File.open("./test/" + dir + "/target.sql").read
76
+
77
+ #puts "processing #{dir}"
78
+
79
+ prep_databases(dbh, source, target)
80
+
81
+ process_diff
82
+
83
+ # puts "processing opposite #{dir}"
84
+
85
+ prep_databases(dbh, target, source)
86
+
87
+ process_diff
88
+
89
+ end
90
+
91
+
92
+ end
@@ -0,0 +1,136 @@
1
+ require 'dbdiff'
2
+ require 'test/unit'
3
+
4
+ class TestForeignKey < Test::Unit::TestCase
5
+
6
+ def test_load
7
+ fk = DbDiff::ForeignKey.new(
8
+ 'TABLE_NAME' => 'mytable',
9
+ 'CONSTRAINT_NAME' => 'ifbk_1',
10
+ 'ORDINAL_POSITION' => '1',
11
+ 'COLUMN_NAME' => 'author_id',
12
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
13
+ 'REFERENCED_TABLE_NAME' => 'authors',
14
+ 'REFERENCED_COLUMN_NAME' => 'id'
15
+ )
16
+
17
+ assert(fk.table_name == 'mytable')
18
+ assert(fk.name == 'ifbk_1')
19
+ assert(fk.ref_table == 'authors')
20
+ assert(fk.columns == ['author_id'])
21
+ assert(fk.ref_columns == ['id'])
22
+
23
+ mfk1 = DbDiff::ForeignKey.new(
24
+ 'TABLE_NAME' => 'mytable',
25
+ 'CONSTRAINT_NAME' => 'ifbk_1',
26
+ 'ORDINAL_POSITION' => '1',
27
+ 'COLUMN_NAME' => 'author_id',
28
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
29
+ 'REFERENCED_TABLE_NAME' => 'authors',
30
+ 'REFERENCED_COLUMN_NAME' => 'id'
31
+ )
32
+
33
+ mfk2 = DbDiff::ForeignKey.new(
34
+ 'TABLE_NAME' => 'mytable',
35
+ 'CONSTRAINT_NAME' => 'ifbk_1',
36
+ 'ORDINAL_POSITION' => '2',
37
+ 'COLUMN_NAME' => 'user_id',
38
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '2',
39
+ 'REFERENCED_TABLE_NAME' => 'authors',
40
+ 'REFERENCED_COLUMN_NAME' => 'user_id'
41
+ )
42
+
43
+ mfk1.merge(mfk2)
44
+
45
+ assert(mfk1.table_name == 'mytable')
46
+ assert(mfk1.name == 'ifbk_1')
47
+ assert(mfk1.ref_table == 'authors')
48
+ assert(mfk1.columns == ['author_id', 'user_id'])
49
+ assert(mfk1.ref_columns == ['id', 'user_id'])
50
+
51
+ end
52
+
53
+
54
+ def test_bad_merge
55
+
56
+ k1 = DbDiff::ForeignKey.new(
57
+ 'TABLE_NAME' => 'mytable',
58
+ 'CONSTRAINT_NAME' => 'ifbk_2',
59
+ 'ORDINAL_POSITION' => '1',
60
+ 'COLUMN_NAME' => 'author_id',
61
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
62
+ 'REFERENCED_TABLE_NAME' => 'authors',
63
+ 'REFERENCED_COLUMN_NAME' => 'id'
64
+ )
65
+
66
+ k2 = DbDiff::ForeignKey.new(
67
+ 'TABLE_NAME' => 'mytable',
68
+ 'CONSTRAINT_NAME' => 'ifbk_1',
69
+ 'ORDINAL_POSITION' => '2',
70
+ 'COLUMN_NAME' => 'user_id',
71
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '2',
72
+ 'REFERENCED_TABLE_NAME' => 'authors',
73
+ 'REFERENCED_COLUMN_NAME' => 'user_id'
74
+ )
75
+
76
+ caught_error = false
77
+
78
+ begin
79
+ k1.merge(k2)
80
+ rescue => e
81
+ caught_error = true
82
+ end
83
+ assert(caught_error)
84
+
85
+ end
86
+
87
+ def test_not_equal
88
+
89
+ mfk1 = DbDiff::ForeignKey.new(
90
+ 'TABLE_NAME' => 'mytable',
91
+ 'CONSTRAINT_NAME' => 'ifbk_2',
92
+ 'ORDINAL_POSITION' => '1',
93
+ 'COLUMN_NAME' => 'author_id',
94
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
95
+ 'REFERENCED_TABLE_NAME' => 'authors',
96
+ 'REFERENCED_COLUMN_NAME' => 'id'
97
+ )
98
+
99
+ mfk2 = DbDiff::ForeignKey.new(
100
+ 'TABLE_NAME' => 'mytable',
101
+ 'CONSTRAINT_NAME' => 'ifbk_1',
102
+ 'ORDINAL_POSITION' => '2',
103
+ 'COLUMN_NAME' => 'user_id',
104
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '2',
105
+ 'REFERENCED_TABLE_NAME' => 'authors',
106
+ 'REFERENCED_COLUMN_NAME' => 'user_id'
107
+ )
108
+ assert(mfk1 != mfk2)
109
+ end
110
+
111
+ def test_equal
112
+
113
+ fk1 = DbDiff::ForeignKey.new(
114
+ 'TABLE_NAME' => 'mytable',
115
+ 'CONSTRAINT_NAME' => 'ifbk_1',
116
+ 'ORDINAL_POSITION' => '1',
117
+ 'COLUMN_NAME' => 'author_id',
118
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
119
+ 'REFERENCED_TABLE_NAME' => 'authors',
120
+ 'REFERENCED_COLUMN_NAME' => 'id'
121
+ )
122
+
123
+ fk2 = DbDiff::ForeignKey.new(
124
+ 'TABLE_NAME' => 'mytable',
125
+ 'CONSTRAINT_NAME' => 'ifbk_1',
126
+ 'ORDINAL_POSITION' => '1',
127
+ 'COLUMN_NAME' => 'author_id',
128
+ 'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
129
+ 'REFERENCED_TABLE_NAME' => 'authors',
130
+ 'REFERENCED_COLUMN_NAME' => 'id'
131
+ )
132
+ assert(fk1 == fk2)
133
+ end
134
+
135
+ end
136
+
data/test/test_key.rb ADDED
@@ -0,0 +1,116 @@
1
+ require 'dbdiff'
2
+ require 'test/unit'
3
+
4
+ class TestKey < Test::Unit::TestCase
5
+
6
+ def test_load
7
+ pk = DbDiff::Key.new(
8
+ 'mytable',
9
+ 'Key_name' => 'PRIMARY',
10
+ 'Seq_in_index' => '1',
11
+ 'Column_name' => 'id',
12
+ 'Non_unique' => '0'
13
+ )
14
+
15
+ assert(pk.table_name == 'mytable')
16
+ assert(pk.primary)
17
+ assert(pk.unique)
18
+ assert(pk.name == 'PRIMARY')
19
+ assert(pk.columns == ['id'])
20
+
21
+ mk1 = DbDiff::Key.new(
22
+ 'mytable',
23
+ 'Key_name' => 'user_key',
24
+ 'Seq_in_index' => '1',
25
+ 'Column_name' => 'user_id',
26
+ 'Non_unique' => '1'
27
+ )
28
+
29
+ mk2 = DbDiff::Key.new(
30
+ 'mytable',
31
+ 'Key_name' => 'user_key',
32
+ 'Seq_in_index' => '2',
33
+ 'Column_name' => 'key',
34
+ 'Non_unique' => '1'
35
+ )
36
+
37
+ mk1.merge(mk2)
38
+
39
+ assert(!mk1.primary )
40
+ assert(!mk1.unique)
41
+ assert(mk1.name == 'user_key')
42
+ assert(mk1.columns == ['user_id', 'key'])
43
+ end
44
+
45
+
46
+
47
+ def test_not_equal
48
+ k1 = DbDiff::Key.new(
49
+ 'mytable',
50
+ 'Key_name' => 'user_key',
51
+ 'Seq_in_index' => '1',
52
+ 'Column_name' => 'user_id',
53
+ 'Non_unique' => '1'
54
+ )
55
+
56
+ k2 = DbDiff::Key.new(
57
+ 'mytable',
58
+ 'Key_name' => 'user_keyAA',
59
+ 'Seq_in_index' => '1',
60
+ 'Column_name' => 'user_id',
61
+ 'Non_unique' => '1'
62
+ )
63
+ require 'pp'
64
+
65
+ assert(k1 != k2)
66
+
67
+ end
68
+
69
+ def test_bad_merge
70
+ k1 = DbDiff::Key.new(
71
+ 'mytable',
72
+ 'Key_name' => 'user_key',
73
+ 'Seq_in_index' => '1',
74
+ 'Column_name' => 'user_id',
75
+ 'Non_unique' => '1'
76
+ )
77
+
78
+ k2 = DbDiff::Key.new(
79
+ 'mytable',
80
+ 'Key_name' => 'key_x',
81
+ 'Seq_in_index' => '1',
82
+ 'Column_name' => 'user_id',
83
+ 'Non_unique' => '1'
84
+ )
85
+
86
+ caught_error = false
87
+ begin
88
+ k1.merge(k2)
89
+ rescue => e
90
+ caught_error = true
91
+ end
92
+ assert(caught_error)
93
+ end
94
+
95
+ def test_equal
96
+ k1 = DbDiff::Key.new(
97
+ 'mytable',
98
+ 'Key_name' => 'user_key',
99
+ 'Seq_in_index' => '1',
100
+ 'Column_name' => 'user_id',
101
+ 'Non_unique' => '1'
102
+ )
103
+
104
+ k2 = DbDiff::Key.new(
105
+ 'mytable',
106
+ 'Key_name' => 'user_key',
107
+ 'Seq_in_index' => '1',
108
+ 'Column_name' => 'user_id',
109
+ 'Non_unique' => '1'
110
+ )
111
+
112
+ assert(k1 == k2)
113
+ end
114
+
115
+ end
116
+