From 97a2cd9861f6db17a939f54902009a9dbd62efc6 Mon Sep 17 00:00:00 2001 From: n atta kusi adusei Date: Wed, 1 Nov 2023 01:00:20 +0000 Subject: [PATCH] Fix CockroachDB errors (#980) * Fix CockroachDB error ``` at or near "default": syntax error (PQ::PQError) from lib/pg/src/pq/connection.cr:214:7 in 'handle_error' from lib/pg/src/pq/connection.cr:197:7 in 'handle_async_frames' from lib/pg/src/pq/connection.cr:173:7 in 'read' from lib/pg/src/pq/connection.cr:168:7 in 'read' from lib/pg/src/pq/connection.cr:446:31 in 'expect_frame' from lib/pg/src/pq/connection.cr:445:5 in 'expect_frame' from lib/pg/src/pg/statement.cr:18:5 in 'perform_query' from lib/pg/src/pg/statement.cr:35:14 in 'perform_exec' from lib/db/src/db/statement.cr:85:9 in 'perform_exec_and_release' from lib/db/src/db/statement.cr:70:7 in 'exec:args' from lib/db/src/db/query_methods.cr:275:7 in 'exec' from lib/avram/src/avram/migrator/runner.cr:60:7 in 'create_db' from lib/avram/src/avram/tasks/db/create.cr:20:5 in 'run_task' from lib/avram/src/avram/tasks/db/base_task.cr:6:7 in 'call' from spec/setup/database.cr:2:3 in '__crystal_main' from /home/runner/work/_temp/crystal-1.6.0-true-undefined/share/crystal/src/crystal/main.cr:115:5 in 'main_user_code' ``` * Improve CockroachDB compatibility See . * Add `restart_identity` parameter to `#truncate` --- src/avram/database.cr | 10 +++++++--- src/avram/migrator/runner.cr | 2 +- src/avram/queryable.cr | 9 ++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/avram/database.cr b/src/avram/database.cr index ebeef465a..d8e85b7e8 100644 --- a/src/avram/database.cr +++ b/src/avram/database.cr @@ -260,11 +260,15 @@ abstract class Avram::Database .map(&.table_name) end - def truncate + def truncate(*, cascade : Bool = true, restart_identity : Bool = true) return if table_names.empty? - statement = ("TRUNCATE TABLE #{table_names.map { |name| name }.join(", ")} RESTART IDENTITY CASCADE;") - database.exec statement + cascade_sql = cascade ? " CASCADE" : "" + restart_id_sql = restart_identity ? " RESTART IDENTITY" : "" + table_names_sql = table_names.join(", ") + sql = "TRUNCATE TABLE #{table_names_sql}#{restart_id_sql}#{cascade_sql}" + + database.exec(sql) end def delete diff --git a/src/avram/migrator/runner.cr b/src/avram/migrator/runner.cr index 94343797e..63eb4044f 100644 --- a/src/avram/migrator/runner.cr +++ b/src/avram/migrator/runner.cr @@ -57,7 +57,7 @@ class Avram::Migrator::Runner def self.create_db(quiet? : Bool = false) DB.connect("#{credentials.connection_string}/#{db_user}") do |db| - db.exec "CREATE DATABASE #{db_name} WITH OWNER DEFAULT" + db.exec "CREATE DATABASE #{db_name}" end unless quiet? puts "Done creating #{db_name.colorize(:green)}" diff --git a/src/avram/queryable.cr b/src/avram/queryable.cr index 57dad0013..ccbb9bdb0 100644 --- a/src/avram/queryable.cr +++ b/src/avram/queryable.cr @@ -53,10 +53,13 @@ module Avram::Queryable(T) # # To delete all data referenced by foreign keys as well, set *cascade* # to true. - def self.truncate(*, cascade : Bool = false) + def self.truncate(*, cascade : Bool = false, restart_identity : Bool = false) query = self.new - cascade_str = cascade ? " CASCADE" : "" - query.database.exec "TRUNCATE TABLE #{query.table_name}#{cascade_str}" + cascade_sql = cascade ? " CASCADE" : "" + restart_id_sql = restart_identity ? " RESTART IDENTITY" : "" + sql = "TRUNCATE TABLE #{query.table_name}#{restart_id_sql}#{cascade_sql}" + + query.database.exec(sql) end end