@@ -74,6 +74,35 @@ updated_at TIMESTAMP(3) WITH TIME ZONE
7474 );
7575 """ ;
7676
77+ /**
78+ * A snapshot whose table-level foreign key and check constraints are
79+ * accepted and ignored by the schema parser, together with the column
80+ * defaults and named constraints that surround them.
81+ */
82+ private static final String CONSTRAINT_SCHEMA = """
83+ CREATE TABLE customers
84+ (
85+ id BIGINT NOT NULL,
86+ name VARCHAR(255) NOT NULL,
87+ CONSTRAINT customers_pk PRIMARY KEY (id),
88+ CONSTRAINT customers_name_unique UNIQUE (name)
89+ );
90+
91+ CREATE TABLE customer_orders
92+ (
93+ id BIGINT NOT NULL,
94+ customer_id BIGINT NOT NULL,
95+ quantity INTEGER NOT NULL DEFAULT 1,
96+ status VARCHAR(32) DEFAULT 'new',
97+ created_at TIMESTAMP DEFAULT now(),
98+ PRIMARY KEY (id),
99+ CONSTRAINT customer_orders_customer_fk FOREIGN KEY (customer_id)
100+ REFERENCES customers (id) ON DELETE CASCADE,
101+ CONSTRAINT customer_orders_quantity_check CHECK (quantity > 0),
102+ CHECK (status <> '')
103+ );
104+ """ ;
105+
77106 private static final UUID EXTERNAL_ID = UUID .fromString ("3f2504e0-4f89-11d3-9a0c-0305e82c3301" );
78107
79108 private static final OffsetDateTime UPDATED_AT = OffsetDateTime .of (
@@ -128,6 +157,8 @@ static void stopPostgres() {
128157 */
129158 @ BeforeEach
130159 void resetDatabase () throws Exception {
160+ execute ("DROP TABLE IF EXISTS customer_orders" );
161+ execute ("DROP TABLE IF EXISTS customers" );
131162 execute ("DROP TABLE IF EXISTS users" );
132163 execute (SCHEMA );
133164 }
@@ -453,16 +484,64 @@ INSERT INTO users (id, code, serial_id, revision, external_id, updated_at)
453484 }
454485
455486 /**
456- * Compiles the schema snapshot and the given queries, then compiles every
457- * generated Java file into an isolated temporary classes directory.
487+ * Proves that a snapshot carrying table-level foreign key and check
488+ * constraints, column defaults, and named constraints is valid PostgreSQL
489+ * DDL, compiles through the pipeline, and reads back the values the
490+ * database defaults produced.
458491 */
492+ @ Test
493+ void shouldExecuteGeneratedQueryForSnapshotWithIgnoredTableConstraints () throws Exception {
494+ execute (CONSTRAINT_SCHEMA );
495+
496+ Path classesDirectory = generateAndCompile (
497+ CONSTRAINT_SCHEMA ,
498+ """
499+ -- name: GetCustomerOrder :one
500+ SELECT id, customer_id, quantity, status
501+ FROM customer_orders
502+ WHERE id = $1;
503+ """
504+ );
505+
506+ execute ("INSERT INTO customers (id, name) VALUES (1, 'Alice')" );
507+ execute ("INSERT INTO customer_orders (id, customer_id) VALUES (10, 1)" );
508+
509+ try (URLClassLoader classLoader = classLoader (classesDirectory )) {
510+ Object query = newQuery (classLoader , "GetCustomerOrder" );
511+
512+ Method method = query .getClass ().getMethod ("getCustomerOrder" , Long .class );
513+
514+ Object result = method .invoke (query , 10L );
515+
516+ assertNotNull (result );
517+
518+ assertEquals (
519+ List .of ("id" , "customer_id" , "quantity" , "status" ),
520+ recordComponentNames (result )
521+ );
522+
523+ assertEquals (10L , component (result , "id" ));
524+ assertEquals (1L , component (result , "customer_id" ));
525+ assertEquals (1 , component (result , "quantity" ));
526+ assertEquals ("new" , component (result , "status" ));
527+ }
528+ }
529+
459530 private Path generateAndCompile (String queries ) throws Exception {
531+ return generateAndCompile (SCHEMA , queries );
532+ }
533+
534+ /**
535+ * Compiles the given schema snapshot and queries, then compiles every
536+ * generated Java file into an isolated temporary classes directory.
537+ */
538+ private Path generateAndCompile (String schema , String queries ) throws Exception {
460539 Path schemaFile = tempDir .resolve ("schema.sql" );
461540 Path queriesFile = tempDir .resolve ("queries.sql" );
462541 Path generatedDirectory = tempDir .resolve ("generated" );
463542 Path classesDirectory = tempDir .resolve ("classes" );
464543
465- Files .writeString (schemaFile , SCHEMA );
544+ Files .writeString (schemaFile , schema );
466545 Files .writeString (queriesFile , queries );
467546
468547 Config config = new Config (
0 commit comments