-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Write tests for CREATE INDEX line in migrations template (#336) * Implement including table name in CREATE INDEX line in migrations template * Refactor generating CREATE INDEX sql statements for reference fields (#336) * Refactor generating migrations * Refactor tests for migration cli templates Former-commit-id: fc551c5
- Loading branch information
1 parent
394d524
commit 678cb93
Showing
7 changed files
with
201 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "../../../spec_helper" | ||
require "./migration_spec_helper" | ||
|
||
module Amber::CLI | ||
describe CrectoMigration do | ||
describe "#render" do | ||
context "when rendering a migration with an index for belongs_to" do | ||
migration = MigrationSpecHelper.sample_migration_for(CrectoMigration) | ||
migration_text = MigrationSpecHelper.text_for(migration) | ||
|
||
it "create the index with proper naming convention" do | ||
create_index_sql = MigrationSpecHelper.sample_migration_create_index_sql | ||
migration_text.should contain create_index_sql | ||
end | ||
end | ||
|
||
context "pg" do | ||
migration = MigrationSpecHelper.sample_migration_for(GraniteMigration) | ||
migration_text = MigrationSpecHelper.text_for(migration) | ||
|
||
it "should contain correct CREATE TABLE statement" do | ||
create_table_sql = MigrationSpecHelper.sample_migration_create_table_sql_pg | ||
migration_text.should contain create_table_sql | ||
end | ||
|
||
it "should contain correct CREATE INDEX statement" do | ||
create_index_sql = MigrationSpecHelper.sample_migration_create_index_sql | ||
migration_text.should contain create_index_sql | ||
end | ||
|
||
it "should contain correct DROP TABLE statement" do | ||
drop_table_sql = MigrationSpecHelper.sample_migration_drop_table_sql | ||
migration_text.should contain drop_table_sql | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "../../../spec_helper" | ||
require "./migration_spec_helper" | ||
|
||
module Amber::CLI | ||
describe GraniteMigration do | ||
describe "#render" do | ||
context "when rendering a migration with an index for belongs_to" do | ||
migration = MigrationSpecHelper.sample_migration_for(GraniteMigration) | ||
migration_text = MigrationSpecHelper.text_for(migration) | ||
|
||
it "create the index with proper naming convention" do | ||
create_index_sql = MigrationSpecHelper.sample_migration_create_index_sql | ||
migration_text.should contain create_index_sql | ||
end | ||
|
||
end | ||
|
||
context "pg" do | ||
migration = MigrationSpecHelper.sample_migration_for(GraniteMigration) | ||
migration_text = MigrationSpecHelper.text_for(migration) | ||
|
||
it "should contain correct CREATE TABLE statement" do | ||
create_table_sql = MigrationSpecHelper.sample_migration_create_table_sql_pg | ||
migration_text.should contain create_table_sql | ||
end | ||
|
||
it "should contain correct CREATE INDEX statement" do | ||
create_index_sql = MigrationSpecHelper.sample_migration_create_index_sql | ||
migration_text.should contain create_index_sql | ||
end | ||
|
||
it "should contain correct DROP TABLE statement" do | ||
drop_table_sql = MigrationSpecHelper.sample_migration_drop_table_sql | ||
migration_text.should contain drop_table_sql | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "../../../spec_helper" | ||
require "./migration_spec_helper" | ||
|
||
module Amber::CLI | ||
describe Migration do | ||
migration = MigrationSpecHelper.sample_migration_for(Migration) | ||
|
||
describe "#create_index_for_reference_fields_sql" do | ||
it "return the correct CREATE INDEX sql string" do | ||
expected = MigrationSpecHelper.sample_migration_create_index_sql | ||
actual = migration.create_index_for_reference_fields_sql | ||
actual.should eq expected | ||
end | ||
end | ||
|
||
context "pg" do | ||
|
||
describe "#create_table_sql" do | ||
it "should return the correct CREATE TABLE statement" do | ||
create_table_sql = MigrationSpecHelper.sample_migration_create_table_sql_pg | ||
migration.create_table_sql.should eq create_table_sql | ||
end | ||
end | ||
|
||
describe "#drop_table_sql" do | ||
it "should return the correct DROP TABLE statement" do | ||
drop_table_sql = MigrationSpecHelper.sample_migration_drop_table_sql | ||
migration.drop_table_sql.should eq drop_table_sql | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Amber::CLI | ||
module MigrationSpecHelper | ||
|
||
def self.text_for(migration : Migration) : String | ||
migration_text = "" | ||
begin | ||
migration.render("./tmp") | ||
migration_filename = Dir.entries("./tmp/db/migrations").sort.last | ||
migration_text = File.read("./tmp/db/migrations/#{migration_filename}") | ||
ensure | ||
`rm -rf ./tmp/db` | ||
end | ||
return migration_text | ||
end | ||
|
||
def self.sample_migration_for(migration_template_type) | ||
migration_template_type.new("post", ["user:ref", "title:string", "body:text"]) | ||
end | ||
|
||
def self.sample_migration_create_table_sql_pg | ||
<<-SQL | ||
CREATE TABLE posts ( | ||
id BIGSERIAL PRIMARY KEY, | ||
user_id BIGINT, | ||
title VARCHAR, | ||
body TEXT, | ||
created_at TIMESTAMP, | ||
updated_at TIMESTAMP | ||
); | ||
SQL | ||
end | ||
|
||
def self.sample_migration_create_index_sql | ||
"CREATE INDEX post_user_id_idx ON posts (user_id);" | ||
end | ||
|
||
def self.sample_migration_drop_table_sql | ||
"DROP TABLE IF EXISTS posts;" | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
src/amber/cli/templates/migration/crecto/db/migrations/{{timestamp}}_create_{{name}}.sql.ecr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
-- +micrate Up | ||
CREATE TABLE <%= @name %>s ( | ||
<%= @primary_key %>, | ||
<%= @fields.map{ |field| "#{field.name}#{field.reference? ? "_id" : ""} #{field.db_type}" }.join(",\n ") %> | ||
); | ||
<% @fields.select{ |f| f.reference? }.each do |field|-%> | ||
CREATE INDEX <%= field.name %>_id_idx ON <%= @name %>s (<%= field.name %>_id); | ||
<% end -%> | ||
<%= create_table_sql %> | ||
<%= create_index_for_reference_fields_sql %> | ||
|
||
-- +micrate Down | ||
DROP TABLE IF EXISTS <%= @name %>s; | ||
<%= drop_table_sql %> |
11 changes: 3 additions & 8 deletions
11
...amber/cli/templates/migration/granite/db/migrations/{{timestamp}}_create_{{name}}.sql.ecr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
-- +micrate Up | ||
CREATE TABLE <%= @name %>s ( | ||
<%= @primary_key %>, | ||
<%= @fields.map{ |field| "#{field.name}#{field.reference? ? "_id" : ""} #{field.db_type}" }.join(",\n ") %> | ||
); | ||
<% @fields.select{ |f| f.reference? }.each do |field|-%> | ||
CREATE INDEX <%= field.name %>_id_idx ON <%= @name %>s (<%= field.name %>_id); | ||
<% end -%> | ||
<%= create_table_sql %> | ||
<%= create_index_for_reference_fields_sql %> | ||
|
||
-- +micrate Down | ||
DROP TABLE IF EXISTS <%= @name %>s; | ||
<%= drop_table_sql %> |