convergence 1.0.6 → 1.2.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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +35 -4
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +57 -0
  5. data/CONTRIBUTING.md +80 -0
  6. data/Gemfile.lock +25 -9
  7. data/README.md +160 -1
  8. data/Rakefile +45 -2
  9. data/convergence.gemspec +5 -1
  10. data/lib/convergence/cli.rb +10 -2
  11. data/lib/convergence/column.rb +3 -2
  12. data/lib/convergence/command/apply.rb +38 -9
  13. data/lib/convergence/command/dryrun.rb +5 -5
  14. data/lib/convergence/command/export.rb +19 -1
  15. data/lib/convergence/command.rb +6 -0
  16. data/lib/convergence/database_connector/postgres_connector.rb +25 -0
  17. data/lib/convergence/database_connector/sqlite_connector.rb +22 -0
  18. data/lib/convergence/database_connector.rb +6 -0
  19. data/lib/convergence/default_parameter/mysql_default_parameter.rb +1 -1
  20. data/lib/convergence/default_parameter/postgres_default_parameter.rb +57 -0
  21. data/lib/convergence/default_parameter/sqlite_default_parameter.rb +57 -0
  22. data/lib/convergence/default_parameter.rb +6 -0
  23. data/lib/convergence/diff.rb +46 -3
  24. data/lib/convergence/dsl.rb +18 -3
  25. data/lib/convergence/dumper/mysql_schema_dumper.rb +37 -9
  26. data/lib/convergence/dumper/postgres_schema_dumper.rb +193 -0
  27. data/lib/convergence/dumper/sqlite_schema_dumper.rb +132 -0
  28. data/lib/convergence/dumper.rb +113 -0
  29. data/lib/convergence/sql_generator/mysql_generator.rb +32 -3
  30. data/lib/convergence/sql_generator/postgres_generator.rb +270 -0
  31. data/lib/convergence/sql_generator/sqlite_generator.rb +192 -0
  32. data/lib/convergence/table.rb +3 -2
  33. data/lib/convergence/version.rb +1 -1
  34. data/spec/config/spec_database.yml +12 -0
  35. data/spec/convergence/config_spec.rb +75 -0
  36. data/spec/convergence/diff_spec.rb +291 -44
  37. data/spec/convergence/dsl_spec.rb +26 -0
  38. data/spec/convergence/dumper/mysql_schema_dumper_spec.rb +25 -2
  39. data/spec/convergence/dumper/postgres_schema_dumper_spec.rb +103 -0
  40. data/spec/convergence/dumper/sqlite_schema_dumper_spec.rb +85 -0
  41. data/spec/convergence/dumper_spec.rb +110 -16
  42. data/spec/convergence/foreign_key_spec.rb +33 -0
  43. data/spec/convergence/index_spec.rb +52 -0
  44. data/spec/convergence/pretty_diff_spec.rb +85 -0
  45. data/spec/convergence/table_spec.rb +46 -0
  46. data/spec/fixtures/add_table_with_enum_set.schema +5 -0
  47. data/spec/fixtures/change_table_comment_to_paper.schema +2 -0
  48. data/spec/fixtures/execute_raw_sql.schema +28 -0
  49. data/spec/fixtures/postgres/add_columns_to_paper.schema +29 -0
  50. data/spec/fixtures/postgres/add_table.schema +32 -0
  51. data/spec/fixtures/postgres/change_comment_columns_to_paper.schema +28 -0
  52. data/spec/fixtures/postgres/change_table_comment_to_paper.schema +28 -0
  53. data/spec/fixtures/postgres/drop_foreign_key.schema +25 -0
  54. data/spec/fixtures/postgres/drop_table.schema +19 -0
  55. data/spec/fixtures/postgres/remove_columns_to_paper.schema +27 -0
  56. data/spec/fixtures/postgres/rename_column_on_paper.schema +28 -0
  57. data/spec/fixtures/postgres/rename_table.schema +28 -0
  58. data/spec/fixtures/postgres_test_db.sql +41 -0
  59. data/spec/fixtures/rename_column_to_author.schema +26 -0
  60. data/spec/fixtures/rename_table.schema +26 -0
  61. data/spec/fixtures/sqlite/add_columns_to_paper.schema +29 -0
  62. data/spec/fixtures/sqlite/add_table.schema +32 -0
  63. data/spec/fixtures/sqlite/change_columns_to_paper.schema +28 -0
  64. data/spec/fixtures/sqlite/drop_foreign_key.schema +25 -0
  65. data/spec/fixtures/sqlite/drop_table.schema +19 -0
  66. data/spec/fixtures/sqlite/remove_columns_to_paper.schema +27 -0
  67. data/spec/fixtures/sqlite/rename_column_on_paper.schema +28 -0
  68. data/spec/fixtures/sqlite/rename_table.schema +27 -0
  69. data/spec/fixtures/sqlite_test_db.sql +31 -0
  70. data/spec/fixtures/test_db.sql +10 -0
  71. data/spec/integrations/command_diff.rb +35 -0
  72. data/spec/integrations/command_dryrun.rb +59 -2
  73. data/spec/integrations/command_export.rb +28 -0
  74. data/spec/postgres_integrations/command_dryrun.rb +101 -0
  75. data/spec/spec_helper.rb +13 -0
  76. data/spec/sqlite_integrations/command_dryrun.rb +92 -0
  77. metadata +135 -7
