1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000,2008 Oracle.  All rights reserved.
4#
5# $Id: sdb009.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	sdb009
8# TEST	Test DB->rename() method for subdbs
9proc sdb009 { method args } {
10	global errorCode
11	source ./include.tcl
12
13	set omethod [convert_method $method]
14	set args [convert_args $method $args]
15
16	puts "Subdb009: $method ($args): Test of DB->rename()"
17
18	if { [is_queue $method] == 1 } {
19		puts "\tSubdb009: Skipping for method $method."
20		return
21	}
22
23	set txnenv 0
24	set envargs ""
25	set eindex [lsearch -exact $args "-env"]
26	#
27	# If we are using an env, then testfile should just be the db name.
28	# Otherwise it is the test directory and the name.
29	if { $eindex == -1 } {
30		set testfile $testdir/subdb009.db
31		set env NULL
32	} else {
33		set testfile subdb009.db
34		incr eindex
35		set env [lindex $args $eindex]
36		set envargs " -env $env "
37		set txnenv [is_txnenv $env]
38		if { $txnenv == 1 } {
39			append args " -auto_commit "
40			append envargs " -auto_commit "
41		}
42		set testdir [get_home $env]
43	}
44	set oldsdb OLDDB
45	set newsdb NEWDB
46
47	# Make sure we're starting from a clean slate.
48	cleanup $testdir $env
49	error_check_bad "$testfile exists" [file exists $testfile] 1
50
51	puts "\tSubdb009.a: Create/rename file"
52	puts "\t\tSubdb009.a.1: create"
53	set db [eval {berkdb_open -create -mode 0644}\
54	    $omethod $args {$testfile $oldsdb}]
55	error_check_good dbopen [is_valid_db $db] TRUE
56
57	# The nature of the key and data are unimportant; use numeric key
58	# so record-based methods don't need special treatment.
59	set txn ""
60	set key 1
61	set data [pad_data $method data]
62
63	if { $txnenv == 1 } {
64		set t [$env txn]
65		error_check_good txn [is_valid_txn $t $env] TRUE
66		set txn "-txn $t"
67	}
68	error_check_good dbput [eval {$db put} $txn {$key $data}] 0
69	if { $txnenv == 1 } {
70		error_check_good txn [$t commit] 0
71	}
72	error_check_good dbclose [$db close] 0
73
74	puts "\t\tSubdb009.a.2: rename"
75	error_check_good rename_file [eval {berkdb dbrename} $envargs \
76	    {$testfile $oldsdb $newsdb}] 0
77
78	puts "\t\tSubdb009.a.3: check"
79	# Open again with create to make sure we've really completely
80	# disassociated the subdb from the old name.
81	set odb [eval {berkdb_open -create -mode 0644}\
82	    $omethod $args $testfile $oldsdb]
83	error_check_good odb_open [is_valid_db $odb] TRUE
84	set odbt [$odb get $key]
85	error_check_good odb_close [$odb close] 0
86
87	set ndb [eval {berkdb_open -create -mode 0644}\
88	    $omethod $args $testfile $newsdb]
89	error_check_good ndb_open [is_valid_db $ndb] TRUE
90	set ndbt [$ndb get $key]
91	error_check_good ndb_close [$ndb close] 0
92
93	# The DBT from the "old" database should be empty, not the "new" one.
94	error_check_good odbt_empty [llength $odbt] 0
95	error_check_bad ndbt_empty [llength $ndbt] 0
96	error_check_good ndbt [lindex [lindex $ndbt 0] 1] $data
97
98	# Now there's both an old and a new.  Rename the "new" to the "old"
99	# and make sure that fails.
100	puts "\tSubdb009.b: Make sure rename fails instead of overwriting"
101	set ret [catch {eval {berkdb dbrename} $envargs $testfile \
102	    $oldsdb $newsdb} res]
103	error_check_bad rename_overwrite $ret 0
104	error_check_good rename_overwrite_ret [is_substr $errorCode EEXIST] 1
105
106	puts "\tSubdb009 succeeded."
107}
108