Skip to content

Commit

Permalink
Use table name as part of database index name (#336) (#340)
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
marksiemers authored and eliasjpr committed Oct 31, 2017
1 parent 394d524 commit 678cb93
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 16 deletions.
39 changes: 39 additions & 0 deletions spec/amber/cli/templates/crecto_migration_spec.cr
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
40 changes: 40 additions & 0 deletions spec/amber/cli/templates/granite_migration_spec.cr
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
35 changes: 35 additions & 0 deletions spec/amber/cli/templates/migration_spec.cr
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
42 changes: 42 additions & 0 deletions spec/amber/cli/templates/migration_spec_helper.cr
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
39 changes: 39 additions & 0 deletions src/amber/cli/templates/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ module Amber::CLI
@primary_key = primary_key
end

def create_index_for_reference_fields_sql
sql_statements = reference_fields.map do |field|
create_index_for_reference_field_sql(field)
end
sql_statements.join("\n")
end

def create_table_sql
<<-SQL
CREATE TABLE #{@name}s (
#{@primary_key},
#{create_table_fields_sql}
);
SQL
end

def database
if File.exists?(AMBER_YML) &&
(yaml = YAML.parse(File.read AMBER_YML)) &&
Expand All @@ -33,6 +49,10 @@ module Amber::CLI
end
end

def drop_table_sql
"DROP TABLE IF EXISTS #{@name}s;"
end

def primary_key
case @database
when "pg"
Expand All @@ -45,5 +65,24 @@ module Amber::CLI
"id INTEGER NOT NULL PRIMARY KEY"
end
end

private def create_index_for_reference_field_sql(field : Field)
index_name = "#{@name.underscore}_#{field.name}_id_idx"
<<-SQL
CREATE INDEX #{index_name} ON #{@name}s (#{field.name}_id);
SQL
end

private def create_table_field_sql(field : Field)
"#{field.name}#{field.reference? ? "_id" : ""} #{field.db_type}"
end

private def create_table_fields_sql
@fields.map{|field| create_table_field_sql(field) }.join(",\n ")
end

private def reference_fields
@fields.select(&.reference?)
end
end
end
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 %>
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 %>

0 comments on commit 678cb93

Please sign in to comment.