convergence 1.0.6 → 1.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 (69) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +35 -4
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +46 -0
  5. data/CONTRIBUTING.md +80 -0
  6. data/Gemfile.lock +23 -8
  7. data/README.md +131 -1
  8. data/Rakefile +45 -2
  9. data/convergence.gemspec +3 -1
  10. data/lib/convergence/cli.rb +10 -2
  11. data/lib/convergence/command/apply.rb +38 -9
  12. data/lib/convergence/command/dryrun.rb +5 -5
  13. data/lib/convergence/command/export.rb +19 -1
  14. data/lib/convergence/command.rb +6 -0
  15. data/lib/convergence/database_connector/postgres_connector.rb +25 -0
  16. data/lib/convergence/database_connector/sqlite_connector.rb +22 -0
  17. data/lib/convergence/database_connector.rb +6 -0
  18. data/lib/convergence/default_parameter/mysql_default_parameter.rb +1 -1
  19. data/lib/convergence/default_parameter/postgres_default_parameter.rb +57 -0
  20. data/lib/convergence/default_parameter/sqlite_default_parameter.rb +57 -0
  21. data/lib/convergence/default_parameter.rb +6 -0
  22. data/lib/convergence/diff.rb +9 -2
  23. data/lib/convergence/dsl.rb +18 -3
  24. data/lib/convergence/dumper/mysql_schema_dumper.rb +37 -9
  25. data/lib/convergence/dumper/postgres_schema_dumper.rb +193 -0
  26. data/lib/convergence/dumper/sqlite_schema_dumper.rb +132 -0
  27. data/lib/convergence/dumper.rb +113 -0
  28. data/lib/convergence/sql_generator/mysql_generator.rb +18 -3
  29. data/lib/convergence/sql_generator/postgres_generator.rb +256 -0
  30. data/lib/convergence/sql_generator/sqlite_generator.rb +178 -0
  31. data/lib/convergence/version.rb +1 -1
  32. data/spec/config/spec_database.yml +12 -0
  33. data/spec/convergence/config_spec.rb +75 -0
  34. data/spec/convergence/diff_spec.rb +125 -45
  35. data/spec/convergence/dsl_spec.rb +26 -0
  36. data/spec/convergence/dumper/mysql_schema_dumper_spec.rb +25 -2
  37. data/spec/convergence/dumper/postgres_schema_dumper_spec.rb +103 -0
  38. data/spec/convergence/dumper/sqlite_schema_dumper_spec.rb +85 -0
  39. data/spec/convergence/dumper_spec.rb +110 -16
  40. data/spec/convergence/foreign_key_spec.rb +33 -0
  41. data/spec/convergence/index_spec.rb +52 -0
  42. data/spec/convergence/pretty_diff_spec.rb +85 -0
  43. data/spec/convergence/table_spec.rb +23 -0
  44. data/spec/fixtures/add_table_with_enum_set.schema +5 -0
  45. data/spec/fixtures/change_table_comment_to_paper.schema +2 -0
  46. data/spec/fixtures/execute_raw_sql.schema +28 -0
  47. data/spec/fixtures/postgres/add_columns_to_paper.schema +29 -0
  48. data/spec/fixtures/postgres/add_table.schema +32 -0
  49. data/spec/fixtures/postgres/change_comment_columns_to_paper.schema +28 -0
  50. data/spec/fixtures/postgres/change_table_comment_to_paper.schema +28 -0
  51. data/spec/fixtures/postgres/drop_foreign_key.schema +25 -0
  52. data/spec/fixtures/postgres/drop_table.schema +19 -0
  53. data/spec/fixtures/postgres/remove_columns_to_paper.schema +27 -0
  54. data/spec/fixtures/postgres_test_db.sql +41 -0
  55. data/spec/fixtures/sqlite/add_columns_to_paper.schema +29 -0
  56. data/spec/fixtures/sqlite/add_table.schema +32 -0
  57. data/spec/fixtures/sqlite/change_columns_to_paper.schema +28 -0
  58. data/spec/fixtures/sqlite/drop_foreign_key.schema +25 -0
  59. data/spec/fixtures/sqlite/drop_table.schema +19 -0
  60. data/spec/fixtures/sqlite/remove_columns_to_paper.schema +27 -0
  61. data/spec/fixtures/sqlite_test_db.sql +31 -0
  62. data/spec/fixtures/test_db.sql +10 -0
  63. data/spec/integrations/command_diff.rb +35 -0
  64. data/spec/integrations/command_dryrun.rb +37 -2
  65. data/spec/integrations/command_export.rb +28 -0
  66. data/spec/postgres_integrations/command_dryrun.rb +79 -0
  67. data/spec/spec_helper.rb +13 -0
  68. data/spec/sqlite_integrations/command_dryrun.rb +70 -0
  69. metadata +96 -8
