Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add :unlogged option to create_table for PostgreSQL #575

Merged
merged 1 commit into from

2 participants

@JonathanTron

No description provided.

@jeremyevans
Owner

This looks fine, I'll try merging it and testing it tomorrow. Thanks for the help!

@jeremyevans jeremyevans merged commit 97304e6 into jeremyevans:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2012
  1. @JonathanTron
This page is out of date. Refresh to see the latest.
View
17 lib/sequel/adapters/shared/postgres.rb
@@ -97,6 +97,7 @@ module DatabaseMethods
SYSTEM_TABLE_REGEXP = /^pg|sql/.freeze
FOREIGN_KEY_LIST_ON_DELETE_MAP = {'a'.freeze=>:no_action, 'r'.freeze=>:restrict, 'c'.freeze=>:cascade, 'n'.freeze=>:set_null, 'd'.freeze=>:set_default}.freeze
POSTGRES_DEFAULT_RE = /\A(?:B?('.*')::[^']+|\((-?\d+(?:\.\d+)?)\))\z/
+ UNLOGGED = 'UNLOGGED '.freeze
# SQL fragment for custom sequences (ones not created by serial primary key),
# Returning the schema and literal form of the sequence name, by parsing
@@ -634,6 +635,22 @@ def create_schema_sql(name)
"CREATE SCHEMA #{quote_identifier(name)}"
end
+ # SQL fragment for unlogged table.
+ def unlogged_table_sql
+ self.class.const_get(:UNLOGGED)
+ end
+
+ # DDL statement for creating a table with the given name, columns, and options
+ def create_table_prefix_sql(name, options)
+ raise(Error, "can't provide both :temp and :unlogged to create_table") if options[:temp] && options[:unlogged]
+ temp_or_unlogged_sql = if options[:temp]
+ temporary_table_sql
+ elsif options[:unlogged]
+ unlogged_table_sql
+ end
+ "CREATE #{temp_or_unlogged_sql}TABLE#{' IF NOT EXISTS' if options[:if_not_exists]} #{options[:temp] ? quote_identifier(name) : quote_schema_table(name)}"
+ end
+
# Use a PostgreSQL-specific create table generator
def create_table_generator_class
Postgres::CreateTableGenerator
View
3  lib/sequel/database/schema_methods.rb
@@ -154,6 +154,9 @@ def create_join_table(hash, options={})
# :collate :: The collation to use for the table.
# :engine :: The table engine to use for the table.
#
+ # PostgreSQL specific options:
+ # :unlogged :: Create the table as an unlogged table.
+ #
# See <tt>Schema::Generator</tt> and the {"Schema Modification" guide}[link:files/doc/schema_modification_rdoc.html].
def create_table(name, options={}, &block)
remove_cached_schema(name)
View
31 spec/adapters/postgres_spec.rb
@@ -17,6 +17,37 @@ def logger.method_missing(m, msg)
#POSTGRES_DB.instance_variable_set(:@server_version, 80200)
+describe "PostgreSQL", '#create_table' do
+ before do
+ @db = POSTGRES_DB
+ POSTGRES_DB.sqls.clear
+ end
+ after do
+ @db.drop_table?(:tmp_dolls)
+ @db.drop_table?(:unlogged_dolls)
+ end
+
+ specify "should create a temporary table" do
+ @db.create_table(:tmp_dolls, :temp => true){text :name}
+ check_sqls do
+ @db.sqls.should == ['CREATE TEMPORARY TABLE "tmp_dolls" ("name" text)']
+ end
+ end
+
+ specify "should create an unlogged table" do
+ @db.create_table(:unlogged_dolls, :unlogged => true){text :name}
+ check_sqls do
+ @db.sqls.should == ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']
+ end
+ end
+
+ specify "should not allow to pass both :temp and :unlogged" do
+ proc do
+ @db.create_table(:temp_unlogged_dolls, :temp => true, :unlogged => true){text :name}
+ end.should raise_error(Sequel::Error, "can't provide both :temp and :unlogged to create_table")
+ end
+end
+
describe "A PostgreSQL database" do
before(:all) do
@db = POSTGRES_DB
Something went wrong with that request. Please try again.