@@ -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,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
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 "notes", null: true, comment: "Description", renamed_from: "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" 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_author_links", comment: "Paper Author Relation", renamed_from: "paper_authors" 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,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,26 @@
1
+ create_table "authors", collate: "utf8_general_ci", comment: "" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "full_name", limit: 110, renamed_from: "name"
4
+ t.int "age", unsigned: true
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", collate: "utf8_general_ci", 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
+ end
18
+
19
+ create_table "paper_authors", collate: "utf8_general_ci", comment: "Paper Author Relation" do |t|
20
+ t.int "id", primary_key: true, extra: "auto_increment"
21
+ t.int "paper_id", comment: "Paper id"
22
+ t.int "author_id", comment: "Paper author id"
23
+
24
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
25
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
26
+ end
@@ -0,0 +1,26 @@
1
+ create_table "authors", collate: "utf8_general_ci", comment: "" do |t|
2
+ t.int "id", primary_key: true, extra: "auto_increment"
3
+ t.varchar "name", limit: 110
4
+ t.int "age", unsigned: true
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", collate: "utf8_general_ci", 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
+ end
18
+
19
+ create_table "paper_author_links", collate: "utf8_general_ci", comment: "Paper Author Relation", renamed_from: "paper_authors" do |t|
20
+ t.int "id", primary_key: true, extra: "auto_increment"
21
+ t.int "paper_id", comment: "Paper id"
22
+ t.int "author_id", comment: "Paper author id"
23
+
24
+ t.foreign_key "author_id", reference: "authors", reference_column: "id", name: "paper_authors_author_id_fk"
25
+ t.foreign_key "paper_id", reference: "papers", reference_column: "id", name: "paper_authors_paper_id_fk"
26
+ end
@@ -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,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
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 "notes", null: true, renamed_from: "description"
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,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_author_links", renamed_from: "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
@@ -73,6 +108,28 @@ describe 'Command::Dryrun#execute' do
73
108
  end
74
109
  end
75
110
 
111
+ describe 'rename column' do
112
+ let(:exec_dsl) { 'rename_column_to_author.schema' }
113
+
114
+ it 'should be output rename column query, not drop+add' do
115
+ result = execute(exec_dsl)
116
+ expect(result).to be_include('# ALTER TABLE `authors` RENAME COLUMN `name` TO `full_name`;')
117
+ expect(result).not_to be_include('DROP COLUMN `name`')
118
+ expect(result).not_to be_include('ADD COLUMN `full_name`')
119
+ end
120
+ end
121
+
122
+ describe 'rename table' do
123
+ let(:exec_dsl) { 'rename_table.schema' }
124
+
125
+ it 'should be output rename table query, not drop+add' do
126
+ result = execute(exec_dsl)
127
+ expect(result).to be_include('# ALTER TABLE `paper_authors` RENAME TO `paper_author_links`;')
128
+ expect(result).not_to be_include('DROP TABLE `paper_authors`')
129
+ expect(result).not_to be_include('CREATE TABLE `paper_author_links`')
130
+ end
131
+ end
132
+
76
133
  describe 'auto increment' do
77
134
  describe 'create table with auto increment option' do
78
135
  let(:exec_dsl) { 'add_table.schema' }
@@ -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