@@ -0,0 +1,28 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true, comment: "Created At"
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers", comment: "Paper" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", comment: "Slug"
14
+ t.varchar "title1", limit: 300, comment: "Title 1"
15
+ t.varchar "title2", limit: 300, comment: "Title 2"
16
+ t.text "description", null: true, comment: "Description"
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors", comment: "Paper Author Relation" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id", comment: "Paper id"
24
+ t.int "author_id", comment: "Paper author id"
25
+
26
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
27
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
28
+ end
@@ -0,0 +1,28 @@
1
+ create_table "authors", comment: "Author Table" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers", comment: "Paper" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", comment: "Slug"
14
+ t.varchar "title1", limit: 300, comment: "Title 1"
15
+ t.varchar "title2", limit: 300, comment: "Title 2"
16
+ t.text "description", null: true, comment: "Description"
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors", comment: "Paper Author Relation" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id", comment: "Paper id"
24
+ t.int "author_id", comment: "Paper author id"
25
+
26
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
27
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
28
+ end
@@ -0,0 +1,25 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers", comment: "Paper" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", comment: "Slug"
14
+ t.varchar "title1", limit: 300, comment: "Title 1"
15
+ t.varchar "title2", limit: 300, comment: "Title 2"
16
+ t.text "description", null: true, comment: "Description"
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors", comment: "Paper Author Relation" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id", comment: "Paper id"
24
+ t.int "author_id", comment: "Paper author id"
25
+ end
@@ -0,0 +1,19 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers", comment: "Paper" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", comment: "Slug"
14
+ t.varchar "title1", limit: 300, comment: "Title 1"
15
+ t.varchar "title2", limit: 300, comment: "Title 2"
16
+ t.text "description", null: true, comment: "Description"
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
@@ -0,0 +1,27 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers", comment: "Paper" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", comment: "Slug"
14
+ t.varchar "title1", limit: 300, comment: "Title 1"
15
+ t.varchar "title2", limit: 300, comment: "Title 2"
16
+
17
+ t.index "slug", name: "index_papers_on_slug", unique: true
18
+ end
19
+
20
+ create_table "paper_authors", comment: "Paper Author Relation" do |t|
21
+ t.int "id", primary_key: true, extra: "auto_increment"
22
+ t.int "paper_id", comment: "Paper id"
23
+ t.int "author_id", comment: "Paper author id"
24
+
25
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
26
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
27
+ end
@@ -0,0 +1,41 @@
1
+ DROP TABLE IF EXISTS paper_authors;
2
+ DROP TABLE IF EXISTS papers;
3
+ DROP TABLE IF EXISTS authors;
4
+
5
+ CREATE TABLE papers (
6
+ id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
7
+ slug varchar(255) NOT NULL,
8
+ title1 varchar(300) NOT NULL,
9
+ title2 varchar(300) NOT NULL,
10
+ description text,
11
+ edition_number integer NOT NULL DEFAULT 0,
12
+ published_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
13
+ );
14
+ CREATE UNIQUE INDEX index_papers_on_slug ON papers (slug);
15
+ COMMENT ON TABLE papers IS 'Paper';
16
+ COMMENT ON COLUMN papers.slug IS 'Slug';
17
+ COMMENT ON COLUMN papers.title1 IS 'Title 1';
18
+ COMMENT ON COLUMN papers.title2 IS 'Title 2';
19
+ COMMENT ON COLUMN papers.description IS 'Description';
20
+ COMMENT ON COLUMN papers.edition_number IS 'Edition number';
21
+ COMMENT ON COLUMN papers.published_at IS 'Published at';
22
+
23
+ CREATE TABLE authors (
24
+ id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
25
+ name varchar(110) NOT NULL,
26
+ age integer NOT NULL,
27
+ created_at timestamp,
28
+ updated_at timestamp
29
+ );
30
+ CREATE INDEX index_authors_on_created_at ON authors (created_at);
31
+
32
+ CREATE TABLE paper_authors (
33
+ id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
34
+ paper_id integer NOT NULL,
35
+ author_id integer NOT NULL,
36
+ CONSTRAINT paper_authors_author_id_fk FOREIGN KEY (author_id) REFERENCES authors (id),
37
+ CONSTRAINT paper_authors_paper_id_fk FOREIGN KEY (paper_id) REFERENCES papers (id)
38
+ );
39
+ COMMENT ON TABLE paper_authors IS 'Paper Author Relation';
40
+ COMMENT ON COLUMN paper_authors.paper_id IS 'Paper id';
41
+ COMMENT ON COLUMN paper_authors.author_id IS 'Paper author id';
@@ -0,0 +1,29 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+ t.text "description", null: true
17
+ t.varchar "subtitle", null: true
18
+
19
+ t.index "slug", name: "index_papers_on_slug", unique: true
20
+ end
21
+
22
+ create_table "paper_authors" do |t|
23
+ t.int "id", primary_key: true, extra: "auto_increment"
24
+ t.int "paper_id"
25
+ t.int "author_id"
26
+
27
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
28
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
29
+ end
@@ -0,0 +1,32 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+ t.text "description", null: true
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id"
24
+ t.int "author_id"
25
+
26
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
27
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
28
+ end
29
+
30
+ create_table "dummies" do |t|
31
+ t.int "id", primary_key: true, extra: "auto_increment"
32
+ end
@@ -0,0 +1,28 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 200
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+ t.text "description", null: true
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id"
24
+ t.int "author_id"
25
+
26
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
27
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
28
+ end
@@ -0,0 +1,25 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+ t.text "description", null: true
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
20
+
21
+ create_table "paper_authors" do |t|
22
+ t.int "id", primary_key: true, extra: "auto_increment"
23
+ t.int "paper_id"
24
+ t.int "author_id"
25
+ end
@@ -0,0 +1,19 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+ t.text "description", null: true
17
+
18
+ t.index "slug", name: "index_papers_on_slug", unique: true
19
+ end
@@ -0,0 +1,27 @@
1
+ create_table "authors" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age"
5
+ t.datetime "created_at", null: true
6
+ t.datetime "updated_at", null: true
7
+
8
+ t.index "created_at", name: "index_authors_on_created_at"
9
+ end
10
+
11
+ create_table "papers" do |t|
12
+ t.int "id", primary_key: true, extra: "auto_increment"
13
+ t.varchar "slug", limit: 255
14
+ t.varchar "title1", limit: 300
15
+ t.varchar "title2", limit: 300
16
+
17
+ t.index "slug", name: "index_papers_on_slug", unique: true
18
+ end
19
+
20
+ create_table "paper_authors" do |t|
21
+ t.int "id", primary_key: true, extra: "auto_increment"
22
+ t.int "paper_id"
23
+ t.int "author_id"
24
+
25
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
26
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
27
+ end
@@ -0,0 +1,31 @@
1
+ DROP TABLE IF EXISTS paper_authors;
2
+ DROP TABLE IF EXISTS papers;
3
+ DROP TABLE IF EXISTS authors;
4
+
5
+ CREATE TABLE authors (
6
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
7
+ name varchar(110) NOT NULL,
8
+ age int NOT NULL,
9
+ created_at datetime,
10
+ updated_at datetime
11
+ );
12
+ CREATE INDEX index_authors_on_created_at ON authors (created_at);
13
+
14
+ CREATE TABLE papers (
15
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
16
+ slug varchar(255) NOT NULL,
17
+ title1 varchar(300) NOT NULL,
18
+ title2 varchar(300) NOT NULL,
19
+ description text,
20
+ edition_number int NOT NULL DEFAULT 0,
21
+ published_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
22
+ );
23
+ CREATE UNIQUE INDEX index_papers_on_slug ON papers (slug);
24
+
25
+ CREATE TABLE paper_authors (
26
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
27
+ paper_id int NOT NULL,
28
+ author_id int NOT NULL,
29
+ CONSTRAINT paper_authors_author_id_fk FOREIGN KEY (author_id) REFERENCES authors (id),
30
+ CONSTRAINT paper_authors_paper_id_fk FOREIGN KEY (paper_id) REFERENCES papers (id)
31
+ );
@@ -1,6 +1,7 @@
1
1
  DROP TABLE IF EXISTS `paper_authors`;
