1+ use v6;
2+ use Test;
3+ use Red;
4+
5+ # Test to ensure schema creation with :if-not-exists works correctly
6+ # with the CommonSQL driver path (exception handling)
7+
8+ model TestTable is rw {
9+ has Int $.id is serial;
10+ has Str $.name is column;
11+ }
12+
13+ my $*RED-DEBUG = $_ with %*ENV<RED_DEBUG>;
14+ my $*RED-DEBUG-RESPONSE = $_ with %*ENV<RED_DEBUG_RESPONSE>;
15+ my @conf = (%*ENV<RED_DATABASE> // "SQLite").split(" ");
16+ my $driver = @conf.shift;
17+ my $*RED-DB = database $driver, |%( @conf.map: { do given .split: "=" { .[0] => val .[1] } } );
18+
19+ # Clean start
20+ lives-ok { schema(TestTable).drop }, "drop table if it exists";
21+
22+ # Create table first time
23+ lives-ok { schema(TestTable).create }, "create table normally";
24+
25+ # Now test that :if-not-exists works when table already exists
26+ lives-ok { schema(TestTable).create: :if-not-exists }, "create existing table with :if-not-exists works";
27+
28+ # Test multiple times to ensure stability
29+ lives-ok { schema(TestTable).create: :if-not-exists }, "create existing table with :if-not-exists works (2nd time)";
30+ lives-ok { schema(TestTable).create: :if-not-exists }, "create existing table with :if-not-exists works (3rd time)";
31+
32+ # Verify the table is actually functional by inserting data
33+ my $record;
34+ lives-ok { $record = TestTable.^create: :name("test-record") }, "insert data works";
35+ is $record.name, "test-record", "data integrity preserved";
36+
37+ # Verify we can still detect failure when not using the flag
38+ throws-like { schema(TestTable).create }, Exception, "normal create still fails on existing table";
39+
40+ done-testing;
0 commit comments