ActiveRecord NullDB adapter not loading tables/columns - ruby

I am trying to use the NullDB database adapter to create/run specs for an external database. As I dont want to query the external database for the specs I want to use NullDB instead.
The Problem is: I created a seperate schema.rb file that should be used by nulldb, set nulldb as adapter and the schema.rb path in my config for the test env, but, my models dont have columns, therefore methods like name= are also failing.
NullDb reads out the schema correct and also creates tables with the columns.
NullDBNamespace::Product.columns
(4.2ms) DROP TABLE "products"
(9.8ms) CREATE TABLE "products" ("id" serial primary key, "products_id" integer, "products_model" integer, "products_master" integer, "manufacturers_id" integer, "products_tax_class_id" integer, "products_quantity" integer, "products_evp" decimal(16,8), "products_ean" character varying)
(3.1ms) DROP TABLE "descriptions"
(7.1ms) CREATE TABLE "descriptions" ("id" serial primary key, "products_id" integer, "language_id" integer, "products_name" character varying, "products_description" text)
(3.1ms) DROP TABLE "languages"
(5.9ms) CREATE TABLE "languages" ("id" serial primary key, "languages_id" integer, "name" character varying, "code" character varying)
but I get
NullDBNamespace::Product.columns
=> []
also there are no tables for the connection:
NullDBNamespace::Product.connection.tables
=> ["schema_info"]
Does anybody has an idea?

Related

Springboot test fail because of table not found from H2 database, but the table has been created

The problem that I can see is that the table is created with the name 'article' and the query is done with ARTICLE in capital letter. I tried to change the application.properties file to this:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DATABASE_TO_LOWER=TRUE
but it doesn't help the problem.
Here is some output from the console that demonstrate the table has been created:
Hibernate: create table "article" ("id" bigint generated by default as identity, "content" clob, "language" integer, "last_edited" timestamp, "published" timestamp, "title" varchar(255), "image_id" bigint, "user_id" bigint not null, primary key ("id"))
and later this pops up:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ARTICLE" not found; SQL statement: select * from Article where language=? ORDER BY published desc
I tried to name the table in uppercase letter with
DATABASE_TO_LOWER=FALSE, DATABASE_TO_UPPER=TRUE;
and it didn't help either.
What could I try next?

drop foreign key constraint on DB2 table through alter table

I have a DB2 (for IBMi) table created as below. I would like to drop the forign key constraint while running in an SQLRPGLE program. Is this possible?
create table grid_action_details(id integer not null
generated always as identity
(start with 1 increment by 1)
PRIMARY KEY,grid_details_id integer,foreign key(grid_details_id)
references grid_details(id),action_code_details_id integer,
foreign key(action_code_details_id)
references action_code_details(id),action_code_status varchar(2),
created_date date default
current_date,created_by varchar(30),
last_updated_date date default current_date,updated_by
varchar(30),required_parameter clob);
I tried the below syntax but it just doesn't seem to work for me:
ALTER TABLE table-name
DROP FOREIGN KEY foreign_key_name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_id
ACTION_CODE_DETAILS_ID in IESQAFILE type *N not found.
You drop the FK constraint via name, not via column.
Since you didn't specify one during create, you'll need to look to see what name the system generated.
Always a best practice to name things yourself.
CONSTAINT <name> FOREIGN KEY (<columns>)
create table grid_action_details (
id integer not null generated always as identity (
start with 1 increment by 1
)
,constraint grid_action_details_pk
primary key
,grid_details_id integer
,constraint grid_details_fk
foreign key (grid_details_id)
references grid_details (id)
,action_code_details_id integer
,constraint action_code_details_fk
foreign key (action_code_details_id)
references action_code_details (id)
,action_code_status varchar(2)
,created_date date default current_date
,created_by varchar(30)
,last_updated_date date default current_date
,updated_by varchar(30)
,required_parameter clob
);
Now you can drop by the known name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_fk
EDIT
To find the generated name use:
the ACS Schema component
DSPFD
SQL against one of the catalog views (QSYS2.SYSCST, SYSIBM.SQLFOREIGNKEYS, SYSIBM.REFERENTIAL_CONSTRAINTS )

How do I create a H2 database with the date data type

I am trying to create a database in H2 (version 1.4.199), I need to include the date data type which is causing me issues when creating the database. I get a 50004 unknown data type error.
Can anyone advise where I am going wrong?
CREATE TABLE IF NOT EXISTS chapter (
CHAPTER_ID long,
STUDENT_ID long,
DATE - FORMAT yyyy-mm-dd,
UNIQUE (DATE),
PRIMARY KEY (CHAPTER_ID)
);
Your table definition is not valid, it should be
CREATE TABLE IF NOT EXISTS chapter (
CHAPTER_ID BIGINT,
STUDENT_ID BIGINT,
DATE DATE,
UNIQUE (DATE),
PRIMARY KEY (CHAPTER_ID)
);
or something like it.

Create POSTGRES table with VARCHAR[] in H2 in-memory database

I have DDL for my PostgreSQL database. One of the columns in the table is of type VARCHAR[]. H2 fails to create this table, even if the database is running in PosgtreSQL compatibility mode (url: jdbc:h2:mem:user-management;MODE=PostgreSQL)
Simplified DDL:
create table "users" (
"uuid" VARCHAR NOT NULL PRIMARY KEY,
"roles" VARCHAR[] NOT NULL
);
Is there any data type that is compatible between H2 and PostgreSQL and is actually a varchar array? Thanks!
It is possible to use array here, it's worked for me in h2 postgre mode.
create table "users" (
"uuid" VARCHAR NOT NULL PRIMARY KEY,
"roles" varchar array
);

How to rename a primary key in Oracle such that it can be reused

On Oracle, I create a table like this:
CREATE TABLE "Mig1"(
"Id" INTEGER NOT NULL
, CONSTRAINT "PK_Mig1" PRIMARY KEY
(
"Id" )
)
Then, I rename the PK:
ALTER TABLE "Mig1" RENAME CONSTRAINT "PK_Mig1" TO "PK_XXX"
Then, I rename the table:
ALTER TABLE "Mig1" RENAME TO "XXX"
Then, I try to create another table that uses the name of the previously renamed table:
CREATE TABLE "Mig1"(
"Id" INTEGER NOT NULL
, CONSTRAINT "PK_Mig1" PRIMARY KEY
(
"Id" )
)
At this point I get: An error occurred: ORA-00955: name is already used by an existing object. And this is because somehow the primary key of the first table is still around in some way although it was renamed. If I try to create the second table like this:
CREATE TABLE "Mig1"(
"Id" INTEGER NOT NULL
, CONSTRAINT "YYY" PRIMARY KEY
(
"Id" )
)
it works. So how do I rename the primary key correctly with all of its associated resources such that its name can be reused?
There is an index associated with the primary key constraint, and it is probably still called "PK_Mig1". Try this:
ALTER INDEX "PK_Mig1" RENAME TO "PK_XXX";

Resources