diff --git a/.env b/.env index 31f1c32f..549b76a0 100755 --- a/.env +++ b/.env @@ -1,8 +1,8 @@ -export PG_DATABASE_URL=postgres://granite:password@localhost:5432/granite_db -export MYSQL_DATABASE_URL=mysql://granite:password@localhost:3306/granite_db -export SQLITE_DATABASE_URL=sqlite3:./granite.db -export CURRENT_ADAPTER=pg -export PG_VERSION=10.4 -export MYSQL_VERSION=5.7 -export SQLITE_VERSION=3110000 -export SQLITE_VERSION_YEAR=2016 +PG_DATABASE_URL=postgres://granite:password@localhost:5432/granite_db +MYSQL_DATABASE_URL=mysql://granite:password@localhost:3306/granite_db +SQLITE_DATABASE_URL=sqlite3:./granite.db +CURRENT_ADAPTER=pg +PG_VERSION=10.4 +MYSQL_VERSION=5.7 +SQLITE_VERSION=3110000 +SQLITE_VERSION_YEAR=2016 diff --git a/Dockerfile b/Dockerfile index 92cb7889..95c64555 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM crystallang/crystal:0.29.0 +FROM crystallang/crystal:0.30.0 ARG sqlite_version=3110000 ARG sqlite_version_year=2016 diff --git a/README.md b/README.md index 7ce9d9e9..9d11b999 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,7 @@ The test suite depends on access to a PostgreSQL, MySQL, and SQLite database to There is a self-contained testing environment provided via the `docker-compose.yml` file in this repository. We are testing against multiple databases so you have to specify which docker-compose file you would like to use. Replace "{database_type}" with "mysql" or "pg" or "sqlite". Before you can run the docker configuration you have to set the appropriate -env variables. To do so you can either load them yourself or load the .env file - -``` -$ source .env -``` +env variables. To do so you can either load them yourself or modify the `.env` file that docker-compose loads by default. You can find postgres versions at https://hub.docker.com/_/postgres/ You can find mysql versions at https://hub.docker.com/_/mysql/ diff --git a/docker-compose.yml b/docker-compose.yml index 390c381d..d64b2737 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '2' services: spec: build: . - command: 'bash -c "cd /app/user && bin/ameba && crystal tool format --check && crystal spec --warnings all --error-on-warnings"' + command: 'bash -c "cd /app/user && bin/ameba && crystal tool format --check && crystal spec --warnings all"' working_dir: /app/user environment: PG_DATABASE_URL: 'postgres://postgres:@pg:5432/postgres' diff --git a/shard.yml b/shard.yml index c66e7a79..87873bb3 100644 --- a/shard.yml +++ b/shard.yml @@ -13,20 +13,20 @@ license: MIT dependencies: db: github: crystal-lang/crystal-db - version: ~> 0.5.1 + version: ~> 0.6.0 development_dependencies: mysql: github: crystal-lang/crystal-mysql - version: ~> 0.7.0 + version: ~> 0.8.0 sqlite3: github: crystal-lang/crystal-sqlite3 - version: ~> 0.12.0 + version: ~> 0.13.0 pg: github: will/crystal-pg - version: ~> 0.16.1 + version: ~> 0.18.0 ameba: github: veelenga/ameba diff --git a/spec/granite/converters/uuid_spec.cr b/spec/granite/converters/uuid_spec.cr index 752f069e..3f5ec1bd 100644 --- a/spec/granite/converters/uuid_spec.cr +++ b/spec/granite/converters/uuid_spec.cr @@ -25,7 +25,7 @@ describe Granite::Converters::Uuid do describe ".to_db" do it "should convert a UUID enum into Bytes" do uuid = UUID.random - Granite::Converters::Uuid(Bytes).to_db(uuid).should eq uuid.to_slice + Granite::Converters::Uuid(Bytes).to_db(uuid).should eq uuid.bytes.to_slice end end @@ -34,7 +34,7 @@ describe Granite::Converters::Uuid do uuid = UUID.new "cfe37f98-fdbf-43a3-b3d8-9c3288fb9ba6" rs = FieldEmitter.new.tap do |e| - e._set_values([uuid.to_slice]) + e._set_values([uuid.bytes.to_slice]) end Granite::Converters::Uuid(Bytes).from_rs(rs).should eq uuid diff --git a/spec/granite/query/spec_helper.cr b/spec/granite/query/spec_helper.cr index fd55a566..d0f4e3a4 100644 --- a/spec/granite/query/spec_helper.cr +++ b/spec/granite/query/spec_helper.cr @@ -22,11 +22,11 @@ end def builder {% if env("CURRENT_ADAPTER").id == "pg" %} - Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Pg + Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Pg {% elsif env("CURRENT_ADAPTER").id == "mysql" %} - Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Mysql + Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Mysql {% else %} - Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Sqlite + Granite::Query::Builder(Model).new Granite::Query::Builder::DbType::Sqlite {% end %} end diff --git a/spec/mocks/db_mock.cr b/spec/mocks/db_mock.cr index 64d20f02..8425648a 100644 --- a/spec/mocks/db_mock.cr +++ b/spec/mocks/db_mock.cr @@ -3,7 +3,7 @@ class FakeStatement < DB::Statement FieldEmitter.new end - protected def perform_exec(args : Enumerable) + protected def perform_exec(args : Enumerable) : DB::ExecResult DB::ExecResult.new 0_i64, 0_i64 end end @@ -11,11 +11,11 @@ end class FakeContext include DB::ConnectionContext - def uri + def uri : URI URI.new "" end - def prepared_statements? + def prepared_statements? : Bool false end @@ -30,11 +30,11 @@ class FakeConnection < DB::Connection @prepared_statements = false end - def build_unprepared_statement(query : String) + def build_unprepared_statement(query : String) : FakeStatement FakeStatement.new self end - def build_prepared_statement(query : String) + def build_prepared_statement(query : String) : FakeStatement FakeStatement.new self end end @@ -65,9 +65,10 @@ class FieldEmitter < DB::ResultSet end end - def move_next + def move_next : Bool @position += 1 @field_position = 0 + @position < @values.size end def read @@ -80,11 +81,11 @@ class FieldEmitter < DB::ResultSet end end - def column_count + def column_count : Int32 @values.size end - def column_name(index : Int32) + def column_name(index : Int32) : String "Column #{index}" end end diff --git a/src/adapter/mysql.cr b/src/adapter/mysql.cr index 23479958..f9a44989 100644 --- a/src/adapter/mysql.cr +++ b/src/adapter/mysql.cr @@ -30,7 +30,7 @@ class Granite::Adapter::Mysql < Granite::Adapter::Base log statement, elapsed_time end - def insert(table_name : String, fields, params, lastval) + def insert(table_name : String, fields, params, lastval) : Int64 statement = String.build do |stmt| stmt << "INSERT INTO #{quote(table_name)} (" stmt << fields.map { |name| "#{quote(name)}" }.join(", ") diff --git a/src/adapter/pg.cr b/src/adapter/pg.cr index 4d20853f..9b4f5fab 100644 --- a/src/adapter/pg.cr +++ b/src/adapter/pg.cr @@ -39,7 +39,7 @@ class Granite::Adapter::Pg < Granite::Adapter::Base log statement, elapsed_time end - def insert(table_name : String, fields, params, lastval) + def insert(table_name : String, fields, params, lastval) : Int64 statement = String.build do |stmt| stmt << "INSERT INTO #{quote(table_name)} (" stmt << fields.map { |name| "#{quote(name)}" }.join(", ") diff --git a/src/adapter/sqlite.cr b/src/adapter/sqlite.cr index 226d1d4d..3292c190 100644 --- a/src/adapter/sqlite.cr +++ b/src/adapter/sqlite.cr @@ -31,7 +31,7 @@ class Granite::Adapter::Sqlite < Granite::Adapter::Base log statement, elapsed_time end - def insert(table_name : String, fields, params, lastval) + def insert(table_name : String, fields, params, lastval) : Int64 statement = String.build do |stmt| stmt << "INSERT INTO #{quote(table_name)} (" stmt << fields.map { |name| "#{quote(name)}" }.join(", ") diff --git a/src/granite/converters.cr b/src/granite/converters.cr index 77b52b9b..bd4d102b 100644 --- a/src/granite/converters.cr +++ b/src/granite/converters.cr @@ -10,7 +10,9 @@ module Granite::Converters {% if T == String %} value.to_s {% elsif T == Bytes %} - value.to_slice.dup + # we need a heap allocated slice + v = value.bytes.each.to_a + Slice.new(v.to_unsafe, v.size) {% else %} {% raise "#{@type.name}#to_db does not support #{T} yet." %} {% end %}