2
2
  DROP TABLE IF EXISTS `papers`;
3
3
  DROP TABLE IF EXISTS `authors`;
4
+ DROP TABLE IF EXISTS `enum_set_samples`;
4
5
 
5
6
  CREATE TABLE `papers` (
6
7
  `id` int(11) NOT NULL AUTO_INCREMENT,
@@ -8,6 +9,8 @@ CREATE TABLE `papers` (
8
9
  `title1` varchar(300) NOT NULL COMMENT 'Title 1',
9
10
  `title2` varchar(300) NOT NULL COMMENT 'Title 2',
10
11
  `description` text COMMENT 'Description',
12
+ `edition_number` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Edition number',
13
+ `published_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Published at',
11
14
  PRIMARY KEY (`id`),
12
15
  UNIQUE KEY `index_papers_on_slug` (`slug`),
13
16
  KEY `index_papers_on_title1_title2` (`title1` (100), `title2` (200))
@@ -33,3 +36,10 @@ CREATE TABLE `paper_authors` (
33
36
  CONSTRAINT `paper_authors_author_id_fk` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`),
34
37
  CONSTRAINT `paper_authors_paper_id_fk` FOREIGN KEY (`paper_id`) REFERENCES `papers` (`id`)
35
38
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Paper Author Relation';
39
+
40
+ CREATE TABLE `enum_set_samples` (
41
+ `id` int(11) NOT NULL AUTO_INCREMENT,
42
+ `status` enum('active','inactive','pending') NOT NULL DEFAULT 'active',
43
+ `flags` set('a','b','it''s tricky') NOT NULL,
44
+ PRIMARY KEY (`id`)
45
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'convergence/command/diff'
3
+
4
+ describe 'Command::Diff#execute' do
5
+ def execute(file1, file2)
6
+ opts = {
7
+ diff: [
8
+ File.expand_path("#{File.dirname(__FILE__)}/../fixtures/#{file1}"),
9
+ File.expand_path("#{File.dirname(__FILE__)}/../fixtures/#{file2}")
10
+ ]
11
+ }
12
+ Convergence::Command::Diff.new(opts, config: nil).execute
13
+ end
14
+
15
+ it 'should show tables only present in the second file as added' do
16
+ result = execute('drop_table.schema', 'add_table.schema')
17
+ expect(result).to be_include('+ create_table :paper_authors, collate: "utf8_general_ci", comment: "Paper Author Relation" do |t|')
18
+ end
19
+
20
+ it 'should show tables only present in the first file as removed' do
21
+ result = execute('add_table.schema', 'drop_table.schema')
22
+ expect(result).to be_include('- create_table :paper_authors, collate: "utf8_general_ci", comment: "Paper Author Relation" do |t|')
23
+ end
24
+
25
+ it 'should show a column change between the two files as a unified diff' do
26
+ result = execute('add_table.schema', 'change_comment_columns_to_paper.schema')
27
+ expect(result).to be_include("- t.datetime :created_at, null: true\n")
28
+ expect(result).to be_include("+ t.datetime :created_at, null: true, comment: \"Created At\"\n")
29
+ end
30
+
31
+ it 'should be empty when comparing a schema file with itself' do
32
+ result = execute('add_table.schema', 'add_table.schema')
33
+ expect(result).to eq('')
34
+ end
35
+ end
@@ -2,10 +2,10 @@ require 'spec_helper'
2
2
  require 'convergence/command/dryrun'
3
3
 
4
4
  describe 'Command::Dryrun#execute' do
5
- def execute(dsl_path)
5
+ def execute(dsl_path, extra_options = {})
6
6
  parse_option = {
7
7
  input: File.expand_path("#{File.dirname(__FILE__)}/../fixtures/#{dsl_path}")
8
- }
8
+ }.merge(extra_options)
9
9
  Convergence::Command::Dryrun.new(parse_option, config: mysql_settings).execute
10
10
  end
11
11
 
@@ -35,6 +35,25 @@ describe 'Command::Dryrun#execute' do
35
35
  end
36
36
  end
37
37
 
38
+ describe 'add table with enum/set columns' do
39
+ let(:exec_dsl) { 'add_table_with_enum_set.schema' }
40
+ let(:expect_query) do
41
+ q = <<-QUERY
42
+ # CREATE TABLE `enum_set_dummies` (
43
+ # `id` int(11) NOT NULL AUTO_INCREMENT,
44
+ # `status` enum('active','inactive','pending') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'active',
45
+ # `flags` set('a','b','c') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
46
+ # PRIMARY KEY (`id`)
47
+ # ) ENGINE=InnoDB ROW_FORMAT=Compact DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
48
+ QUERY
49
+ q.strip
50
+ end
51
+ it 'should be output create table query with quoted enum/set values' do
52
+ result = execute(exec_dsl)
53
+ expect(result).to be_include(expect_query)
54
+ end
55
+ end
56
+
38
57
  describe 'drop table' do
39
58
  let(:exec_dsl) { 'drop_table.schema' }
40
59
 
@@ -42,6 +61,22 @@ describe 'Command::Dryrun#execute' do
42
61
  result = execute(exec_dsl)
43
62
  expect(result).to be_include('DROP TABLE `paper_authors`')
44
63
  end
64
+
65
+ context 'when safe_migration is enabled' do
66
+ it 'should not output drop table query' do
67
+ result = execute(exec_dsl, safe_migration: true)
68
+ expect(result).not_to be_include('DROP TABLE `paper_authors`')
69
+ end
70
+ end
71
+ end
72
+
73
+ describe 'execute raw sql' do
74
+ let(:exec_dsl) { 'execute_raw_sql.schema' }
75
+
76
+ it 'should be output the raw sql statement after the generated schema changes' do
77
+ result = execute(exec_dsl)
78
+ expect(result).to be_include('# UPDATE authors SET age = 0 WHERE age IS NULL;')
79
+ end
45
80
  end
46
81
 
47
82
  describe 'add columns' do
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'convergence/command/export'
3
+
4
+ describe 'Command::Export#execute' do
5
+ def execute(extra_options = {})
6
+ Convergence::Command::Export.new(extra_options, config: mysql_settings).execute
7
+ end
8
+
9
+ it 'should be output convergence DSL by default' do
10
+ result = execute
11
+ expect(result).to be_include('create_table :authors')
12
+ end
13
+
14
+ describe '--dump-rails-migration' do
15
+ it 'should be output an ActiveRecord migration' do
16
+ result = execute(dump_rails_migration: true, filename: 'add_initial_tables')
17
+ expect(result).to be_include('# Filename:')
18
+ expect(result).to be_include('_add_initial_tables.rb')
19
+ expect(result).to be_include('class AddInitialTables < ActiveRecord::Migration[')
20
+ expect(result).to be_include('create_table :authors do |t|')
21
+ end
22
+
23
+ it 'should use a default filename/class name when --filename is omitted' do
24
+ result = execute(dump_rails_migration: true)
25
+ expect(result).to be_include('class ConvergenceMigration < ActiveRecord::Migration[')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+ require 'convergence/command/dryrun'
3
+
4
+ describe 'PostgreSQL Command::Dryrun#execute' do
5
+ def execute(dsl_path, extra_options = {})
6
+ parse_option = {
7
+ input: File.expand_path("#{File.dirname(__FILE__)}/../fixtures/postgres/#{dsl_path}")
8
+ }.merge(extra_options)
9
+ Convergence::Command::Dryrun.new(parse_option, config: postgres_settings).execute
10
+ end
11
+
12
+ describe 'change table options' do
13
+ let(:exec_dsl) { 'change_table_comment_to_paper.schema' }
14
+
15
+ it 'should be output alter table query' do
16
+ result = execute(exec_dsl)
17
+ expect(result).to be_include(%(# COMMENT ON TABLE "authors" IS 'Author Table';))
18
+ end
19
+ end
20
+
21
+ describe 'add table' do
22
+ let(:exec_dsl) { 'add_table.schema' }
23
+
24
+ it 'should be output create table query' do
25
+ result = execute(exec_dsl)
26
+ expect(result).to be_include(%(# "id" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,))
27
+ expect(result).to be_include(%(# COMMENT ON TABLE "dummies" IS 'Dummy Table';))
28
+ expect(result).to be_include(%(# COMMENT ON COLUMN "dummies"."id" IS 'Hello Convergence';))
29
+ end
30
+ end
31
+
32
+ describe 'drop table' do
33
+ let(:exec_dsl) { 'drop_table.schema' }
34
+
35
+ it 'should be output drop table query' do
36
+ result = execute(exec_dsl)
37
+ expect(result).to be_include('DROP TABLE "paper_authors";')
38
+ end
39
+ end
40
+
41
+ describe 'add columns' do
42
+ let(:exec_dsl) { 'add_columns_to_paper.schema' }
43
+
44
+ it 'should be output alter add column query' do
45
+ result = execute(exec_dsl)
46
+ expect(result).to be_include(%(# ALTER TABLE "papers" ADD COLUMN "subtitle" varchar DEFAULT NULL;))
47
+ end
48
+ end
49
+
50
+ describe 'remove columns' do
51
+ let(:exec_dsl) { 'remove_columns_to_paper.schema' }
52
+
53
+ it 'should be output alter drop column query' do
54
+ result = execute(exec_dsl)
55
+ expect(result).to be_include(%(# ALTER TABLE "papers" DROP COLUMN "description";))
56
+ end
57
+ end
58
+
59
+ describe 'change columns' do
60
+ describe 'change comment' do
61
+ let(:exec_dsl) { 'change_comment_columns_to_paper.schema' }
62
+
63
+ it 'should be output alter table column query' do
64
+ result = execute(exec_dsl)
65
+ expect(result).to be_include(%(# COMMENT ON COLUMN "authors"."created_at" IS 'Created At';))
66
+ end
67
+ end
68
+ end
69
+
70
+ describe 'drop foreign key' do
71
+ let(:exec_dsl) { 'drop_foreign_key.schema' }
72
+
73
+ it 'should be output drop foreign key query' do
74
+ result = execute(exec_dsl)
75
+ expect(result).to be_include(%(# ALTER TABLE "paper_authors" DROP CONSTRAINT "paper_authors_author_id_fk";))
76
+ expect(result).to be_include(%(# ALTER TABLE "paper_authors" DROP CONSTRAINT "paper_authors_paper_id_fk";))
77
+ end
78
+ end
79
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,8 @@ require 'pry'
5
5
  require 'convergence'
6
6
  require 'convergence/database_connector'
7
7
  Dir["#{File.dirname(__FILE__)}/integrations/**/*.rb"].each { |f| require f }
8
+ Dir["#{File.dirname(__FILE__)}/postgres_integrations/**/*.rb"].each { |f| require f }
9
+ Dir["#{File.dirname(__FILE__)}/sqlite_integrations/**/*.rb"].each { |f| require f }
8
10
 
9
11
  $default_output = File.open('/dev/null', 'w')
10
12
 
@@ -13,6 +15,17 @@ def mysql_settings
13
15
  Convergence::Config.new(Hash[mysql_settings.map { |k, v| [k.to_sym, v] }])
14
16
  end
15
17
 
18
+ def postgres_settings
19
+ postgres_settings = YAML.load_file("#{File.dirname(__FILE__)}/config/spec_database.yml")['postgresql']
20
+ Convergence::Config.new(Hash[postgres_settings.map { |k, v| [k.to_sym, v] }])
21
+ end
22
+
23
+ def sqlite_settings
24
+ sqlite_settings = YAML.load_file("#{File.dirname(__FILE__)}/config/spec_database.yml")['sqlite3']
25
+ database_path = File.expand_path("#{File.dirname(__FILE__)}/fixtures/#{sqlite_settings['database']}")
26
+ Convergence::Config.new(adapter: sqlite_settings['adapter'], database: database_path)
27
+ end
28
+
16
29
  def rollback
17
30
  # FIXME But I have no idea to rollback create/drop/alter table of mysql
18
31
  sqls = File.open("#{File.dirname(__FILE__)}/fixtures/test_db.sql")