Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Migrator#create and #drop sql into separate methods #326

Merged
merged 6 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions spec/granite/migrator/migrator_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
require "../../spec_helper"

describe Granite::Migrator::Base do
describe "#drop_sql" do
it "generates correct SQL with #{{{ env("CURRENT_ADAPTER") }}} adapter" do
{% if env("CURRENT_ADAPTER") == "mysql" %}
Review.migrator.drop_sql.should eq "DROP TABLE IF EXISTS `reviews`;"
{% else %}
Review.migrator.drop_sql.should eq "DROP TABLE IF EXISTS \"reviews\";"
{% end %}
end
end

describe "#create_sql" do
it "generates correct SQL with #{{{ env("CURRENT_ADAPTER") }}} adapter" do
{% if env("CURRENT_ADAPTER") == "pg" %}
Review.migrator.create_sql.should eq <<-SQL
CREATE TABLE "reviews"(
"id" BIGSERIAL PRIMARY KEY,
"name" VARCHAR(255)
Jens0512 marked this conversation as resolved.
Show resolved Hide resolved
,
"downvotes" INT
,
"upvotes" BIGINT
,
"sentiment" FLOAT
Jens0512 marked this conversation as resolved.
Show resolved Hide resolved
,
"interest" REAL
Jens0512 marked this conversation as resolved.
Show resolved Hide resolved
,
"published" BOOL
,
"created_at" TIMESTAMP
Jens0512 marked this conversation as resolved.
Show resolved Hide resolved
) ;\n
SQL

Kvs.migrator.create_sql.should eq <<-SQL
CREATE TABLE "kvs"(
"k" VARCHAR(255) PRIMARY KEY,
"v" VARCHAR(255)
) ;\n
SQL

UUIDModel.migrator.create_sql.should eq <<-SQL
CREATE TABLE "uuids"(
"uuid" UUID PRIMARY KEY) ;\n
SQL

# Also check Array types for pg
ArrayModel.migrator.create_sql.should eq <<-SQL
CREATE TABLE "array_model"(
"id" SERIAL PRIMARY KEY,
"str_array" TEXT[]
,
"i16_array" SMALLINT[]
,
"i32_array" INT[]
,
"i64_array" BIGINT[]
,
"f32_array" REAL[]
,
"f64_array" DOUBLE PRECISION[]
,
"bool_array" BOOLEAN[]
) ;\n
SQL

{% elsif env("CURRENT_ADAPTER") == "mysql" %}
Review.migrator.create_sql.should eq <<-SQL
CREATE TABLE `reviews`(
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255)
,
`downvotes` INT
,
`upvotes` BIGINT
,
`sentiment` FLOAT
,
`interest` REAL
Jens0512 marked this conversation as resolved.
Show resolved Hide resolved
,
`published` BOOL
,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ;\n
SQL

Kvs.migrator.create_sql.should eq <<-SQL
CREATE TABLE `kvs`(
`k` VARCHAR(255) PRIMARY KEY,
`v` VARCHAR(255)
) ;\n
SQL

UUIDModel.migrator.create_sql.should eq <<-SQL
CREATE TABLE `uuids`(
`uuid` CHAR(36) PRIMARY KEY) ;\n
SQL

{% elsif env("CURRENT_ADAPTER") == "sqlite" %}
Review.migrator.create_sql.should eq <<-SQL
CREATE TABLE "reviews"(
"id" INTEGER NOT NULL PRIMARY KEY,
"name" VARCHAR(255)
,
"downvotes" INTEGER
,
"upvotes" INTEGER
,
"sentiment" FLOAT
,
"interest" REAL
,
"published" BOOL
,
"created_at" VARCHAR
) ;\n
SQL

Kvs.migrator.create_sql.should eq <<-SQL
CREATE TABLE "kvs"(
"k" VARCHAR(255) PRIMARY KEY,
"v" VARCHAR(255)
) ;\n
SQL

UUIDModel.migrator.create_sql.should eq <<-SQL
CREATE TABLE "uuids"(
"uuid" CHAR(36) PRIMARY KEY) ;\n
SQL

{% end %}
end
end
end
14 changes: 10 additions & 4 deletions src/granite/migrator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ module Granite::Migrator
{% adapter = "#{klass}.adapter".id %}

disable_granite_docs? class Migrator < Granite::Migrator::Base
def drop_sql
"DROP TABLE IF EXISTS #{ @quoted_table_name };"
end

def drop
{{klass}}.exec "DROP TABLE IF EXISTS #{ @quoted_table_name };"
{{klass}}.exec drop_sql
end

def create
def create_sql
resolve = ->(key : String) {
{{adapter}}.class.schema_type?(key) || raise "Migrator(#{ {{adapter}}.class.name }) doesn't support '#{key}' yet."
}

stmt = String.build do |s|
String.build do |s|
s.puts "CREATE TABLE #{ @quoted_table_name }("

# primary key
Expand Down Expand Up @@ -82,8 +86,10 @@ module Granite::Migrator

s.puts ") #{@table_options};"
end
end

{{klass}}.exec stmt
def create
{{klass}}.exec create_sql
end
end

Expand Down