Skip to content

Commit

Permalink
Fix CockroachDB errors (#980)
Browse files Browse the repository at this point in the history
* 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 <cockroachdb/cockroach#38931>.

* Add `restart_identity` parameter to `#truncate`
  • Loading branch information
akadusei authored Nov 1, 2023
1 parent df3c20f commit 97a2cd9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/avram/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/avram/migrator/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down
9 changes: 6 additions & 3 deletions src/avram/queryable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 97a2cd9

Please sign in to comment.