Rename custom attribute from sales/quote in Magento - magento

I am trying to rename a custom attribute in Magento, the following code adds the attribute I just need the equivalent to remove using the Magento installer scripts:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("customer", "is_school", array(
"type" => "int",
"backend" => "",
"label" => "Organisation Type?",
"input" => "int",
"source" => "",
"visible" => false,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$installer->getConnection()->addColumn($installer->getTable('sales/quote'), 'is_school', 'int(11)');
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'is_school', 'int(11)');
// need code to rename these two column above from is_school to 'org_type'
$installer->endSetup();

$installer->getConnection()
->changeColumn($tableName, $oldColumnName, $newColumnName,$definition);
Where
`changeColumn` method is used to modify and rename existing column in the table. It has such parameters:
$tableName - the table name that should be modified
$oldColumnName- the old name of the column, that should be renamed and modified
$newColumnName- a new name of the column
$definition - a new definition of the column (INT(10), DECIMAL(12,4), etc)
For more information
addColumn() method adds new column to exiting table. It has such parameters:
$tableName - the table name that should be modified
$columnName- the name of the column, that should be added
$definition - definition of the column (INT(10), DECIMAL(12,4), etc)
addConstraint() method creates a new constraint foreign key. It has such parameters
$fkName - the foreing key name, should be unique per database, if you don't specify FK_ prefix, it will be added automaticaly
$tableName - the table name for adding a foreign key
$columnName - the column name that should be refered to another table, if you have complex foreign key, use comma to specify more than one column
$refTableName - the foreign table name, wich will be handled
$refColumnName - the column name(s) in the foreign table
$onDelete - action on row removing in the foreign table. Can be empty string (do nothing), cascade, set null. This field is optional, and if it is not specified, cascade value will be used.
$onUpdate action on row key updating in the foreign table. Can be empty string (do nothing), cascade, set null. This field is optional, and if it is not specified, cascade value will be used.
$purge - a flag for enabling cleaning of the rows after foreign key adding (e.g. remove the recodrs that are not referenced)
addKey() method is used for adding of indexes to a table. It has such parameters:
$tableName - the table name where the index should be added
$indexName - the index name
$fields - column name(s) used in the index
$indexType - type of the index. Possible values are: index, unique, primary, fulltext. This parameter is optional, so the default value is index
dropColumn() method is used for removing of columns from the existing table. It has such parameters:
$tableName - the table name that should be modified
$columnName- the name of the column, that should removed
dropForeignKey() method is used for removing of foreing keys. It has such parameters:
$tableName - the table name for removing a foreign key
$fkName - the foreing key name
dropKey() method is used for removing of the table indexes. It has such parameters:
$tableName - the table name where the index should be removed
$keyName - the index name
modifyColumn method is used to modify existing column in the table. It has such parameters:
$tableName - the table name that should be modified
$columnName- the name of the column, that should be renamed
$definition - a new definition of the column (INT(10), DECIMAL(12,4), etc)
changeColumn method is used to modify and rename existing column in the table. It has such parameters:
$tableName - the table name that should be modified
$oldColumnName- the old name of the column, that should be renamed and modified
$newColumnName- a new name of the column
$definition - a new definition of the column (INT(10), DECIMAL(12,4), etc)
changeTableEngine method is used to change table engine, from MyISAM to InnoDB for instance. It has such parameters:
$tableName - the table name
$engine - new engine name (MEMORY, MyISAM, InnoDB, etc)
Let me know if you have any query

Related

How to ALTER TABLE [X] ADD COLUMN [Y] [type] NOT NULL in clickhouse?

When creating a table in Clickhouse DB you can do :
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|EPHEMERAL|ALIAS expr1] [compression_codec] [TTL expr1],
name2 [type2] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|EPHEMERAL|ALIAS expr2] [compression_codec] [TTL expr2],
...
) ENGINE = engine
For adding a new column with ALTER
ADD COLUMN [IF NOT EXISTS] name [type] [default_expr] [codec] [AFTER name_after | FIRST]
It seems to not be working when for example I do
ALTER TABLE product_stock_history_analytics
ADD COLUMN IF NOT EXISTS product_id STRING NOT NULL;
Is there a way to set a new column to NOT NULL in a pre-existing table? Do I have to set a default value in order to achieve the desired effect ?
ClickHouse columns are not Nullable by default. At the moment the standard SQL NOT NULL is not supported in ALTER TABLE statements, but it's also not necessary.
ALTER TABLE product_stock_history_analytics ADD COLUMN IF NOT EXISTS product_id String;
will create a not nullable String column using the standard (non ANSI SQL) ClickHouse syntax.

Find the last record based on a attribute of a table

In laravel, I create a migration table named 'timelogs'. Assumed that, this table have three column id,userid,value. 'id' is primary key and have auto increment property , 'userid' is foreign key. I insert data 1,2,2,2,2 and 3,4,5,6,7 for'userid' and 'value' field respectively.
Now I want to find last inserted record.Such as userid = 2 and value = 7.Here userid field contain different user's id. I want to find specific user's last record. How can I do this without using primary key?
$last_record = DB::table('timelogs')->where('userid', $user_id)->orderBy('id', 'desc')->first();
//var_dump($last_record);

DBUnit fails with Oracle mixed-case column names

The problem is an existing Oracle table (that I cannot change) with mixed case column names, eg
create table BADTAB ( ID varchar(16) not null, "Name" varchar2(64),
constraint I_BADTAB_PK PRIMARY KEY(ID) );
When I try to do a DBUnit INSERT from an XML dataset it fails
Caused by: java.sql.SQLException: ORA-00904: "NAME": invalid identifier
When I enclose the column name in quotes it fails
<column>"Name"</column>
org.dbunit.dataset.NoSuchColumnException: BADTAB."NAME" - (Non-uppercase input column: "ReadingsPres") in ColumnNameToIndexes cache map.
Note that the map's column names are NOT case sensitive.
at org.dbunit.dataset.AbstractTableMetaData.getColumnIndex(AbstractTableMetaData.java:117)
...
QUESTION:
How can I override DBUnit's column metadata to make it recognize the lowercase column name?
What classes do I override and how do I inject them into the DBUnit test run?
There have been some previous discussions around this org.dbunit.dataset.NoSuchTableException: Did not find table 'xxx' in schema 'null'
You should be able to set a database configuration property to cater for case-sensitive names:
DatabaseConfig config = databaseConnection.getConfig();
config.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, true);

Fetch value from database table where one column of table(foreign key) is equal to input value

I am using linq to fetch value from database table where one column of table (foreign key) is equal to input value but I am not getting that property when writing linq.
var country =
from x in db.Viewerships
where x.AssetId == model.ID
select x.CountryName;
In above code x.AssetId is not found because AssetId is used as foreign key in the table.
When you add the tables in your model, there is checkbox which says "Include foreign key columns in the model", if you didn't tick this option, it will NOT include the Foreign Key Columns in your entity. However it will by default create the navigational property for the other entity, so you have 2 options
Either re-add the table with said option ticked
Use the navigational property like this
Code:
var country =
from x in db.Viewerships
where x.Asset.ID == model.ID
select x.CountryName;
Assuming your Asset is the navigational property and Asset table's Primary key column is ID

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